Maya Pythonでオブジェクト内のすべてのシェルを取得する



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)):

        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 =

    for obj in sel:

        faces = + ".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 =, 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))  # Restore selection.

    return shells

# Create a bunch of combined spheres then select it.
new_mesh = make_bunch_of_spheres()

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()


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():


# 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)




Next Post


木 8月 20 , 2020