Python API 2.0 Reference: scripted/pyConvertVerticesToFacesCmd.py

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