def create_proxies(self, node, mesh_fn, skin_fn): components = om.MFnSingleIndexedComponent() try: skin_influences = skin_fn.influenceObjects() except: sys.stderr.write('Object has no influences') return self.reset_attr() num_faces = mesh_fn.numPolygons poly_iter = om.MItMeshPolygon(node) # iterates through all the selected mesh faces computation = om1.MComputation() computation.beginComputation(True, False) computation.setProgressRange(0, poly_iter.count()) computation.setProgress(0) progress_amount = 0 while not poly_iter.isDone(): # initialize average_weights as a zero list with the length of number of influences self.average_weights = [0] * len(skin_influences) face_verts = poly_iter.getVertices() vert_comp = components.create(om.MFn.kMeshVertComponent) components.addElements(face_verts) vert_iter = om.MItMeshVertex(node, vert_comp) # iterates through all the face vertices, computes the max influence per face while not vert_iter.isDone(): weights, inf_index = skin_fn.getWeights( node, vert_iter.currentItem()) self.average_face_weights(weights) vert_iter.next() self.set_face_weights(poly_iter.index(), self.average_weights, skin_influences) computation.setProgress(poly_iter.index()) poly_iter.next(None) # list holding the num faces, will be used to get the faces that need to be deleted faces_list = [i for i in range(num_faces)] computation.endComputation() for inf, faces in self.faces_weights.iteritems(): mesh_name = node.partialPathName() proxy_name = mesh_name.split('|')[-1] + '_' + inf + '_proxy' dg_mod = om.MDagModifier() dg_mod.commandToExecute('duplicate -n ' + proxy_name + ' ' + mesh_name) dg_mod.commandToExecute('parent -w ' + proxy_name) dg_mod.doIt() # duplicates the skinned mesh, named as above mesh_duplicate_dag = self.get_object_path(proxy_name) self.unlock_channels(mesh_duplicate_dag) mesh_duplicate_fn = om.MFnMesh(mesh_duplicate_dag) # faces is read from the self.faces_weights attribute, holds the indexes of the faces that will be kept faces_to_delete = list(set(faces_list) - set(faces)) if len(faces_to_delete) > 0: mesh_duplicate_fn.extractFaces(faces_to_delete) mesh_duplicate_fn.collapseFaces(faces_to_delete) mesh_duplicate_fn.updateSurface() # sets the proxy pivot based on the joint position / rotation self.set_proxy_pivot(mesh_duplicate_dag, inf) self.proxy_influence_pairs[mesh_duplicate_dag.partialPathName( )] = self.get_object_path(inf) self.proxies.append(proxy_name) self.animate_proxy() self.create_proxy_group(node)
if proxyGroupName != '': proxyGroupName += '_proxies_grp' else: proxyGroupName = 'proxies_grp' dgMod = om.MDagModifier() dgMod.commandToExecute('group -w -n ' + proxyGroupName +' ' + self.proxyGroups) dgMod.doIt() ======= self.reset_attr() num_faces = mesh_fn.numPolygons poly_iter = om.MItMeshPolygon(node) # iterates through all the selected mesh faces computation = om1.MComputation() computation.beginComputation(True, False) computation.setProgressRange(0, poly_iter.count()) computation.setProgress(0) progress_amount = 0 while not poly_iter.isDone(): # initialize average_weights as a zero list with the length of number of influences self.average_weights = [0] * len(skin_influences) face_verts = poly_iter.getVertices() vert_comp = components.create(om.MFn.kMeshVertComponent) components.addElements(face_verts) vert_iter = om.MItMeshVertex(node, vert_comp) # iterates through all the face vertices, computes the max influence per face while not vert_iter.isDone(): weights, inf_index = skin_fn.getWeights(node, vert_iter.currentItem()) self.average_face_weights(weights)
def NCCAPointBake(_fileName,_name,_startFrame,_endFrame) : # grab the selected object selected = OM.MSelectionList() obj=OM.MObject( ) selected.add(_name) selected.getDependNode(0,obj) # get the parent transform fn = OM.MFnTransform(obj) Mesh="" oChild = fn.child(0) # check to see if what we have is a mesh if(oChild.apiTypeStr()=="kMesh") : print "got Mesh" # get our mesh Mesh=OM.MFnMesh(oChild) else : print "Didn't get mesh ", oChild.apiType() return # now we try and open the file for writing try : file=open(str(_fileName[0]),'w') # if this fails catch the error and exit except IOError : print "Error opening file",str(_fileName) return # set the frame to start print "PB get anim control" currFrame=OM.MTime() anim=OMA.MAnimControl() # as these can take time to process we have an interupter to allow for the process to be # stopped interupter=OM.MComputation() # set the start of the heavy computation interupter.beginComputation() # now we set the tab level to 0 for the initial write to the file tabIndent=0 # now we get the mesh number of points numPoints = cmds.polyEvaluate( _name, v=True) # write the xml headers file.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n") file.write("<NCCAPointBake>\n") # up the tab level tabIndent=tabIndent+1 # write the initial header data WriteData(file,tabIndent,"<MeshName> %s </MeshName>" %(_name)) WriteData(file,tabIndent,"<NumVerts> %d </NumVerts>" %(numPoints)) WriteData(file,tabIndent,"<StartFrame> %s </StartFrame>" %(_startFrame)) WriteData(file,tabIndent,"<EndFrame> %s </EndFrame>" %(_endFrame)) WriteData(file,tabIndent,"<NumFrames> %s </NumFrames>" %(_endFrame-_startFrame)) WriteData(file,tabIndent,"<TranslateMode> %s </TranslateMode>" %("absolute")) # now for every frame write out the vertex data for frame in range(_startFrame,_endFrame) : print "Doing frame %04d" %(frame) # move to the correct frame currFrame.setValue (frame) anim.setCurrentTime(currFrame) # write out the frame tag WriteData(file,tabIndent,"<Frame number=\"%d\">" %(frame)) tabIndent=tabIndent+1 for vertex in range(0,numPoints) : # now the actual vertex data for the current mesh index value data = cmds.xform( (_name+ ".vtx["+str(vertex)+"]"), q=True, ws=True, t=True ) WriteData(file,tabIndent,"<Vertex number=\"%d\" attrib=\"translate\"> %f %f %f </Vertex>" %(vertex,data[0],data[1],data[2])) # now un-indent as we have ended the frame tabIndent=tabIndent-1 WriteData(file,tabIndent,"</Frame>") # if we have interupted exit and finish if interupter.isInterruptRequested() : file.write("</NCCAPointBake>\n") file.close() print "File export interrupted "; return # now finish file.write("</NCCAPointBake>\n") # and close the file file.close()
import maya.OpenMaya as OM # as these can take time to process we have an interupter to allow for the process to be # stopped interupter=OM.MComputation() # set the start of the heavy computation interupter.beginComputation() while True : if interupter.isInterruptRequested() : print "intrupted by escape" interupter.endComputation(); quit=True break for i in range(0,999): print '.',