python - 在maya python中选择子对象

标签 python maya mel

我目前正在尝试为 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)

Example

关于python - 在maya python中选择子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59034834/

相关文章:

python - 无法在 python 中导入 zmq(安装问题)

python - 为 DataFrames 创建我自己的方法(python)

windows - 限制 Maya 在 Windows 上可以打开的文件数量

linux - 是否有类似于 Quartz Composer 的应用程序,但适用于任何通用类型的数据?

python - Maya 中 MEL/Python 中使用的 AttrFieldSliderGrp 命令

Python3 将数组从 (x,y,3) 减少到 (x,y,1) (RGB 到灰度)

python - 使用 Python 向 Apple Notification Service 发送消息

python - 使用 python 脚本在 Maya 中创建 UI

python - 在带有动画子对象的父对象上卡住缩放变换(MAYA MEL/Python 脚本)

python - 如何使用 Python 或 Mel 正确抑制/静音 Maya 脚本编辑器警告?