Skip to main content
 首页 » 编程设计

python之在maya python中选择子对象

2025年05月04日19zhoujg

我目前正在尝试为 DAG 节点的每个子对象赋予不同的颜色。当我在组件选择模式下选择每个子对象/元素(但不是全部)然后在其上运行脚本时,它会起作用。但是选择整个网格对我来说不起作用。我已经尝试过不同种类的 listRelatives,但都没有用。我可以进入对象并获取每个顶点,但它们没有按连通性分组。

    def assignDMat(self,*args): 
    #selection = self.selector() 
    selection = cmds.ls(selection=True) 
    for j in range(0, len(selection)): 
        cmds.polyColorPerVertex(selection[j], r=0,  g=0, b=0, cdo=True) 
        cmds.polyColorPerVertex(selection[j], r=random.uniform(0,1), cdo=True) 

新代码:

import maya.cmds as cmds 
import random 
 
selection = cmds.ls(selection=True) 
 
cmds.polySeparate(selection) 
 
 
for i in range(0,len(selection)): 
    obj=cmds.listRelatives(selection[i]) 
    print(len(obj)) 
    for j in range(0,len(obj)-1): 
        cmds.polyColorPerVertex(obj[j], r=0,  g=0, b=0, cdo=True) 
        cmds.polyColorPerVertex(obj[j], r=random.uniform(0,1), cdo=True) 
 
cmds.polyUnite(("polySurface*"), n="Result", ch=False) 

Intended Result

请您参考如下方法:

使用分离然后重新合并网格会导致很多意想不到的问题,有些您已经遇到过,所以最好避免这种情况。

相反,我们可以创建一个函数,通过返回一组面索引列表来返回一个对象的所有单独外壳。这可以通过 cmds.polySelect 将面索引传递给它来完成,然后它会选择整个外壳。由于现在选择了 shell,我们现在可以只收集新的选择并继续获取下一个 shell。然后,很容易循环遍历它们中的每一个并使用 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)