问题描述:
在Maya中使用默认的编写器创建命令 cmds.deformer(type='wrap')
仅仅只能创建一个Wrap节点,并不能获得正确的节点关系。所以还需要建立起正确的节点连接关系。
解决方案:
这里将建立Wrap节点的过程封装成了一个函数。
def createWrap(*args, **kwargs):
influence = args[0]
surface = args[1]
shapes = mc.listRelatives(influence, shapes=True)
influenceShape = shapes[0]
shapes = mc.listRelatives(surface, shapes=True)
surfaceShape = shapes[0]
weightThreshold = kwargs.get('weightThreshold', 0.0)
maxDistance = kwargs.get('maxDistance', 1.0)
exclusiveBind = kwargs.get('exclusiveBind', False)
autoWeightThreshold = kwargs.get('autoWeightThreshold', True)
falloffMode = kwargs.get('falloffMode', 0)
wrapData = mc.deformer(surface, type='wrap')
wrapNode = wrapData[0]
mc.setAttr(wrapNode + '.weightThreshold', weightThreshold)
mc.setAttr(wrapNode + '.maxDistance', maxDistance)
mc.setAttr(wrapNode + '.exclusiveBind', exclusiveBind)
mc.setAttr(wrapNode + '.autoWeightThreshold', autoWeightThreshold)
mc.setAttr(wrapNode + '.falloffMode', falloffMode)
mc.connectAttr(surface + '.worldMatrix[0]', wrapNode + '.geomMatrix')
duplicateData = mc.duplicate(influence, name=influence + 'Base')
base = duplicateData[0]
shapes = mc.listRelatives(base, shapes=True)
baseShape = shapes[0]
mc.hide(base)
if not mc.attributeQuery('dropoff', n=influence, exists=True):
mc.addAttr(influence, sn='dr', ln='dropoff', dv=4.0, min=0.0, max=20.0)
mc.setAttr(influence + '.dr', k=True)
if mc.nodeType(influenceShape) == 'mesh':
if not mc.attributeQuery('smoothness', n=influence, exists=True):
mc.addAttr(influence, sn='smt', ln='smoothness', dv=0.0, min=0.0)
mc.setAttr(influence + '.smt', k=True)
if not mc.attributeQuery('inflType', n=influence, exists=True):
mc.addAttr(influence, at='short', sn='ift', ln='inflType', dv=2, min=1, max=2)
mc.connectAttr(influenceShape + '.worldMesh', wrapNode + '.driverPoints[0]')
mc.connectAttr(baseShape + '.worldMesh', wrapNode + '.basePoints[0]')
mc.connectAttr(influence + '.inflType', wrapNode + '.inflType[0]')
mc.connectAttr(influence + '.smoothness', wrapNode + '.smoothness[0]')
if mc.nodeType(influenceShape) == 'nurbsCurve' or mc.nodeType(influenceShape) == 'nurbsSurface':
if not mc.attributeQuery('wrapSamples', n=influence, exists=True):
mc.addAttr(influence, at='short', sn='wsm', ln='wrapSamples', dv=10, min=1)
mc.setAttr(influence + '.wsm', k=True)
mc.connectAttr(influenceShape + '.ws', wrapNode + '.driverPoints[0]')
mc.connectAttr(baseShape + '.ws', wrapNode + '.basePoints[0]')
mc.connectAttr(influence + '.wsm', wrapNode + '.nurbsSamples[0]')
mc.connectAttr(influence + '.dropoff', wrapNode + '.dropoff[0]')
return wrapNode
调用时直接传入用于变形的物体的Transform Node和被变形的物体的Transform Node即可。
用例
# 用于包裹变形的物体的Transform节点
wrap_deformer = cmds.polyPlane(sh=3, sw=3, name="p1")[0]
# 被变形的物体的Transform节点
org_plane = cmds.polyPlane(sh=10, sw=10, name="p2")[0]
# 创建Wrap Node
# weightThreshold, maxDistance, exclusiveBind, autoWeightTreshold 为Wrap节点的属性配置参数,具体意义可见官方帮助
createWrap(wrap_deformer, org_plane, weightThreshold=0, maxDistance=1, exclusiveBind=False, autoWeightThreshold=True, falloffMode=0)