def loadClusters(clusterDicts): for clusterDict in clusterDicts: clusterHandleName = clusterDict.get('clusterHandle') clusterName = clusterDict.get('clusterName') pos = clusterDict.get('pos') dagMembers = clusterDict.get('dagMembers') selectionList = om.MSelectionList() for member in dagMembers: meshPath = GenAPI.getDagPath(member) verts = GenAPI.getMObjectAllVerts(member) selectionList.add(meshPath, verts) om.MGlobal.setActiveSelectionList(selectionList) #creating cluster cluster = cmds.cluster() clusterShape = cmds.listRelatives(cluster[1], type='shape') cmds.setAttr('%s.originX' % clusterShape[0], pos[0]) cmds.setAttr('%s.originY' % clusterShape[0], pos[1]) cmds.setAttr('%s.originZ' % clusterShape[0], pos[2]) cmds.setAttr('%s.rotatePivotX' % cluster[1], pos[0]) cmds.setAttr('%s.rotatePivotY' % cluster[1], pos[1]) cmds.setAttr('%s.rotatePivotZ' % cluster[1], pos[2]) cmds.setAttr('%s.scalePivotX' % cluster[1], pos[0]) cmds.setAttr('%s.scalePivotY' % cluster[1], pos[1]) cmds.setAttr('%s.scalePivotZ' % cluster[1], pos[2]) inc = 0 progressWin = UILib.ProgressWin() progressWin.itr = len(dagMembers) progressWin.setTitle('Loading Clusters') for member in dagMembers: deformerTool = WeightListTool(member, cluster[0]) weightList = clusterDict.get('weightList')[inc] membershipList = clusterDict.get('membershipList')[inc] deformerTool.setMembershipList(membershipList) deformerTool.setWeightList(weightList) inc += 1 progressWin.message = member progressWin.inc = inc progressWin.progress() cmds.rename(cluster[0], clusterName) cmds.rename(cluster[1], clusterHandleName) progressWin.end() om.MGlobal.displayInfo('Successfully loaded clusters.')
def setWeightList(self, weightList): ''' method for setting wieght list to mesh input python float list (weightList) ''' floatArray = om.MFloatArray() util = om.MScriptUtil() util.createFloatArrayFromList(weightList, floatArray) components = GenAPI.getMObjectAllVerts(self.meshPath) deformerWeightFn = oma.MFnWeightGeometryFilter(self.deformerObject) deformerWeightFn.setWeight(self.meshPath, components, floatArray)
def setWeightList(self,weightList): ''' method for setting wieght list to mesh input python float list (weightList) ''' floatArray = om.MFloatArray() util = om.MScriptUtil() util.createFloatArrayFromList(weightList,floatArray) components = GenAPI.getMObjectAllVerts(self.meshPath) deformerWeightFn = oma.MFnWeightGeometryFilter(self.deformerObject) deformerWeightFn.setWeight(self.meshPath,components,floatArray)
def reverseWeightList(self): '''method for reversing weighList on current mesh''' deformerWeightFn = oma.MFnWeightGeometryFilter(self.deformerObject) weightList = om.MFloatArray() verts = GenAPI.getMObjectAllVerts(self.mesh) deformerWeightFn.getWeights(self.meshPath, verts, weightList) reverseWeightList = [] for i in range(weightList.length()): reverseWeight = abs(weightList[i] - 1.00000) reverseWeightList.append(reverseWeight) return reverseWeightList
def setWeightList(self,influence,weightList): ''' set influence value ONLY WORKS FOR POLY MESH input MDagPath (influence) input python float list (weightList) ''' index = self.indexForInfluenceObject(influence) mIntArray = om.MIntArray() mIntArray.append(index) util = om.MScriptUtil() weights = om.MFloatArray() util.createFloatArrayFromList(weightList,weights) oldValues = om.MFloatArray() verts = GenAPI.getMObjectAllVerts(self.shape) self.skinclusterFn.setWeights(self.shapePath,verts,mIntArray,weights,True,oldValues)
def reverseWeightList(self): '''method for reversing weighList on current mesh''' deformerWeightFn = oma.MFnWeightGeometryFilter(self.deformerObject) weightList = om.MFloatArray() verts = GenAPI.getMObjectAllVerts(self.mesh) deformerWeightFn.getWeights(self.meshPath,verts,weightList) reverseWeightList = [] for i in range(weightList.length()): reverseWeight = abs(weightList[i] - 1.00000) reverseWeightList.append(reverseWeight) return reverseWeightList
def addDagMember(mesh, deformer): ''' method for adding dagObject to deformer input meshPath (python string) input defrormer(python string) ''' deformerObject = GenAPI.getMObject(deformer) meshPath = GenAPI.getDagPath(mesh) vertObjects = GenAPI.getMObjectAllVerts(mesh) geoFilter = oma.MFnGeometryFilter(deformerObject) deformerSet = geoFilter.deformerSet() deformerSetFn = om.MFnSet(deformerSet) for vert in vertObjects: deformerSetFn.addMember(meshPath, vert)
def addDagMember(mesh,deformer): ''' method for adding dagObject to deformer input meshPath (python string) input defrormer(python string) ''' deformerObject = GenAPI.getMObject(deformer) meshPath = GenAPI.getDagPath(mesh) vertObjects = GenAPI.getMObjectAllVerts(mesh) geoFilter = oma.MFnGeometryFilter(deformerObject) deformerSet = geoFilter.deformerSet() deformerSetFn = om.MFnSet(deformerSet) for vert in vertObjects: deformerSetFn.addMember(meshPath,vert)
def createClusterFromMesh(fromMesh,toMesh): ''' Function for creating cluster from another duplicate modified mesh input fromMesh (python string) input toMesh (python string) ''' toMeshPath = GenAPI.getDagPath(toMesh) fromVerts = GenAPI.getMObjectAllVerts(toMesh) selectionList = om.MSelectionList() selectionList.add(toMeshPath,fromVerts) om.MGlobal.setActiveSelectionList(selectionList) #creating cluster cluster = cmds.cluster() transformPosition = cmds.xform(fromMesh,q = True,ws = True, rp = True) clusterShape = cmds.listRelatives(cluster[1],type = 'shape') cmds.setAttr('%s.originX'%clusterShape[0],transformPosition[0]) cmds.setAttr('%s.originY'%clusterShape[0],transformPosition[1]) cmds.setAttr('%s.originZ'%clusterShape[0],transformPosition[2]) cmds.setAttr('%s.rotatePivotX'%cluster[1],transformPosition[0]) cmds.setAttr('%s.rotatePivotY'%cluster[1],transformPosition[1]) cmds.setAttr('%s.rotatePivotZ'%cluster[1],transformPosition[2]) cmds.setAttr('%s.scalePivotX'%cluster[1],transformPosition[0]) cmds.setAttr('%s.scalePivotY'%cluster[1],transformPosition[1]) cmds.setAttr('%s.scalePivotZ'%cluster[1],transformPosition[2]) weightList = MeasuringLib.MeasuringTool.createWeightListFromMesh(fromMesh, toMesh) deformerTool = WeightListTool(toMesh,cluster[0]) deformerTool.setWeightList(weightList) om.MGlobal.displayInfo('Created cluster from %s'%fromMesh)
def createClusterFromMesh(fromMesh, toMesh): ''' Function for creating cluster from another duplicate modified mesh input fromMesh (python string) input toMesh (python string) ''' toMeshPath = GenAPI.getDagPath(toMesh) fromVerts = GenAPI.getMObjectAllVerts(toMesh) selectionList = om.MSelectionList() selectionList.add(toMeshPath, fromVerts) om.MGlobal.setActiveSelectionList(selectionList) #creating cluster cluster = cmds.cluster() transformPosition = cmds.xform(fromMesh, q=True, ws=True, rp=True) clusterShape = cmds.listRelatives(cluster[1], type='shape') cmds.setAttr('%s.originX' % clusterShape[0], transformPosition[0]) cmds.setAttr('%s.originY' % clusterShape[0], transformPosition[1]) cmds.setAttr('%s.originZ' % clusterShape[0], transformPosition[2]) cmds.setAttr('%s.rotatePivotX' % cluster[1], transformPosition[0]) cmds.setAttr('%s.rotatePivotY' % cluster[1], transformPosition[1]) cmds.setAttr('%s.rotatePivotZ' % cluster[1], transformPosition[2]) cmds.setAttr('%s.scalePivotX' % cluster[1], transformPosition[0]) cmds.setAttr('%s.scalePivotY' % cluster[1], transformPosition[1]) cmds.setAttr('%s.scalePivotZ' % cluster[1], transformPosition[2]) weightList = MeasuringLib.MeasuringTool.createWeightListFromMesh( fromMesh, toMesh) deformerTool = WeightListTool(toMesh, cluster[0]) deformerTool.setWeightList(weightList) om.MGlobal.displayInfo('Created cluster from %s' % fromMesh)
def extrapToCluster(transform, meshes): '''function for extraping weights from a transform to a cluster''' vertSelection = om.MSelectionList() for mesh in meshes: #gathering verticies meshPath = GenAPI.getDagPath(mesh) meshObject = GenAPI.getMObject(mesh) shape = mesh if meshObject.apiTypeStr() == 'kTransform': shape = cmds.listRelatives(meshPath.fullPathName(), type='shape')[0] meshObject = GenAPI.getMObject(shape) verts = GenAPI.getMObjectAllVerts(mesh) vertSelection.add(meshPath, verts) om.MGlobal.setActiveSelectionList(vertSelection) #creating cluster cluster = cmds.cluster() transformPosition = cmds.xform(transform, q=True, ws=True, rp=True) clusterShape = cmds.listRelatives(cluster[1], type='shape') cmds.setAttr('%s.originX' % clusterShape[0], transformPosition[0]) cmds.setAttr('%s.originY' % clusterShape[0], transformPosition[1]) cmds.setAttr('%s.originZ' % clusterShape[0], transformPosition[2]) cmds.setAttr('%s.rotatePivotX' % cluster[1], transformPosition[0]) cmds.setAttr('%s.rotatePivotY' % cluster[1], transformPosition[1]) cmds.setAttr('%s.rotatePivotZ' % cluster[1], transformPosition[2]) cmds.setAttr('%s.scalePivotX' % cluster[1], transformPosition[0]) cmds.setAttr('%s.scalePivotY' % cluster[1], transformPosition[1]) cmds.setAttr('%s.scalePivotZ' % cluster[1], transformPosition[2]) #retrieving weight lists progressWin = UILib.ProgressWin() progressWin.setTitle('Extrap Cluster') progressWin.itr = len(meshes) inc = 0 for mesh in meshes: meshObject = GenAPI.getMObject(mesh) meshPath = GenAPI.getDagPath(mesh) shape = mesh weightList = [] if meshObject.apiTypeStr() == 'kTransform': shape = cmds.listRelatives(meshPath.fullPathName(), type='shape')[0] weightList = MeasuringLib.MeasuringTool.createWeigthListFromTransform( shape, transform) weightTool = WeightListTool(shape, cluster[0]) weightTool.setWeightList(weightList) progressWin.inc = inc progressWin.progress() inc += 1 progressWin.end() om.MGlobal.displayInfo('Extraped cluster from %s' % transform)
def mirrorCluster(transform, deformerName, prefix='L', oppPrefix='R', axis='x', direction='<', table=[-1, 1, 1]): vertSelection = om.MSelectionList() name = transform.split(prefix)[1] dagList = WeightListTool.sortDagMembers(deformerName, prefix, oppPrefix) if not len(dagList) == 0: singles = dagList[0] doubles = dagList[1] members = WeightListTool.getDagMembers(deformerName) for mesh in singles: meshPath = GenAPI.getDagPath(mesh) verts = GenAPI.getMObjectAllVerts(mesh) vertSelection.add(meshPath, verts) for meshGroup in doubles: for mesh in meshGroup: if not mesh in members: blankWeightList = WeightListTool.createBlankList( mesh, 0, True) weightTool = WeightListTool(mesh, deformerName) weightTool.setMembershipList(blankWeightList[1]) weightTool.setWeightList(blankWeightList[0]) meshPath = GenAPI.getDagPath(mesh) verts = GenAPI.getMObjectAllVerts(mesh) vertSelection.add(meshPath, verts) om.MGlobal.setActiveSelectionList(vertSelection) #creating cluster cluster = cmds.cluster() transformPosition = cmds.xform(transform, q=True, ws=True, rp=True) mirrorPosition = [ transformPosition[0] * table[0], transformPosition[1] * table[1], transformPosition[2] * table[2] ] clusterShape = cmds.listRelatives(cluster[1], type='shape') cmds.setAttr('%s.originX' % clusterShape[0], mirrorPosition[0]) cmds.setAttr('%s.originY' % clusterShape[0], mirrorPosition[1]) cmds.setAttr('%s.originZ' % clusterShape[0], mirrorPosition[2]) cmds.setAttr('%s.rotatePivotX' % cluster[1], mirrorPosition[0]) cmds.setAttr('%s.rotatePivotY' % cluster[1], mirrorPosition[1]) cmds.setAttr('%s.rotatePivotZ' % cluster[1], mirrorPosition[2]) cmds.setAttr('%s.scalePivotX' % cluster[1], mirrorPosition[0]) cmds.setAttr('%s.scalePivotY' % cluster[1], mirrorPosition[1]) cmds.setAttr('%s.scalePivotZ' % cluster[1], mirrorPosition[2]) for mesh in singles: weightTool = WeightListTool(mesh, deformerName) weightList = weightTool.mirrorWeightList(table=table, axis=axis, direction=direction) weightTool.__init__(mesh, cluster[0]) print weightList weightTool.setWeightList(weightList[0]) weightTool.setMembershipList(weightList[1]) for meshGroup in doubles: weightListA = WeightListTool.mirrorWeightListMultiMesh( meshGroup[0], meshGroup[1], deformerName, table) weightListB = WeightListTool.mirrorWeightListMultiMesh( meshGroup[1], meshGroup[0], deformerName, table) deformerTool = WeightListTool(meshGroup[1], cluster[0]) deformerTool.setMembershipList(weightListA[1]) deformerTool.setWeightList(weightListA[0]) deformerTool.__init__(meshGroup[0], cluster[0]) deformerTool.setMembershipList(weightListB[1]) deformerTool.setWeightList(weightListB[0]) try: cmds.rename(cluster[1], '%s%s' % (oppPrefix, name)) except: pass om.MGlobal.displayInfo('Created mirrored cluster from %s' % deformerName) else: om.MGlobal.displayError( 'Cannot mirror %s. Please make sure both sides are members.' % deformerName)
def mirrorCluster(transform,deformerName,prefix = 'L',oppPrefix = 'R', axis = 'x',direction = '<', table = [-1,1,1]): vertSelection = om.MSelectionList() dagList = WeightListTool.sortDagMembers(deformerName,prefix,oppPrefix) singles = dagList[0] doubles = dagList[1] members = WeightListTool.getDagMembers(deformerName) for mesh in singles: meshPath = GenAPI.getDagPath(mesh) verts = GenAPI.getMObjectAllVerts(mesh) vertSelection.add(meshPath,verts) for meshGroup in doubles: for mesh in meshGroup: if not mesh in members: blankWeightList = WeightListTool.createBlankList(mesh, 0, True) weightTool = WeightListTool(mesh,deformerName) weightTool.setMembershipList(blankWeightList[1]) weightTool.setWeightList(blankWeightList[0]) meshPath = GenAPI.getDagPath(mesh) verts = GenAPI.getMObjectAllVerts(mesh) vertSelection.add(meshPath,verts) om.MGlobal.setActiveSelectionList(vertSelection) #creating cluster cluster = cmds.cluster() transformPosition = cmds.xform(transform,q = True,ws = True, rp = True) mirrorPosition = [transformPosition[0] * table[0],transformPosition[1] * table[1],transformPosition[2] * table[2]] clusterShape = cmds.listRelatives(cluster[1],type = 'shape') cmds.setAttr('%s.originX'%clusterShape[0],mirrorPosition[0]) cmds.setAttr('%s.originY'%clusterShape[0],mirrorPosition[1]) cmds.setAttr('%s.originZ'%clusterShape[0],mirrorPosition[2]) cmds.setAttr('%s.rotatePivotX'%cluster[1],mirrorPosition[0]) cmds.setAttr('%s.rotatePivotY'%cluster[1],mirrorPosition[1]) cmds.setAttr('%s.rotatePivotZ'%cluster[1],mirrorPosition[2]) cmds.setAttr('%s.scalePivotX'%cluster[1],mirrorPosition[0]) cmds.setAttr('%s.scalePivotY'%cluster[1],mirrorPosition[1]) cmds.setAttr('%s.scalePivotZ'%cluster[1],mirrorPosition[2]) for mesh in singles: weightTool = WeightListTool(mesh,deformerName) weightList = weightTool.mirrorWeightList(table = table,axis = axis, direction = direction) weightTool.__init__(mesh,cluster[0]) print weightList weightTool.setWeightList(weightList[0]) weightTool.setMembershipList(weightList[1]) for meshGroup in doubles: weightListA = WeightListTool.mirrorWeightListMultiMesh(meshGroup[0], meshGroup[1], deformerName, table) weightListB = WeightListTool.mirrorWeightListMultiMesh(meshGroup[1], meshGroup[0], deformerName, table) deformerTool = WeightListTool(meshGroup[1],cluster[0]) deformerTool.setMembershipList(weightListA[1]) deformerTool.setWeightList(weightListA[0]) deformerTool.__init__(meshGroup[0],cluster[0]) deformerTool.setMembershipList(weightListB[1]) deformerTool.setWeightList(weightListB[0]) om.MGlobal.displayInfo('Created mirrored cluster from %s'%deformerName)
def extrapToCluster(transform,meshes): '''function for extraping weights from a transform to a cluster''' vertSelection = om.MSelectionList() for mesh in meshes: #gathering verticies meshPath = GenAPI.getDagPath(mesh) meshObject = GenAPI.getMObject(mesh) shape = mesh if meshObject.apiTypeStr() == 'kTransform': shape = cmds.listRelatives(meshPath.fullPathName(),type = 'shape')[0] meshObject = GenAPI.getMObject(shape) verts = GenAPI.getMObjectAllVerts(mesh) vertSelection.add(meshPath,verts) om.MGlobal.setActiveSelectionList(vertSelection) #creating cluster cluster = cmds.cluster() transformPosition = cmds.xform(transform,q = True,ws = True, rp = True) clusterShape = cmds.listRelatives(cluster[1],type = 'shape') cmds.setAttr('%s.originX'%clusterShape[0],transformPosition[0]) cmds.setAttr('%s.originY'%clusterShape[0],transformPosition[1]) cmds.setAttr('%s.originZ'%clusterShape[0],transformPosition[2]) cmds.setAttr('%s.rotatePivotX'%cluster[1],transformPosition[0]) cmds.setAttr('%s.rotatePivotY'%cluster[1],transformPosition[1]) cmds.setAttr('%s.rotatePivotZ'%cluster[1],transformPosition[2]) cmds.setAttr('%s.scalePivotX'%cluster[1],transformPosition[0]) cmds.setAttr('%s.scalePivotY'%cluster[1],transformPosition[1]) cmds.setAttr('%s.scalePivotZ'%cluster[1],transformPosition[2]) #retrieving weight lists progressWin = UILib.ProgressWin() progressWin.setTitle('Extrap Cluster') progressWin.itr = len(meshes) inc = 0 for mesh in meshes: meshObject = GenAPI.getMObject(mesh) meshPath = GenAPI.getDagPath(mesh) shape = mesh weightList = [] if meshObject.apiTypeStr() == 'kTransform': shape = cmds.listRelatives(meshPath.fullPathName(),type = 'shape')[0] weightList = MeasuringLib.MeasuringTool.createWeigthListFromTransform(shape,transform) weightTool = WeightListTool(shape,cluster[0]) weightTool.setWeightList(weightList) progressWin.inc = inc progressWin.progress() inc += 1 progressWin.end() om.MGlobal.displayInfo('Extraped cluster from %s'%transform)
def loadClusters(clusterDicts): for clusterDict in clusterDicts: clusterHandleName = clusterDict.get('clusterHandle') clusterName = clusterDict.get('clusterName') pos = clusterDict.get('pos') dagMembers = clusterDict.get('dagMembers') selectionList = om.MSelectionList() for member in dagMembers: try: meshPath = GenAPI.getDagPath(member) verts = GenAPI.getMObjectAllVerts(member) selectionList.add(meshPath,verts) except: pass om.MGlobal.setActiveSelectionList(selectionList) #creating cluster cluster = cmds.cluster() clusterShape = cmds.listRelatives(cluster[1],type = 'shape') cmds.setAttr('%s.originX'%clusterShape[0],pos[0]) cmds.setAttr('%s.originY'%clusterShape[0],pos[1]) cmds.setAttr('%s.originZ'%clusterShape[0],pos[2]) cmds.setAttr('%s.rotatePivotX'%cluster[1],pos[0]) cmds.setAttr('%s.rotatePivotY'%cluster[1],pos[1]) cmds.setAttr('%s.rotatePivotZ'%cluster[1],pos[2]) cmds.setAttr('%s.scalePivotX'%cluster[1],pos[0]) cmds.setAttr('%s.scalePivotY'%cluster[1],pos[1]) cmds.setAttr('%s.scalePivotZ'%cluster[1],pos[2]) inc = 0 progressWin = UILib.ProgressWin() progressWin.itr = len(dagMembers) progressWin.setTitle('Loading Clusters') for member in dagMembers: try: deformerTool = WeightListTool(member,cluster[0]) weightList = clusterDict.get('weightList')[inc] membershipList = clusterDict.get('membershipList')[inc] deformerTool.setMembershipList(membershipList) deformerTool.setWeightList(weightList) inc += 1 progressWin.message = member progressWin.inc = inc progressWin.progress() except: pass cmds.rename(cluster[0],clusterName) cmds.rename(cluster[1],clusterHandleName) progressWin.end() om.MGlobal.displayInfo('Successfully loaded clusters.')