0
votes

I am trying to list colorSet names in order to control how many of them I have on a given mesh. I can't seem to pass the right variable to the cmds.ls for it to recognize colorSet

I've read around and it seems like mostly cmds.ls is used for meshes but with correct attributes it can be used to list pretty much anything

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

The code ends up ignoring my if statement and continues to create colorSets indefinitely. How do I make my code delete old ones before creating new ones?

2
Just to be clear, you're trying to see if a given object has an existing colorSet, and if it does then delete it? ā€“ Green Cell
Exactly. Iā€™m trying to create a list that would store those color sets. Then if an object already has more than one simply clear out that list. ā€“ boomstick

2 Answers

3
votes

You can use cmds.listHistory to get all the inputs from an object, then cmds.ls to filter that result to find any color sets:

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.
2
votes

You should also be able to get the color sets with

cmds.polyColorSet( your_object_here, q=True, acs=True ) 

To avoid extra None checks i'd try

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

This should work even if the actual colorSet nodes have been deleted by a delete history operation