如何在 Maya 中创建颜色集列表、访问它们和删除它们?

How create a list colorSets, access them and delete them in Maya?

我正在尝试列出 colorSet 名称,以控制我在给定网格上有多少个。我似乎无法将正确的变量传递给 cmds.ls 以使其识别 colorSet

我看了一圈,似乎大多数 cmds.ls 用于网格,但如果属性正确,它可以用来列出几乎任何东西

import maya.cmds as cmds

colorList = cmds.ls('colorSet*', sl=True, long=True)
objects = cmds.ls( sl=True, long=True)

if len(objects) > 0:
    if len(colorList) > 0:
        cmds.delete(colorList)

    result=cmds.polyColorSet(cr=True, colorSet='colorSet') 
    result=cmds.polyColorSet(cr=True, colorSet='colorSet')

代码最终忽略了我的 if 语句并继续无限期地创建颜色集。如何让我的代码在创建新代码之前删除旧代码?

您可以使用 cmds.listHistory 从对象获取所有输入,然后 cmds.ls 过滤该结果以找到任何颜色集:

import maya.cmds as cmds

for obj in cmds.ls(sl=True):  # Loop through the selection.
    history = cmds.listHistory(obj)  # Get a list of the object's history nodes, which may include a color set.
    existing_color_sets = cmds.ls(history, type="createColorSet")  # Filter history nodes to only color sets.
    if existing_color_sets:  # If a color set exists, delete it.
        cmds.delete(existing_color_sets)

    cmds.polyColorSet(obj, cr=True, colorSet="colorSet")  # Create a new color set.

您还应该能够使用

获得颜色集
cmds.polyColorSet( your_object_here, q=True, acs=True ) 

为了避免额外的 None 检查,我会尝试

def num_color_sets(obj):
    return len(cmds.polyColorSet( obj, q=True, acs=True ) or [])

即使实际的 colorSet 节点已被删除历史记录操作删除,这也应该有效