def convertMFnDagNodes( nodeNames ): dagNodes = [] for nodeName in nodeNames: dagNode = om.MFnDagNode( sgModelDag.getDagPath( nodeName ) ) dagNodes.append( dagNode ) return dagNodes
def makeCenterCurveFromCurves(crvs, rebuildSpanRate=1.0): crvShapes = [] for crv in crvs: crvShape = sgModelDag.getShape(crv) if not crvShape: continue crvShapes.append(crvShape) lengthAll = 0.0 crvInfo = cmds.createNode('curveInfo') for crvShape in crvShapes: if not cmds.isConnected(crvShape + '.local', crvInfo + '.inputCurve'): cmds.connectAttr(crvShape + '.local', crvInfo + '.inputCurve', f=1) length = cmds.getAttr(crvInfo + '.arcLength') lengthAll += length cmds.delete(crvInfo) lengthAverage = lengthAll / len(crvShapes) rebuildSpans = int(lengthAverage * rebuildSpanRate) for crvShape in crvShapes: cmds.rebuildCurve(crvShape, constructionHistory=0, replaceOriginal=1, rebuildType=0, endKnots=1, keepRange=0, keepControlPoints=0, keepEndPoints=1, keepTangents=0, s=rebuildSpans, d=3, tol=0.01) fnNurbsCurve = om.MFnNurbsCurve(sgModelDag.getDagPath(crvShapes[0])) numCVs = fnNurbsCurve.numCVs() points = [] for i in range(numCVs): points.append([0, 0, 0]) curve = cmds.curve(p=points, d=3) for i in range(numCVs): sumPoints = om.MVector(0, 0, 0) for crvShape in crvShapes: sumPoints += om.MVector(*cmds.xform( crvShape + '.controlPoints[%d]' % i, q=1, ws=1, t=1)) averagePoints = sumPoints / len(crvShapes) cmds.move(averagePoints.x, averagePoints.y, averagePoints.z, curve + '.controlPoints[%d]' % i, os=1) return curve
def removePositiveNormalInfluence(selectedObjs): mesh, vtxIndices = sgModelMesh.getMeshAndIndicesPoints(selectedObjs) skinClusterNode = sgModelDag.getNodeFromHistory(mesh, 'skinCluster') if not skinClusterNode: return None skinClusterNode = skinClusterNode[0] influenceAndWeightList = sgModelSkinCluster.getInfluenceAndWeightList(mesh) fnMesh = sgModelMesh.getFnMesh(mesh) meshInvMatrix = sgModelDag.getDagPath(mesh).inclusiveMatrixInverse() meshPoints = sgModelMesh.getLocalPoints(mesh) plugMatrix = sgModelSkinCluster.getPlugMatrix(mesh) #normal = om.MVector() percentRate = 100.0 / len(vtxIndices) currentRate = 0.0 for i in vtxIndices: influenceList, childInfluenceList, weights = influenceAndWeightList[i] for j in range(len(influenceList)): jointMatrix = sgModelDg.getMatrixFromPlug( plugMatrix.elementByLogicalIndex(influenceList[j])) jointPointOnLocalMesh = om.MPoint(jointMatrix( 3, 0), jointMatrix(3, 1), jointMatrix(3, 2)) * meshInvMatrix vMeshPoint = om.MVector(meshPoints[i] - jointPointOnLocalMesh) interPoints = om.MPointArray() fnMesh.intersect(om.MPoint(jointPointOnLocalMesh), vMeshPoint, interPoints) lengthMeshPoint = vMeshPoint.length() * 0.99999 if not interPoints.length(): continue if om.MVector(interPoints[0] - jointPointOnLocalMesh).length() < lengthMeshPoint: weights[j] = 0 allWeights = 0.0 for weight in weights: allWeights += weight if allWeights == 0: continue for j in range(len(weights)): logicalIndex = influenceList[j] cmds.setAttr( skinClusterNode + '.weightList[%d].weights[%d]' % (i, logicalIndex), weights[j] / allWeights) currentRate += percentRate print "%6.2f" % currentRate + "% caculated"
def makeCenterCurveFromCurves( crvs, rebuildSpanRate= 1.0 ): crvShapes = [] for crv in crvs: crvShape = sgModelDag.getShape( crv ) if not crvShape: continue crvShapes.append( crvShape ) lengthAll = 0.0 crvInfo = cmds.createNode( 'curveInfo' ) for crvShape in crvShapes: if not cmds.isConnected( crvShape+'.local', crvInfo+'.inputCurve' ): cmds.connectAttr( crvShape+'.local', crvInfo+'.inputCurve', f=1 ) length = cmds.getAttr( crvInfo+'.arcLength' ) lengthAll += length cmds.delete( crvInfo ) lengthAverage = lengthAll / len( crvShapes ) rebuildSpans = int( lengthAverage * rebuildSpanRate ) for crvShape in crvShapes: cmds.rebuildCurve( crvShape, constructionHistory=0, replaceOriginal=1, rebuildType=0, endKnots=1, keepRange=0, keepControlPoints=0, keepEndPoints=1, keepTangents=0, s=rebuildSpans, d=3, tol=0.01 ) fnNurbsCurve = om.MFnNurbsCurve( sgModelDag.getDagPath( crvShapes[0] ) ) numCVs = fnNurbsCurve.numCVs() points = [] for i in range( numCVs ): points.append( [0,0,0] ) curve = cmds.curve( p=points, d=3 ) for i in range( numCVs ): sumPoints = om.MVector(0,0,0) for crvShape in crvShapes: sumPoints += om.MVector( *cmds.xform( crvShape+'.controlPoints[%d]' % i, q=1, ws=1, t=1 ) ) averagePoints = sumPoints / len( crvShapes ) cmds.move( averagePoints.x, averagePoints.y, averagePoints.z, curve+'.controlPoints[%d]' % i, os=1 ) return curve
def flip(self, meshName): points = om.MPointArray() fnMesh = om.MFnMesh(sgModelDag.getDagPath(meshName)) fnMesh.getPoints(points) for i in range(points.length()): mirrorIndex = self.mirrorIndices[i] targetPoint = points[mirrorIndex] cmds.move(-targetPoint.x, targetPoint.y, targetPoint.z, meshName + '.vtx[%d]' % i, os=1)
def setPivotToLowerPoint( target ): dagPath = sgModelDag.getDagPath( sgModelDag.getShape( target ) ) fnMesh = om.MFnMesh( dagPath ) points = om.MPointArray() fnMesh.getPoints( points ) lowerHeight = 1000000.0 lowerIndex = 0 for i in range( points.length() ): if lowerHeight > points[i].y: lowerHeight = points[i].y lowerIndex = i lowerPoint = points[ lowerIndex ]*dagPath.inclusiveMatrix() cmds.move( lowerPoint.x, lowerPoint.y, lowerPoint.z, target+'.rotatePivot', target+'.scalePivot' )
def removePositiveNormalInfluence( selectedObjs ): mesh, vtxIndices = sgModelMesh.getMeshAndIndicesPoints( selectedObjs ) skinClusterNode = sgModelDag.getNodeFromHistory( mesh, 'skinCluster' ) if not skinClusterNode: return None skinClusterNode = skinClusterNode[0] influenceAndWeightList = sgModelSkinCluster.getInfluenceAndWeightList( mesh ) fnMesh = sgModelMesh.getFnMesh( mesh ) meshInvMatrix = sgModelDag.getDagPath( mesh ).inclusiveMatrixInverse() meshPoints = sgModelMesh.getLocalPoints( mesh ) plugMatrix = sgModelSkinCluster.getPlugMatrix( mesh ) #normal = om.MVector() percentRate = 100.0/len( vtxIndices ) currentRate = 0.0 for i in vtxIndices: influenceList, childInfluenceList, weights = influenceAndWeightList[i] for j in range( len( influenceList ) ): jointMatrix = sgModelDg.getMatrixFromPlug( plugMatrix.elementByLogicalIndex( influenceList[j] ) ) jointPointOnLocalMesh = om.MPoint( jointMatrix(3,0), jointMatrix(3,1), jointMatrix(3,2) )*meshInvMatrix vMeshPoint = om.MVector( meshPoints[i]-jointPointOnLocalMesh ) interPoints = om.MPointArray() fnMesh.intersect( om.MPoint( jointPointOnLocalMesh ), vMeshPoint, interPoints ) lengthMeshPoint = vMeshPoint.length()*0.99999 if not interPoints.length(): continue if om.MVector( interPoints[0] - jointPointOnLocalMesh ).length() < lengthMeshPoint: weights[j] = 0 allWeights = 0.0 for weight in weights: allWeights += weight if allWeights == 0: continue for j in range( len( weights ) ): logicalIndex = influenceList[j] cmds.setAttr( skinClusterNode+'.weightList[%d].weights[%d]' %( i, logicalIndex ), weights[j]/allWeights ) currentRate += percentRate print "%6.2f" %currentRate + "% caculated"
def setPivotToLowerPoint(target): dagPath = sgModelDag.getDagPath(sgModelDag.getShape(target)) fnMesh = om.MFnMesh(dagPath) points = om.MPointArray() fnMesh.getPoints(points) lowerHeight = 1000000.0 lowerIndex = 0 for i in range(points.length()): if lowerHeight > points[i].y: lowerHeight = points[i].y lowerIndex = i lowerPoint = points[lowerIndex] * dagPath.inclusiveMatrix() cmds.move(lowerPoint.x, lowerPoint.y, lowerPoint.z, target + '.rotatePivot', target + '.scalePivot')
def getTangentAtParam( curve, paramValue, **options ): curveShape = sgModelDag.getShape( curve ) objectSpace = sgModelData.getValueFromDict( options, 'os' ) fnCurve= om.MFnNurbsCurve( sgModelDag.getDagPath( curveShape ) ) tangentValue = om.MVector() if objectSpace: tangentValue = fnCurve.tangent( paramValue ) else: worldMatrix = cmds.getAttr( curve+'.wm') worldMMatrix = sgModelConvert.convertMatrixToMMatrix( worldMatrix ) tangentValue = fnCurve.tangent( paramValue ) * worldMMatrix return tangentValue
def getPointAtParam( curve, paramValue, **options ): curveShape = sgModelDag.getShape( curve ) objectSpace = sgModelData.getValueFromDict( options, 'os' ) fnCurve= om.MFnNurbsCurve( sgModelDag.getDagPath( curveShape ) ) pointValue = om.MPoint() if objectSpace: fnCurve.getPointAtParam( paramValue, pointValue ) else: worldMatrix = cmds.getAttr( curve+'.wm') worldMMatrix = sgModelConvert.convertMatrixToMMatrix( worldMatrix ) fnCurve.getPointAtParam( paramValue, pointValue ) pointValue *= worldMMatrix return pointValue
def __init__(self, mesh ): import sgModelDag fnMesh = om.MFnMesh( sgModelDag.getDagPath( mesh ) ) numVtx = fnMesh.numVertices() numPoly = fnMesh.numPolygons() verticesPolygons = [] polygonsVertices = [] for i in range( numVtx ): verticesPolygons.append( om.MIntArray() ) for i in range( numPoly ): intArr = om.MIntArray() fnMesh.getPolygonVertices( i, intArr ) polygonsVertices.append( intArr ) for j in range( intArr.length() ): vtxIndex = intArr[j] faceIndices = verticesPolygons[vtxIndex] faceIndexExists = False for k in range( faceIndices.length() ): if i == faceIndices[k]: faceIndexExists = True break if not faceIndexExists: verticesPolygons[vtxIndex].append( i ) self.verticesPolygons = verticesPolygons self.polygonsVertices = polygonsVertices self.fnMesh = fnMesh self.verticesCheckList = [] for i in range( numVtx ): self.verticesCheckList.append( False ) self.numVtx = numVtx self.expendLoofVertices = []
def __init__(self, mesh): import sgModelDag fnMesh = om.MFnMesh(sgModelDag.getDagPath(mesh)) numVtx = fnMesh.numVertices() numPoly = fnMesh.numPolygons() verticesPolygons = [] polygonsVertices = [] for i in range(numVtx): verticesPolygons.append(om.MIntArray()) for i in range(numPoly): intArr = om.MIntArray() fnMesh.getPolygonVertices(i, intArr) polygonsVertices.append(intArr) for j in range(intArr.length()): vtxIndex = intArr[j] faceIndices = verticesPolygons[vtxIndex] faceIndexExists = False for k in range(faceIndices.length()): if i == faceIndices[k]: faceIndexExists = True break if not faceIndexExists: verticesPolygons[vtxIndex].append(i) self.verticesPolygons = verticesPolygons self.polygonsVertices = polygonsVertices self.fnMesh = fnMesh self.verticesCheckList = [] for i in range(numVtx): self.verticesCheckList.append(False) self.numVtx = numVtx self.expendLoofVertices = []
def moveObjectToClosestPoint( targets, base ): if not type( targets ) in [ type([]), type(()) ]: targets = [targets] dagPathBase = sgModelDag.getDagPath( sgModelDag.getShape( base ) ) baseMatrix = dagPathBase.inclusiveMatrix() baseInvMatrix = dagPathBase.inclusiveMatrixInverse() intersector = om.MMeshIntersector() intersector.create( sgModelDag.getMObject( sgModelDag.getShape( base ) ) ) pointOnMesh = om.MPointOnMesh() for target in targets: pivPoint = om.MPoint( *cmds.xform( target, q=1, ws=1, piv=1 )[:3] )*baseInvMatrix intersector.getClosestPoint( pivPoint, pointOnMesh ) point = pointOnMesh.getPoint() pointDiff = (om.MVector( point ) - om.MVector( pivPoint ))*baseMatrix cmds.move( pointDiff.x, pointDiff.y, pointDiff.z, target, ws=1, r=1 )
def MeshSetClosestPoint(baseName, targetName): baseName = sgModelDag.getShape(baseName) oBase = sgModelDg.getMObject(baseName) intersector = om.MMeshIntersector() fnMeshBase = om.MFnMesh(oBase) intersector.create(oBase) pointsBase = om.MPointArray() fnMeshBase.getPoints(pointsBase) dagPathTarget = sgModelDag.getDagPath(targetName) fnMeshTarget = om.MFnMesh(dagPathTarget) pointsTarget = om.MPointArray() fnMeshTarget.getPoints(pointsTarget) pointOnMesh = om.MPointOnMesh() indicesVtx = om.MIntArray() for i in range(pointsTarget.length()): intersector.getClosestPoint(pointsTarget[i], pointOnMesh) faceIndex = pointOnMesh.faceIndex() fnMeshBase.getPolygonVertices(faceIndex, indicesVtx) closeDist = 1000000.0 closeIndex = 0 for j in range(indicesVtx.length()): dist = pointsTarget[i].distanceTo(pointsBase[indicesVtx[j]]) if dist < closeDist: closeDist = dist closeIndex = indicesVtx[j] pointBase = pointsBase[closeIndex] cmds.move(pointBase.x, pointBase.y, pointBase.z, targetName + '.vtx[%d]' % i, os=1)
def moveObjectToClosestPoint(targets, base): if not type(targets) in [type([]), type(())]: targets = [targets] dagPathBase = sgModelDag.getDagPath(sgModelDag.getShape(base)) baseMatrix = dagPathBase.inclusiveMatrix() baseInvMatrix = dagPathBase.inclusiveMatrixInverse() intersector = om.MMeshIntersector() intersector.create(sgModelDag.getMObject(sgModelDag.getShape(base))) pointOnMesh = om.MPointOnMesh() for target in targets: pivPoint = om.MPoint( *cmds.xform(target, q=1, ws=1, piv=1)[:3]) * baseInvMatrix intersector.getClosestPoint(pivPoint, pointOnMesh) point = pointOnMesh.getPoint() pointDiff = (om.MVector(point) - om.MVector(pivPoint)) * baseMatrix cmds.move(pointDiff.x, pointDiff.y, pointDiff.z, target, ws=1, r=1)
def createRivetBasedOnSkinWeights(selectedObjs): import sgModelMesh import sgModelDag import sgModelSkinCluster import sgModelConvert def getJointMultMatrix(jnt, mtxBindPre): cons = cmds.listConnections(jnt + '.wm', type='multMatrix') if cons: for con in cons: if cmds.attributeQuery('skinWeightInfluenceMatrix', node=con, ex=1): if mtxBindPre == cmds.getAttr(con + '.i[0]'): return con mmtxNode = cmds.createNode('multMatrix') cmds.setAttr(mmtxNode + '.i[0]', mtxBindPre, type='matrix') cmds.connectAttr(jnt + '.wm', mmtxNode + '.i[1]') cmds.addAttr(mmtxNode, ln='skinWeightInfluenceMatrix', at='message') return mmtxNode mesh, vtxIndices = sgModelMesh.getMeshAndIndicesPoints(selectedObjs) skinClusterNode = sgModelDag.getNodeFromHistory(mesh, 'skinCluster') if not skinClusterNode: return None skinClusterNode = skinClusterNode[0] influenceAndWeightList, phygicalMap = sgModelSkinCluster.getInfluenceAndWeightList( mesh, vtxIndices) meshMatrix = sgModelDag.getDagPath(mesh).inclusiveMatrix() meshPoints = sgModelMesh.getLocalPoints(mesh) plugMatrix = sgModelSkinCluster.getPlugMatrix(mesh) plugBindPre = sgModelSkinCluster.getPlugBindPre(mesh) BB = om.MBoundingBox() wtAddMtx = cmds.createNode('wtAddMatrix') mtxPlugIndidcesAndWeights = {} allWeights = 0.0 for i in vtxIndices: influenceList, weights = influenceAndWeightList[phygicalMap[i]] for j in range(len(influenceList)): mtxPlugIndex = influenceList[j] if mtxPlugIndex in mtxPlugIndidcesAndWeights.keys(): mtxPlugIndidcesAndWeights[mtxPlugIndex] += weights[j] else: mtxPlugIndidcesAndWeights.update({mtxPlugIndex: weights[j]}) allWeights += weights[j] BB.expand(meshPoints[i]) worldPoint = BB.center() * meshMatrix items = mtxPlugIndidcesAndWeights.items() for i in range(len(items)): influence, weight = items[i] plugMatrixElement = plugMatrix.elementByLogicalIndex(influence) plugBindPreElement = plugBindPre.elementByLogicalIndex(influence) jnt = cmds.listConnections(plugMatrixElement.name(), s=1, d=0, type='joint')[0] mtxBindPre = cmds.getAttr(plugBindPreElement.name()) mmtxNode = getJointMultMatrix(jnt, mtxBindPre) cmds.connectAttr(mmtxNode + '.o', wtAddMtx + '.i[%d].m' % i) cmds.setAttr(wtAddMtx + '.i[%d].w' % i, weight / allWeights) origObj = cmds.createNode('transform', n='OrigObject') destObj = cmds.createNode('transform', n='destObject') cmds.setAttr(destObj + '.dh', 1) cmds.setAttr(destObj + '.dla', 1) mmNode = cmds.createNode('multMatrix') dcmp = cmds.createNode('decomposeMatrix') mtxWtAdd = cmds.getAttr(wtAddMtx + '.o') cmds.connectAttr(origObj + '.wm', mmNode + '.i[0]') cmds.connectAttr(wtAddMtx + '.o', mmNode + '.i[1]') cmds.connectAttr(destObj + '.pim', mmNode + '.i[2]') cmds.connectAttr(mmNode + '.o', dcmp + '.imat') cmds.connectAttr(dcmp + '.ot', destObj + '.t') cmds.connectAttr(dcmp + '.or', destObj + '.r') mmtxWtAdd = sgModelConvert.convertMatrixToMMatrix(mtxWtAdd) worldPoint *= mmtxWtAdd.inverse() cmds.setAttr(origObj + '.t', worldPoint.x, worldPoint.y, worldPoint.z)
def getFnMesh(mesh): mesh = sgModelDag.getShape(mesh) return om.MFnMesh(sgModelDag.getDagPath(mesh))
def getFnMesh( mesh ): mesh = sgModelDag.getShape( mesh ) return om.MFnMesh( sgModelDag.getDagPath( mesh ) )
def createRivetBasedOnSkinWeights( selectedObjs ): import sgModelMesh import sgModelDag import sgModelSkinCluster import sgModelConvert def getJointMultMatrix( jnt, mtxBindPre ): cons = cmds.listConnections( jnt+'.wm', type='multMatrix' ) if cons: for con in cons: if cmds.attributeQuery( 'skinWeightInfluenceMatrix', node=con, ex=1 ): if mtxBindPre == cmds.getAttr( con+'.i[0]' ): return con mmtxNode = cmds.createNode( 'multMatrix' ) cmds.setAttr( mmtxNode+'.i[0]', mtxBindPre, type='matrix' ) cmds.connectAttr( jnt+'.wm', mmtxNode+'.i[1]' ) cmds.addAttr( mmtxNode, ln='skinWeightInfluenceMatrix', at='message' ) return mmtxNode mesh, vtxIndices = sgModelMesh.getMeshAndIndicesPoints( selectedObjs ) skinClusterNode = sgModelDag.getNodeFromHistory( mesh, 'skinCluster' ) if not skinClusterNode: return None skinClusterNode = skinClusterNode[0] influenceAndWeightList, phygicalMap = sgModelSkinCluster.getInfluenceAndWeightList( mesh, vtxIndices ) meshMatrix = sgModelDag.getDagPath( mesh ).inclusiveMatrix() meshPoints = sgModelMesh.getLocalPoints( mesh ) plugMatrix = sgModelSkinCluster.getPlugMatrix( mesh ) plugBindPre = sgModelSkinCluster.getPlugBindPre( mesh ) BB = om.MBoundingBox() wtAddMtx = cmds.createNode( 'wtAddMatrix' ) mtxPlugIndidcesAndWeights = {} allWeights = 0.0 for i in vtxIndices: influenceList, weights = influenceAndWeightList[ phygicalMap[i] ] for j in range( len( influenceList ) ): mtxPlugIndex = influenceList[j] if mtxPlugIndex in mtxPlugIndidcesAndWeights.keys(): mtxPlugIndidcesAndWeights[mtxPlugIndex] += weights[j] else: mtxPlugIndidcesAndWeights.update( {mtxPlugIndex:weights[j]} ) allWeights += weights[j] BB.expand( meshPoints[i] ) worldPoint = BB.center()*meshMatrix items = mtxPlugIndidcesAndWeights.items() for i in range( len( items ) ): influence, weight = items[i] plugMatrixElement = plugMatrix.elementByLogicalIndex( influence ) plugBindPreElement = plugBindPre.elementByLogicalIndex( influence ) jnt = cmds.listConnections( plugMatrixElement.name(), s=1, d=0, type='joint' )[0] mtxBindPre = cmds.getAttr( plugBindPreElement.name() ) mmtxNode = getJointMultMatrix( jnt, mtxBindPre ) cmds.connectAttr( mmtxNode+'.o', wtAddMtx+'.i[%d].m' % i ) cmds.setAttr( wtAddMtx+'.i[%d].w' % i, weight/allWeights ) origObj = cmds.createNode( 'transform', n='OrigObject' ) destObj = cmds.createNode( 'transform', n='destObject' ) cmds.setAttr( destObj+'.dh' , 1 ) cmds.setAttr( destObj+'.dla', 1 ) mmNode = cmds.createNode( 'multMatrix' ) dcmp = cmds.createNode( 'decomposeMatrix' ) mtxWtAdd = cmds.getAttr( wtAddMtx+'.o' ) cmds.connectAttr( origObj+'.wm', mmNode+'.i[0]' ) cmds.connectAttr( wtAddMtx+'.o', mmNode+'.i[1]' ) cmds.connectAttr( destObj+'.pim', mmNode+'.i[2]' ) cmds.connectAttr( mmNode+'.o', dcmp+'.imat' ) cmds.connectAttr( dcmp+'.ot', destObj+'.t' ) cmds.connectAttr( dcmp+'.or', destObj+'.r' ) mmtxWtAdd = sgModelConvert.convertMatrixToMMatrix( mtxWtAdd ) worldPoint *= mmtxWtAdd.inverse() cmds.setAttr( origObj+'.t', worldPoint.x, worldPoint.y, worldPoint.z )