Python API 2.0 Reference: scripted/pyConvertVerticesToFacesCmd.py
12 from builtins
import next
13 import maya.cmds
as cmds
14 import maya.api.OpenMaya
as om
18 The presence of this function tells Maya that the plugin produces, and
19 expects to be passed, objects created using the Maya Python API 2.0.
28 class convertVerticesToFacesCmd(om.MPxCommand):
29 s_name =
"convertVerticesToFaces"
32 om.MPxCommand.__init__(self)
33 self.previousSelectionList =
None
37 return convertVerticesToFacesCmd()
40 self.previousSelectionList = om.MGlobal.getActiveSelectionList()
44 multiVertexComponent =
None
45 singleVertexComponent =
None
46 finalFacesSelection = om.MSelectionList()
47 vertexComponentIter = om.MItSelectionList(self.previousSelectionList, om.MFn.kMeshVertComponent)
48 while not vertexComponentIter.isDone():
49 meshDagPath, multiVertexComponent = vertexComponentIter.getComponent()
50 meshName = meshDagPath.fullPathName();
51 if multiVertexComponent
is not None:
52 itMeshVertex = om.MItMeshVertex(meshDagPath, multiVertexComponent)
53 connectedFacesIndices = itMeshVertex.getConnectedFaces()
54 faceIter = om.MItMeshPolygon(meshDagPath)
56 for i
in connectedFacesIndices :
59 faceVerticesIndices = faceIter.getVertices()
60 faceIsContained =
True
61 for j
in faceVerticesIndices :
62 singleVertexList = om.MSelectionList()
63 singleVertexList.clear()
68 singleVertexList.add(vertexName)
69 meshDagPath, singleVertexComponent = singleVertexList.getComponent(0)
71 if not self.previousSelectionList.hasItem((meshDagPath, singleVertexComponent)):
72 faceIsContained =
False
80 finalFacesSelection.add(faceName)
81 next(vertexComponentIter)
85 om.MGlobal.setActiveSelectionList(finalFacesSelection, om.MGlobal.kReplaceList)
87 containedFacesArray = finalFacesSelection.getSelectionStrings()
88 om.MPxCommand.setResult(containedFacesArray)
94 om.MGlobal.setActiveSelectionList(self.previousSelectionList, om.MGlobal.kReplaceList)
102 def initializePlugin(obj):
103 plugin = om.MFnPlugin(obj,
"Autodesk",
"4.0",
"Any")
105 plugin.registerCommand(convertVerticesToFacesCmd.s_name, convertVerticesToFacesCmd.creator)
107 sys.stderr.write(
"Failed to register command\n")
110 def uninitializePlugin(obj):
111 plugin = om.MFnPlugin(obj)
113 plugin.deregisterCommand(convertVerticesToFacesCmd.s_name)
115 sys.stderr.write(
"Failed to deregister command\n")