【问题标题】:Select Subobjects in maya python在maya python中选择子对象
【发布时间】:2023-03-06 12:21:01
【问题描述】:

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

【问题讨论】:

    标签: python maya mel


    【解决方案1】:

    使用分离然后重新合并网格可能会导致许多意想不到的问题,其中一些您已经遇到过,因此最好避免这种情况。

    相反,我们可以创建一个函数,通过返回一组人脸索引列表来返回对象的所有单独外壳。这可以使用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)
    

    【讨论】:

    • 天哪,这就像一个魅力,正是我想要的。这看起来干净多了,速度也快了很多
    【解决方案2】:
    import random
    obj = cmds.ls(selection=True)
    obj_vtx = [cmds.ls(o + '.vtx[*]', fl=True) for o in obj]
    for obj in obj_vtx:
        color_id = random.uniform(0,1)
        for vtx in obj:
            cmds.polyColorPerVertex(vtx, r=0,  g=0, b=0, cdo=True)
            cmds.polyColorPerVertex(vtx, r=color_id, cdo=True)
    

    编辑:将颜色放入对象循环中

    【讨论】:

    • 遗憾的是,这并不是我想要的。它给每个顶点一个随机颜色,但我需要给每个立方体一个随机颜色。
    • 您可以将 random.uniform 放在一个 lopp 之前,这样每个几何图形都会有其随机颜色。我已经编辑了我的答案。你能举一个你正在尝试做的例子吗?
    • 如果不够清楚,请见谅。我需要以某种方式获取 Maya 内部网格的所有元素/子网格的列表,然后对每个网格应用随机颜色。简单的示例是拥有一个 TestBox 对象并在列表中获取 9 个框。当我使用 polySeperate 命令时,它会将网格分开,就像应该的那样,我可以给它们随机颜色。之后我可以使用 polyUnite 将它们重新合并在一起,但这似乎是错误的方法
    • 哦,我想我明白了,也许这篇文章可以帮助你:stackoverflow.com/questions/57236130/…
    • 听起来有点像,明天需要检查一下。 '到目前为止非常感谢
    【解决方案3】:

    目前有效的解决方案。它没有任何故障保险。例如,如果对象没有父对象,它可能会出错。就我而言,这是可行的。 非常感谢您的意见,DrWeeny。

    import maya.cmds as cmds
    import random
    
    par=cmds.listRelatives(cmds.ls(selection=True),p=True)
    selection = cmds.ls(selection=True)
    cmds.select(selection)
    
    pivotTranslate = cmds.xform (selection[0], q = True, ws = True, rotatePivot = True)
    
    
    
    
    print(piv)
    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=selection[0], ch=False)
    cmds.xform (selection[0], ws = True, pivots = pivotTranslate)
    cmds.select(par)
    cmds.parent(selection[0])
    

    【讨论】:

    • 这个错误对我来说是一个简单的 2 个球体组合示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    相关资源
    最近更新 更多