def ucGetBasePoints(): sels = cmds.ls( sl=1 ) model.fnMeshList = [] model.fnCrvList = [] model.meshPoints = [] model.crvPoints = [] for target in sels: targetShapes = cmds.listRelatives( target, s=1 ) for shape in targetShapes: if cmds.nodeType( shape ) == 'mesh': fnMesh = om.MFnMesh( functions.getMObject( shape ) ) points = om.MPointArray() fnMesh.getPoints( points ) model.fnMeshList.append( fnMesh ) model.meshPoints.append( points ) elif cmds.nodeType( shape ) == 'nurbsCurve': fnCurve = om.MFnNurbsCurve( functions.getMObject( shape ) ) points = om.MPointArray() fnCurve.getCVs( points ) model.fnCrvList.append( fnCurve ) model.crvPoints.append( points )
def addMatrixInfo(self, node,skinNode, index ): fnNode = om.MFnDependencyNode( fnc.getMObject( node ) ) infoPlug = fnNode.findPlug( "blendMeshInfos" ) plugElement = infoPlug.elementByLogicalIndex( index ); keepMatrixPlug = plugElement.child( 4 ) fnSkinNode = om.MFnDependencyNode( fnc.getMObject( skinNode ) ) matrixPlug = fnSkinNode.findPlug( "matrix" ) for i in range( matrixPlug.numElements() ): logicalIndex = matrixPlug[i].logicalIndex() matrixObj = matrixPlug[i].asMObject() keepMatrixElement = keepMatrixPlug.elementByLogicalIndex( logicalIndex ) keepMatrixElement.setMObject( matrixObj )
def checkBlendMesh( targetMeshObj ): node = bc.getBlendAndFixedShape( targetMeshObj ) fnNode = om.MFnDependencyNode( fnc.getMObject( node ) ) blendMeshPlug = fnNode.findPlug( "blendMeshInfos" ) addSpace = ' ' for i in range( blendMeshPlug.numElements() ): inputMeshPlug = blendMeshPlug[i].child( 0 ) targetWPlug = blendMeshPlug[i].child( 2 ) namePlug = blendMeshPlug[i].child( 3 ) cons = om.MPlugArray() inputMeshPlug.connectedTo( cons, True, False ) print addSpace + "Input Mesh :", if cons.length(): print cons[0].name() else: print namePlug.asString() print addSpace + "Target Weights :" for i in range( targetWPlug.numElements() ): loIndex = targetWPlug[i].logicalIndex() value = targetWPlug[i].asDouble() print " "*20 + "[%d]" % loIndex + " : %3.2f" % value
def getSurfaceCenterCurve(surface, multMatrix=False): fnSurface = om.MFnNurbsSurface(fnc.getMObject(surface)) if multMatrix: surfaceMatrix = fnc.getMMatrixFromMtxList(cmds.getAttr(surface + '.wm')) else: surfaceMatrix = om.MMatrix() degreeU = fnSurface.degreeU() numU = fnSurface.numCVsInU() numV = fnSurface.numCVsInV() cvPoints = [] for i in range(numU): bbox = om.MBoundingBox() point = om.MPoint() for j in range(numV): fnSurface.getCV(i, j, point) bbox.expand(point) minPoint = om.MVector(bbox.min()) maxPoint = om.MVector(bbox.max()) cPoint = om.MPoint((minPoint + maxPoint) / 2) * surfaceMatrix cvPoints.append([cPoint.x, cPoint.y, cPoint.z]) return cmds.curve(p=cvPoints, d=degreeU)
def getSurfaceCenterCurve( surface, multMatrix=False ): fnSurface = om.MFnNurbsSurface( fnc.getMObject( surface ) ) if multMatrix: surfaceMatrix = fnc.getMMatrixFromMtxList( cmds.getAttr( surface+'.wm' ) ) else: surfaceMatrix = om.MMatrix() degreeU = fnSurface.degreeU() numU = fnSurface.numCVsInU() numV = fnSurface.numCVsInV() cvPoints = [] for i in range( numU ): bbox = om.MBoundingBox() point = om.MPoint() for j in range( numV ): fnSurface.getCV( i, j, point ) bbox.expand( point ) minPoint = om.MVector( bbox.min() ) maxPoint = om.MVector( bbox.max() ) cPoint = om.MPoint(( minPoint + maxPoint )/2)*surfaceMatrix cvPoints.append( [cPoint.x,cPoint.y,cPoint.z] ) return cmds.curve( p=cvPoints, d=degreeU )
def blendMeshAssign( target, deformObj, driverIndices=[], driverWeights=[], targetIndex=None ): node = fnc.getNodeFromHist( deformObj, 'blendAndFixedShape' ) if not node: node = bc.blendAndFix_toSkin( deformObj ) if not node: node = cmds.deformer( deformObj, type='blendAndFixedShape' )[0] meshObjs = cmds.listConnections( node, s=1, d=0, type='mesh' ) if meshObjs: if target in meshObjs: targetShape = cmds.listRelatives( target, s=1 )[0] attrName = targetShape+'.outMesh' fnNode = om.MFnDependencyNode( fnc.getMObject( node ) ) blendMeshInfoPlugs = fnNode.findPlug( 'blendMeshInfos' ) numElements = blendMeshInfoPlugs.numElements() for i in range( numElements ): inputMeshPlug = blendMeshInfoPlugs[i].child( 0 ) cons = om.MPlugArray() inputMeshPlug.connectedTo( cons, True, False ) if cons.length(): if cons[0].name() == attrName: targetIndex = i cmds.removeMultiInstance( "%s[%d]" %( blendMeshInfoPlugs.name(), targetIndex ) ) print blendMeshInfoPlugs.name(), i, 'is deleted' break if not targetIndex == None: assignNum = targetIndex else: fnNode = om.MFnDependencyNode( fnc.getMObject( node ) ) blendMeshInfoPlug = fnNode.findPlug( 'blendMeshInfos' ) assignNum = blendMeshInfoPlug.numElements() targetShape = cmds.listRelatives( target, s=1 )[0] cmds.connectAttr( targetShape+'.outMesh', node+'.blendMeshInfos[%d].inputMesh' % assignNum ) for i in range( len( driverIndices ) ): index = driverIndices[i] weight = driverWeights[i] cmds.setAttr( node+'.blendMeshInfos[%d].targetWeights[%d]' %( assignNum, index ), weight )
def selectDeltaPoints(nodeName, index, *args): fnNode = om.MFnDependencyNode(fnc.getMObject(nodeName)) plugBlendMeshInfos = fnNode.findPlug("blendMeshInfos") if index >= plugBlendMeshInfos.numElements(): return None plugBlendMeshInfos[index]
def selectDeltaPoints( nodeName, index, *args ): fnNode = om.MFnDependencyNode( fnc.getMObject(nodeName) ) plugBlendMeshInfos = fnNode.findPlug( "blendMeshInfos" ) if index >= plugBlendMeshInfos.numElements(): return None plugBlendMeshInfos[ index ]
def editShapeByMatrix( target, mmtx ): targetShapes = cmds.listRelatives( target, s=1 ) for shape in targetShapes: if cmds.nodeType( shape ) == 'mesh': fnMesh = om.MFnMesh( functions.getMObject( shape ) ) points = om.MPointArray() fnMesh.getPoints( points ) for i in range( points.length() ): points.set( points[i]*mmtx, i ) cmds.move( points[i].x, points[i].y, points[i].z, shape+'.vtx[%d]' % i, os=1 ) elif cmds.nodeType( shape ) == 'nurbsCurve': fnCurve = om.MFnNurbsCurve( functions.getMObject( shape ) ) points = om.MPointArray() fnCurve.getCVs( points ) for i in range( points.length() ): points.set( points[i]*mmtx, i ) cmds.move( points[i].x, points[i].y, points[i].z, shape+'.cv[%d]' % i, os=1 )
def getSurfaceUDistance(surface, multMatrix=False): fnSurface = om.MFnNurbsSurface(fnc.getMObject(surface)) if multMatrix: surfaceMatrix = fnc.getMMatrixFromMtxList(cmds.getAttr(surface + '.wm')) else: surfaceMatrix = om.MMatrix() degreeU = fnSurface.degreeU() numSpansU = fnSurface.numSpansInU() numU = fnSurface.numCVsInU() numV = fnSurface.numCVsInV() cvPoints = om.MPointArray() cvPoints.setLength(numU) knots = om.MDoubleArray() knots.setLength(numU + degreeU - 1) for i in range(numU): bbox = om.MBoundingBox() point = om.MPoint() for j in range(numV): fnSurface.getCV(i, j, point) bbox.expand(point) minPoint = om.MVector(bbox.min()) maxPoint = om.MVector(bbox.max()) cPoint = om.MPoint((minPoint + maxPoint) / 2) * surfaceMatrix cvPoints.set(cPoint, i) for i in range(numU + degreeU - 1): if i < degreeU - 1: knots[i] = 0 elif i - degreeU + 1 > numSpansU: knots[i] = numSpansU else: knots[i] = i - degreeU + 1 curveData = om.MFnNurbsCurveData() createCurveObj = curveData.create() fnCreateCurve = om.MFnNurbsCurve() fnCreateCurve.create(cvPoints, knots, degreeU, om.MFnNurbsCurve.kOpen, 0, 0, createCurveObj) fnCurve = om.MFnNurbsCurve(createCurveObj) return fnCurve.length()
def getSurfaceUDistance( surface, multMatrix=False ): fnSurface = om.MFnNurbsSurface( fnc.getMObject( surface ) ) if multMatrix: surfaceMatrix = fnc.getMMatrixFromMtxList( cmds.getAttr( surface+'.wm' ) ) else: surfaceMatrix = om.MMatrix() degreeU = fnSurface.degreeU() numSpansU = fnSurface.numSpansInU() numU = fnSurface.numCVsInU() numV = fnSurface.numCVsInV() cvPoints = om.MPointArray() cvPoints.setLength( numU ) knots = om.MDoubleArray() knots.setLength( numU+degreeU-1 ) for i in range( numU ): bbox = om.MBoundingBox() point = om.MPoint() for j in range( numV ): fnSurface.getCV( i, j, point ) bbox.expand( point ) minPoint = om.MVector( bbox.min() ) maxPoint = om.MVector( bbox.max() ) cPoint = om.MPoint(( minPoint + maxPoint )/2)*surfaceMatrix cvPoints.set( cPoint, i ) for i in range( numU+degreeU-1 ): if i < degreeU-1: knots[i] = 0 elif i-degreeU+1 > numSpansU: knots[i] = numSpansU else: knots[i] = i-degreeU+1 curveData = om.MFnNurbsCurveData() createCurveObj = curveData.create() fnCreateCurve = om.MFnNurbsCurve() fnCreateCurve.create( cvPoints, knots, degreeU, om.MFnNurbsCurve.kOpen, 0,0, createCurveObj ) fnCurve = om.MFnNurbsCurve( createCurveObj ) return fnCurve.length()
def selectDeltaPoints( targetMeshObj, targetIndex ): nodeName = bc.getBlendAndFixedShape(targetMeshObj) fnNode = om.MFnDependencyNode( fnc.getMObject( nodeName ) ) blendMeshInfosPlug = fnNode.findPlug( 'blendMeshInfos' ) elementNum = blendMeshInfosPlug.numElements() if targetIndex >= elementNum: return None deltasPlug = blendMeshInfosPlug[targetIndex].child( 1 ) vtxNames = [] for i in range( deltasPlug.numElements() ): loIndex = deltasPlug[i].logicalIndex() vtxNames.append( targetMeshObj+'.vtx[%d]' % loIndex ) cmds.select( vtxNames )
def setDeltaBySelected( targetMeshObj, targetIndex, deltaIndices ): if not deltaIndices: return None nodeName = bc.getBlendAndFixedShape(targetMeshObj) fnNode = om.MFnDependencyNode( fnc.getMObject( nodeName ) ) blendMeshInfosPlug = fnNode.findPlug( 'blendMeshInfos' ) elementNum = blendMeshInfosPlug.numElements() if targetIndex >= elementNum: return None deltasPlug = blendMeshInfosPlug[targetIndex].child( 1 ) cuDeltaIndices = [] for i in range( deltasPlug.numElements() ): cuDeltaIndices.append( deltasPlug[i].logicalIndex() ) for deltaIndex in cuDeltaIndices: if not deltaIndex in deltaIndices: cmds.removeMultiInstance( blendMeshInfosPlug[targetIndex].name()+'.deltas[%d]' % deltaIndex )
def addMirrorTarget( meshObj, index ): cmds.addMirrorBlendMeshInfos( meshObj, i=index, mi=0, rei=0 ) blendAndFixed = getBlendAndFixedShapeNode( meshObj ) if not blendAndFixed: return None fnBlendAndFixed = om.MFnDependencyNode( fnc.getMObject( blendAndFixed ) ) plugBlendMeshInfo = fnBlendAndFixed.findPlug( "blendMeshInfos" ) plugBlendMeshElementTarget = plugBlendMeshInfo[ plugBlendMeshInfo.numElements()-1 ] plugWeightsTarget = plugBlendMeshElementTarget.child( 2 ) plugNameTarget = plugBlendMeshElementTarget.child( 3 ) plugAnimCurveTarget = plugBlendMeshElementTarget.child( 5 ) plugAnimOutputTarget = plugBlendMeshElementTarget.child( 6 ) plugMeshInfoElement = plugBlendMeshInfo[index] plugTargetWeights = plugMeshInfoElement.child( 2 ) plugOrigName = plugMeshInfoElement.child( 3 ) plugAnimCurve = plugMeshInfoElement.child( 5 ) nameOrig = plugOrigName.asString() if nameOrig.find( '_L_' ) != -1: replacedName = nameOrig.replace( '_L_', '_R_' ) elif nameOrig.find( '_R_' ) != -1: replacedName = nameOrig.replace( '_R_', '_L_' ) else: replacedName = nameOrig+'_inv' plugNameTarget.setString( replacedName ) cons = cmds.listConnections( plugAnimCurve.name() ) if cons: duAnim = cmds.duplicate( cons[0] )[0] duAnim = cmds.rename( duAnim, replacedName+'_anim' ) cmds.connectAttr( duAnim+'.message', plugAnimCurveTarget.name() ) cmds.connectAttr( duAnim+'.output', plugAnimOutputTarget.name() ) numElements = plugTargetWeights.numElements() logicalIndices = [] weightValues = [] for i in range( numElements ): logicalIndices.append( plugTargetWeights[i].logicalIndex() ) weightValues.append( plugTargetWeights[i].asFloat() ) nodeName = fnBlendAndFixed.name() driverWeightsPlug = fnBlendAndFixed.findPlug( 'driverWeights' ) fnc.clearNoneConnectedElements( driverWeightsPlug ) connectIndex = driverWeightsPlug[ driverWeightsPlug.numElements()-1 ].logicalIndex() + 1 driverInfos = [] for i in range( numElements ): driverInfo = [None, 0,0,0 ] attrName = nodeName+'.driverWeights[%d]' % logicalIndices[i] cons = cmds.listConnections( attrName, s=1, d=0, p=1, c=1 ) if not cons: continue edited = False if cons[1].find( '_L_' ) != -1: invTarget = cons[1].replace( '_L_', '_R_' ) edited = True elif cons[1].find( '_R_' ) != -1: invTarget = cons[1].replace( '_R_', '_L_' ) edited = True else: invTarget = cons[1] invNode, attrName = invTarget.split( '.' ) driverInfo[0] = invNode driverInfoWeight = 0 if edited: driverInfoWeight = weightValues[i] else: driverInfoWeight = -weightValues[i] if attrName == 'outDriver0': driverInfo[1] = driverInfoWeight elif attrName == 'outDriver1': driverInfo[2] = driverInfoWeight elif attrName == 'outDriver2': driverInfo[3] = driverInfoWeight if not cmds.objExists( invNode ): return None cons = cmds.listConnections( invTarget, d=1, s=0, p=1, c=1, type='blendAndFixedShape' ) if not cons: cmds.connectAttr( invTarget, nodeName+'.driverWeights[%d]' % connectIndex ) cmds.setAttr( "%s[%d]" %( plugWeightsTarget.name(), connectIndex ), driverInfoWeight ) connectIndex += 1 else: print cons driverWeightIndex = int( cons[1].split( '[' )[1].replace( ']', '' ) ) cmds.setAttr( "%s[%d]" %( plugWeightsTarget.name(), driverWeightIndex ), driverInfoWeight ) driverInfos.append( driverInfo ) return replacedName, driverInfos, plugBlendMeshElementTarget.logicalIndex()
def createController( targetClass, shapeOption ): addCurve = False addPoly = False if shapeOption == 'Curve': addCurve = True elif shapeOption == 'Polygon': addPoly = True else: addCurve = True addPoly = True crvPoints = targetClass.pointList polyNode = targetClass.polygonNode options = targetClass.options jnt = cmds.createNode( 'transform', n='Put_CTL' ) if addCurve: crvObject = cmds.curve( p=crvPoints, d=1 ) crvShape = cmds.listRelatives( crvObject, s=1 )[0] cmds.parent( crvShape, jnt, add=1, shape=1 ) cmds.rename( crvShape, jnt+'Shape' ) cmds.delete( crvObject ) if addPoly: polyName = jnt+'Shape1' if polyNode: node = cmds.createNode( polyNode ) meshShape = cmds.createNode( 'mesh' ) meshObject = cmds.listRelatives( meshShape, p=1 )[0] cmds.connectAttr( node+'.output', meshShape+'.inMesh' ) if options: for attr, value in options: cmds.setAttr( node+'.'+attr, value ) meshShape = cmds.parent( meshShape, jnt, add=1, shape=1 ) cmds.delete( meshObject ) cmds.rename( meshShape, polyName ) else: vtxCount = targetClass.vtxCount polyCount = targetClass.polyCount PointArr = functions.pointListToPointArr( targetClass.polyPointList ) CountArr = functions.intListToIntArr( targetClass.polyCountList ) ConnectArr = functions.intListToIntArr( targetClass.polyConnectList ) fnMesh = om.MFnMesh() fnMesh.create( vtxCount, polyCount, PointArr, CountArr, ConnectArr, functions.getMObject( jnt ) ) cmds.rename( fnMesh.fullPathName(), polyName ) shader = functions.addCostomShader( polyName ) if not cmds.attributeQuery( 'transparency', node=jnt, ex=1 ): cmds.addAttr( jnt, ln='transparency', min=0, max=1, dv=0.5 ) cmds.setAttr( jnt+'.transparency', e=1, k=1 ) revNode = cmds.createNode( 'reverse' ) cmds.connectAttr( jnt+'.transparency', revNode+'.inputX' ) cmds.connectAttr( jnt+'.transparency', shader+'.outTransparencyR' ) cmds.connectAttr( jnt+'.transparency', shader+'.outTransparencyG' ) cmds.connectAttr( jnt+'.transparency', shader+'.outTransparencyB' ) return jnt
def getShapeInfo( targetMeshObj ): shapeInfoList = [] node = bc.getBlendAndFixedShape( targetMeshObj ) if not cmds.objExists( targetMeshObj ): return None, None fnNode = om.MFnDependencyNode( fnc.getMObject( node ) ) driverWeightPlug = fnNode.findPlug( "driverWeights" ) blendMeshPlug = fnNode.findPlug( "blendMeshInfos" ) for i in range( blendMeshPlug.numElements() ): shapeInfo = [None, None, blendMeshPlug[i].logicalIndex() ] inputMeshPlug = blendMeshPlug[i].child( 0 ) targetWPlug = blendMeshPlug[i].child( 2 ) namePlug = blendMeshPlug[i].child( 3 ) cons = om.MPlugArray() inputMeshPlug.connectedTo( cons, True, False ) if cons.length(): conNode = om.MFnDependencyNode( cons[0].node() ) shapeInfo[0] = cmds.listRelatives( conNode.name(), p=1 )[0].replace( '_inv', '' ) else: shapeInfo[0] = namePlug.asString() angleInfos = [] for i in range( targetWPlug.numElements() ): newAngleInfo = [None, 0, 0, 0] logicalIndex = targetWPlug[i].logicalIndex() targetDriverWPlug = driverWeightPlug.elementByLogicalIndex( logicalIndex ) cons = om.MPlugArray() targetDriverWPlug.connectedTo( cons, True, False ) if not cons.length(): continue angleNode, angleAttr = cons[0].name().split( '.' ) value = targetWPlug[i].asDouble() appendIndex = None if angleAttr == 'outDriver0': appendIndex = 1 elif angleAttr == 'outDriver1': appendIndex = 2 elif angleAttr == 'outDriver2': appendIndex = 3 newAngleInfo[0] = angleNode newAngleInfo[ appendIndex ] = value sameEx = False for angleInfo in angleInfos: if newAngleInfo[0] == angleInfo[0]: if newAngleInfo[1]: angleInfo[1] = newAngleInfo[1] if newAngleInfo[2]: angleInfo[2] = newAngleInfo[2] if newAngleInfo[3]: angleInfo[3] = newAngleInfo[3] sameEx = True if not sameEx: angleInfos.append( newAngleInfo ) shapeInfo[1] = angleInfos shapeInfoList.append( shapeInfo ) return shapeInfoList, fnNode.name()