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