3

I am trying to perform the ShrinkWrap deformer on two 3D models to be able later on to perform the morph from one model to another . When i apply the function i did , when i manipulate the offset value or the target inflation it doesn't give me the same shape of the second Model , it gives me an incomplete different shape, here is the code:

import maya.cmds as cmds 

def matchCurveShapes_andShrinkWrap(firstModel, secondModel):      
    myDict={
         ".boundingBoxCenter":1,
         ".axisReference":0 ,
         ".alongX":1,
         ".alongY":1,
         ".alongZ":1,
         ".offset":0,
         ".targetInflation":0,
         ".falloffIterations":1
     } 
    cmds.delete(firstModel, ch=1)
    getShrink=cmds.deformer(firstModel, type="shrinkWrap")
    cmds.connectAttr(secondModel+".worldMesh[0]", getShrink[0]+".targetGeom", f=1)

    for key, value in myDict.items():
        cmds.setAttr(getShrink[0]+key, value)
                 
matchCurveShapes_andShrinkWrap('pCylinder1', 'pCube3') 
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
john wick
  • 31
  • 2

2 Answers2

2

I can give you my piece of code to create a shrinkWrap, I don't have maya so can't really check your code :

def getTargetMesh(targetTrans=str):

    sh = cmds.ls(targetTrans, dag=True, shapes=True)
    # Find if at least one of them is an allowable target type
    for s in sh:
        io = cmds.getAttr(s+".io")
        if io:
            continue

        mtype = cmds.nodeType(s)
        if mtype == "mesh":
            return s
    return None

def shrinkWrap(mesh, target, **kwargs):

    targetMesh = getTargetMesh(target) # find a not intermediate shape

    # Find all the surf transforms that have been selected
    surf = cmds.listRelatives(mesh, path=True)

    surface = surf[0]
    
    # SET A BUNCH OF ATTRIBUTES WITH KWARGS or with default value    
    projection = kwargs.get('projection') or 0
    closestIfNoIntersection = kwargs.get('closestIfNoIntersection') or 0
    reverse = kwargs.get('reverse') or 0
    bidirectional = kwargs.get('bidirectional') or 0
    boundingBoxCenter = kwargs.get('boundingBoxCenter') or 1
    axisReference = kwargs.get('axisReference') or 0
    alongX = kwargs.get('alongX') or 0
    alongY = kwargs.get('alongY') or 0
    alongZ = kwargs.get('alongZ') or 0
    offset = kwargs.get('offset') or 0
    targetInflation = kwargs.get('targetInflation') or 0

    shrinkwrapNode = cmds.deformer(surface, type='shrinkWrap')[0]

    cmds.setAttr(shrinkwrapNode + ".projection", projection)
    cmds.setAttr(shrinkwrapNode + ".closestIfNoIntersection", closestIfNoIntersection)
    cmds.setAttr(shrinkwrapNode + ".reverse", reverse)
    cmds.setAttr(shrinkwrapNode + ".bidirectional", bidirectional)
    cmds.setAttr(shrinkwrapNode + ".boundingBoxCenter", boundingBoxCenter)
    cmds.setAttr(shrinkwrapNode + ".axisReference", axisReference)
    cmds.setAttr(shrinkwrapNode + ".alongX", alongX)
    cmds.setAttr(shrinkwrapNode + ".alongY", alongY)
    cmds.setAttr(shrinkwrapNode + ".alongZ", alongZ)
    cmds.setAttr(shrinkwrapNode + ".offset", offset)
    cmds.setAttr(shrinkwrapNode + ".targetInflation", targetInflation)

    # Add the target object
    #
    cmds.connectAttr(targetMesh + ".w", shrinkwrapNode + ".tgt")
    # connect up the smooth target attributes
    # so the smoothed target follows the target shape's settings
    #
    cmds.connectAttr(targetMesh + ".co", shrinkwrapNode + ".co")
    cmds.connectAttr(targetMesh + ".suv", shrinkwrapNode + ".suv")
    cmds.connectAttr(targetMesh + ".kb", shrinkwrapNode + ".kb")
    cmds.connectAttr(targetMesh + ".bnr", shrinkwrapNode + ".bnr")
    cmds.connectAttr(targetMesh + ".khe", shrinkwrapNode + ".khe")
    cmds.connectAttr(targetMesh + ".peh", shrinkwrapNode + ".peh")
    cmds.connectAttr(targetMesh + ".kmb", shrinkwrapNode + ".kmb")

    cmds.select(clear=True)
    return shrinkwrapNode

Here is a function where im using it to output a sphere with only quads

def createSquareSphere(res=4):
    cub = cmds.polyCube(n = 'qSphere#')
    v = round(sqrt(pow(4, res)))*2
    sph = cmds.polySphere(n = 'proj_tmp', sa=v, sh=v)
    cmds.polySmooth(cub, dv = 4, mth = 0, sdt = 2, ovb = 1, ofb = 3, ofc = 0, ost = 0, ocr = 0, bnr = 1,
                    c = 1, kb = 1, ksb = 1, khe = 0, kt = 1, kmb = 1, suv = 1, peh = 0, sl = 1,
                    dpe = 1, ps = 0.1, ro = 1, ch = 1)
    shWrp = shrinkWrap(cub[0], sph[0], projection = 3, reverse = 1)
    cmds.delete(cub, ch = True)
    cmds.delete(sph)
    return cub[0]
DrWeeny
  • 2,487
  • 1
  • 14
  • 17
  • # targetMesh = getTargetMesh(target) # find a not intermediate shape ......... It says that the name "getTargetMesh" is not defined – john wick Jun 29 '21 at 08:49
  • yeah you can use your own to find the shape of the target, i can paste the def if you want but it was more if you wanted to compare with your own input and connections – DrWeeny Jun 29 '21 at 09:29
  • yeah , can you please paste the def , just in case my code won't match yours, syntax wise? – john wick Jun 29 '21 at 09:41
1

I cleaned up DrWeeny's excellent answer for my own purposes and figured I might as well post it up.

import maya.cmds as cmds

def create_shrink_wrap(mesh, target, **kwargs):
    """
    Check available kwargs with parameters below.
    """
    parameters = [
        ("projection", 2),
        ("closestIfNoIntersection", 1),
        ("reverse", 0),
        ("bidirectional", 1),
        ("boundingBoxCenter", 1),
        ("axisReference", 1),
        ("alongX", 0),
        ("alongY", 0),
        ("alongZ", 1),
        ("offset", 0),
        ("targetInflation", 0),
        ("targetSmoothLevel", 0),
        ("falloff", 0),
        ("falloffIterations", 1),
        ("shapePreservationEnable", 0),
        ("shapePreservationSteps", 1)
    ]

    target_shapes = cmds.listRelatives(target, f=True, shapes=True, type="mesh", ni=True)
    if not target_shapes:
        raise ValueError("The target supplied is not a mesh")
    target_shape = target_shapes[0]

    shrink_wrap = cmds.deformer(mesh, type="shrinkWrap")[0]

    for parameter, default in parameters:
        cmds.setAttr(
            shrink_wrap + "." + parameter,
            kwargs.get(parameter, default))

    connections = [
        ("worldMesh", "targetGeom"),
        ("continuity", "continuity"),
        ("smoothUVs", "smoothUVs"),
        ("keepBorder", "keepBorder"),
        ("boundaryRule", "boundaryRule"),
        ("keepHardEdge", "keepHardEdge"),
        ("propagateEdgeHardness", "propagateEdgeHardness"),
        ("keepMapBorders", "keepMapBorders")
    ]

    for out_plug, in_plug in connections:
        cmds.connectAttr(
            target_shape + "." + out_plug,
            shrink_wrap + "." + in_plug)

    return shrink_wrap
Green Cell
  • 4,677
  • 2
  • 18
  • 49