scripted/pyConvertVerticesToFacesCmd.py
12 import maya.cmds
as cmds
13 import maya.api.OpenMaya
as om
17 The presence of this function tells Maya that the plugin produces, and
18 expects to be passed, objects created using the Maya Python API 2.0.
27 class convertVerticesToFacesCmd(om.MPxCommand):
28 s_name =
"convertVerticesToFaces"
31 om.MPxCommand.__init__(self)
32 self.previousSelectionList =
None
36 return convertVerticesToFacesCmd()
39 self.previousSelectionList = om.MGlobal.getActiveSelectionList()
43 multiVertexComponent =
None
44 singleVertexComponent =
None
45 finalFacesSelection = om.MSelectionList()
46 vertexComponentIter = om.MItSelectionList(self.previousSelectionList, om.MFn.kMeshVertComponent)
47 while not vertexComponentIter.isDone():
48 meshDagPath, multiVertexComponent = vertexComponentIter.getComponent()
49 meshName = meshDagPath.fullPathName();
50 if multiVertexComponent
is not None:
51 itMeshVertex = om.MItMeshVertex(meshDagPath, multiVertexComponent)
52 connectedFacesIndices = itMeshVertex.getConnectedFaces()
53 faceIter = om.MItMeshPolygon(meshDagPath)
55 for i
in connectedFacesIndices :
58 faceVerticesIndices = faceIter.getVertices()
59 faceIsContained =
True
60 for j
in faceVerticesIndices :
61 singleVertexList = om.MSelectionList()
62 singleVertexList.clear()
67 singleVertexList.add(vertexName)
68 meshDagPath, singleVertexComponent = singleVertexList.getComponent(0)
70 if not self.previousSelectionList.hasItem((meshDagPath, singleVertexComponent)):
71 faceIsContained =
False
79 finalFacesSelection.add(faceName)
80 vertexComponentIter.next()
84 om.MGlobal.setActiveSelectionList(finalFacesSelection, om.MGlobal.kReplaceList)
86 containedFacesArray = finalFacesSelection.getSelectionStrings()
87 om.MPxCommand.setResult(containedFacesArray)
93 om.MGlobal.setActiveSelectionList(self.previousSelectionList, om.MGlobal.kReplaceList)
101 def initializePlugin(obj):
102 plugin = om.MFnPlugin(obj,
"Autodesk",
"4.0",
"Any")
104 plugin.registerCommand(convertVerticesToFacesCmd.s_name, convertVerticesToFacesCmd.creator)
106 sys.stderr.write(
"Failed to register command\n")
109 def uninitializePlugin(obj):
110 plugin = om.MFnPlugin(obj)
112 plugin.deregisterCommand(convertVerticesToFacesCmd.s_name)
114 sys.stderr.write(
"Failed to deregister command\n")