https://stackoverflow.com/questions/59034834/select-subobjects-in-maya-python
フェイスインデックスの一連のリストを返すことにより、オブジェクトのすべての個別のシェルを返す関数を作成できます。これはcmds.polySelect、フェースインデックスを渡すことで実行でき、シェル全体が選択されます。シェルが選択されたので、新しい選択を収集して次のシェルを取得することができます。そうすれば、それぞれをループしてで色を付けるのは簡単cmds.polyColorPerVertexです。
以下は、ランダムな量のポリ球を作成し、それらをすべて組み合わせて、各シェルにランダムな色を設定する例です。
import random
import maya.cmds as cmds
# Just makes a bunch of random spheres, combines them, then returns the new mesh.def make_bunch_of_spheres():
meshes = []
for i in range(random.randint(20, 50)):
meshes.append(cmds.polySphere()[0])
cmds.move(random.randint(-20, 20), random.randint(-20, 20), random.randint(-20, 20), meshes[-1])
return cmds.polyUnite(meshes, constructionHistory=False)[0]
def get_shell_faces():
shells = [] # We'll be putting in lists of face indexes in here later. Each sub-list will represent a separate shell.
sel = cmds.ls(sl=True)
for obj in sel:
faces = cmds.ls(obj + ".f[*]", flatten=True) # Get all face indexes from the object.
for face in faces:
index = int(face.split("[")[1].rstrip("]")) # Extract the faces index number.
cmds.polySelect(obj, extendToShell=index) # Use the face's index to select the whole shell.
new_faces = cmds.ls(sl=True, flatten=True) # Now that the shell is selected, capture its faces.
shells.append(new_faces) # Append the face's as a new shell.
# Remove indexes from the new shell from this current loop, to optimize, and to avoid adding duplicate shells.
for new_face in new_faces:
if new_face in faces:
faces.pop(faces.index(new_face))
cmds.select(sel) # Restore selection.
return shells
# Create a bunch of combined spheres then select it.
new_mesh = make_bunch_of_spheres()
cmds.select(new_mesh)
shells = get_shell_faces() # Get shells from selection.
# Color each shell!for shell in shells:
cmds.polyColorPerVertex(shell, r=random.random(), g=random.random(), b=random.random(), cdo=True)
Extend polygons selection to shell
import maya.OpenMaya as om
import maya.cmds as cmds
def expandSelection():
sel = om.MSelectionList()
om.MGlobal.getActiveSelectionList(sel)
dag = om.MDagPath()
obj = om.MObject()
# get the active object (dag) and its selected components (obj)
sel.getDagPath(0, dag, obj)
# Here we create an iterator over the selected components
itr = om.MItMeshPolygon(dag, obj)
# Now we add all faces of the selection to the array
currfaces = om.MIntArray()
while not itr.isDone():
currfaces.append(itr.index())
itr.next()
# finally we reconvert the MIntArray to a simple list
currfaces = list(currfaces)
# and use maya.cmds to extend the polygon-selection to their shells
cmds.polySelect(dag.fullPathName(), extendToShell = currfaces, add=True)
expandSelection()