12 from builtins
import next
14 import maya.cmds
as cmds
15 import maya.api.OpenMaya
as om
19 The presence of this function tells Maya that the plugin produces, and
20 expects to be passed, objects created using the Maya Python API 2.0.
29 class convertVerticesToFacesCmd(om.MPxCommand):
30 s_name =
"py2ConvertVerticesToFaces"
33 om.MPxCommand.__init__(self)
34 self.previousSelectionList =
None
38 return convertVerticesToFacesCmd()
41 self.previousSelectionList = om.MGlobal.getActiveSelectionList()
45 multiVertexComponent =
None
46 singleVertexComponent =
None
47 finalFacesSelection = om.MSelectionList()
48 vertexComponentIter = om.MItSelectionList(self.previousSelectionList, om.MFn.kMeshVertComponent)
49 while not vertexComponentIter.isDone():
50 meshDagPath, multiVertexComponent = vertexComponentIter.getComponent()
51 meshName = meshDagPath.fullPathName();
52 if multiVertexComponent
is not None:
53 itMeshVertex = om.MItMeshVertex(meshDagPath, multiVertexComponent)
54 connectedFacesIndices = itMeshVertex.getConnectedFaces()
55 faceIter = om.MItMeshPolygon(meshDagPath)
57 for i
in connectedFacesIndices :
60 faceVerticesIndices = faceIter.getVertices()
61 faceIsContained =
True
62 for j
in faceVerticesIndices :
63 singleVertexList = om.MSelectionList()
64 singleVertexList.clear()
69 singleVertexList.add(vertexName)
70 meshDagPath, singleVertexComponent = singleVertexList.getComponent(0)
72 if not self.previousSelectionList.hasItem((meshDagPath, singleVertexComponent)):
73 faceIsContained =
False
81 finalFacesSelection.add(faceName)
82 next(vertexComponentIter)
86 om.MGlobal.setActiveSelectionList(finalFacesSelection, om.MGlobal.kReplaceList)
88 containedFacesArray = finalFacesSelection.getSelectionStrings()
89 om.MPxCommand.setResult(containedFacesArray)
95 om.MGlobal.setActiveSelectionList(self.previousSelectionList, om.MGlobal.kReplaceList)
103 def initializePlugin(obj):
104 plugin = om.MFnPlugin(obj,
"Autodesk",
"4.0",
"Any")
106 plugin.registerCommand(convertVerticesToFacesCmd.s_name, convertVerticesToFacesCmd.creator)
108 sys.stderr.write(
"Failed to register command\n")
111 def uninitializePlugin(obj):
112 plugin = om.MFnPlugin(obj)
114 plugin.deregisterCommand(convertVerticesToFacesCmd.s_name)
116 sys.stderr.write(
"Failed to deregister command\n")