Python API 2.0 Reference
python/api2/py2ConvertVerticesToFacesCmd.py
1 #-
2 # ==========================================================================
3 # Copyright 2015 Autodesk, Inc. All rights reserved.
4 #
5 # Use of this software is subject to the terms of the Autodesk
6 # license agreement provided at the time of installation or download,
7 # or which otherwise accompanies this software in either electronic
8 # or hard copy form.
9 # ==========================================================================
10 #+
11 
12 from builtins import next
13 import sys
14 import maya.cmds as cmds
15 import maya.api.OpenMaya as om
16 
17 def maya_useNewAPI():
18  """
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.
21  """
22  pass
23 
24 ##############################################################################
25 ##
26 ## Command class implementation
27 ##
28 ##############################################################################
29 class convertVerticesToFacesCmd(om.MPxCommand):
30  s_name = "py2ConvertVerticesToFaces"
31 
32  def __init__(self):
33  om.MPxCommand.__init__(self)
34  self.previousSelectionList = None
35 
36  @staticmethod
37  def creator():
38  return convertVerticesToFacesCmd()
39 
40  def doIt(self, args):
41  self.previousSelectionList = om.MGlobal.getActiveSelectionList()
42  self.redoIt()
43 
44  def redoIt(self):
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)
56 
57  for i in connectedFacesIndices :
58  #GET THE VERTEX INDICES FOR CURRENT FACE:
59  faceIter.setIndex(i);
60  faceVerticesIndices = faceIter.getVertices()
61  faceIsContained = True
62  for j in faceVerticesIndices :
63  singleVertexList = om.MSelectionList()
64  singleVertexList.clear()
65  vertexName = meshName
66  vertexName += ".vtx["
67  vertexName += str(j)
68  vertexName += "]"
69  singleVertexList.add(vertexName)
70  meshDagPath, singleVertexComponent = singleVertexList.getComponent(0)
71  ##SEE WHETHER VERTEX BELONGS TO ORIGINAL SELECTION, AND IF IT DOESN'T, THEN THE WHOLE FACE IS NOT CONTAINED:
72  if not self.previousSelectionList.hasItem((meshDagPath, singleVertexComponent)):
73  faceIsContained = False
74  break
75  ##IF FACE IS "CONTAINED", ADD IT TO THE FINAL CONTAINED FACES LIST:
76  if faceIsContained:
77  faceName = meshName
78  faceName += ".f["
79  faceName += str(i)
80  faceName += "]"
81  finalFacesSelection.add(faceName)
82  next(vertexComponentIter)
83 
84 
85  ## FINALLY, MAKE THE NEW "CONTAINED FACES", THE CURRENT SELECTION:
86  om.MGlobal.setActiveSelectionList(finalFacesSelection, om.MGlobal.kReplaceList)
87  ## RETURN NEW CONTAINED FACES LIST FROM THE MEL COMMAND, AS AN ARRAY OF STRINGS:
88  containedFacesArray = finalFacesSelection.getSelectionStrings()
89  om.MPxCommand.setResult(containedFacesArray)
90 
91  def isUndoable(self):
92  return True
93 
94  def undoIt(self):
95  om.MGlobal.setActiveSelectionList(self.previousSelectionList, om.MGlobal.kReplaceList)
96 
97 ##############################################################################
98 ##
99 ## The following routines are used to register/unregister
100 ## the command we are creating within Maya
101 ##
102 ##############################################################################
103 def initializePlugin(obj):
104  plugin = om.MFnPlugin(obj, "Autodesk", "4.0", "Any")
105  try:
106  plugin.registerCommand(convertVerticesToFacesCmd.s_name, convertVerticesToFacesCmd.creator)
107  except:
108  sys.stderr.write("Failed to register command\n")
109  raise
110 
111 def uninitializePlugin(obj):
112  plugin = om.MFnPlugin(obj)
113  try:
114  plugin.deregisterCommand(convertVerticesToFacesCmd.s_name)
115  except:
116  sys.stderr.write("Failed to deregister command\n")
117  raise
118