Selection script for maya

Selection script for maya

jopook
Participant Participant
4,827 Views
19 Replies
Message 1 of 20

Selection script for maya

jopook
Participant
Participant

Hello, I'm noob in script but good in animation, I need some help to create a script selection.

I found an exemple : 

import maya.cmds as cmds
# Get selected objects
curSel = maya.cmds.ls(sl=True)
# Or, you can also specify a type in the listRelatives command
nurbsNodes = maya.cmds.listRelatives(curSel, allDescendents=True, noIntermediate=True, fullPath=True, type="nurbsCurve", path=True)
cmds.select(nurbsNodes)

But It dosn't select all the character controlers...

I would like If I select a character controler curve or locator and I run the script, the result is all controls who can be keyed should be selected. Without the referenced character name. 

Thanks a lot for the one who can help 😄

0 Likes
Accepted solutions (1)
4,828 Views
19 Replies
Replies (19)
Message 2 of 20

jmreinhart
Advisor
Advisor

So since your using locators and curves as controls you'll need to modify that code a bit. I don't know how you name your controls so I'll explain the code so you can hopefully figure out how you need to modify it.

 

import maya.cmds as cmds
# Get selected objects
sel = cmds.ls(sl=True)

# Split the name of the selected object 
# to get the name of the reference
refPrefix = sel.split(':')[0]

# List all the objects that fit the naming of 
# <yourReferencePrefix>:<anything>_CTL
# This is the part you will need to modify depending on how you named your 
# controls.
allMyControls = cmds.ls('{}:*_CTL'.format(refPrefix), type = 'transform')

# Select the controls
cmds.select(allMyControls)

 

 

0 Likes
Message 3 of 20

jopook
Participant
Participant

Hey, thanks a lot, but I got this error message :

 

# Select the controls
cmds.select(allMyControls)
# Error: AttributeError: file <maya console> line 7: 'list' object has no attribute 'split' #

 

 

0 Likes
Message 4 of 20

Kahylan
Advisor
Advisor

Hi!

 

I'm not sure that this will give you the desired outcome, since having both locators and nurbsCurves as controlls is quite messy and you'll probably select some objects in the rig that aren't controls.

Unless there are really strict naming conventions or a tagging system (or all the nurbsCurves and Locators without exception are controls), this isn't really doable...

import maya.cmds as mc

def selectAllControllers():
    """ selects all the Nurbscurves and Locators with the same namespace as the current selection if any of the transform attributes are keyable"""
    
    transforms = ['visibility', 'translateX', 'translateY', 'translateZ', 'rotateX', 'rotateY', 'rotateZ', 'scaleX', 'scaleY', 'scaleZ']
    selectObjs = []
    #get current selection
    sel = mc.ls(sl = True, l = True)
    for s in sel:
        #get prefix
        characterName = s.split(":")[0]
        #get all objects that contain the prefix
        
        objs = mc.ls("{0}:*".format(characterName), dag = True, lf = True)

        for o in objs:
            
            #check if object is either nurbsCurve or locator
            if mc.objectType(o) == "nurbsCurve" or mc.objectType(o) == "locator":
                #get parent transform and check for keyable transform attributes
                par = mc.listRelatives(o, p = True, f = True)[0]
                keyables = mc.listAttr(par, k= True)
                if any(attr in transforms for attr in keyables):
                    #if there is a keyable transform attribute, add to the list of objects to be added to selection
                    selectObjs.append(par)
                
    mc.select(selectObjs, add = True)
              
selectAllControllers()   

 

Now I also should point out that there is a list of Attributes which are checked for keyability. This list can be changed to include more or less attributes, and if one of the attributes in the list is keyable, the curve will be selected.

 

I hope it helps!

Message 5 of 20

jopook
Participant
Participant

no error message but nothing is selected XD 

0 Likes
Message 6 of 20

Kahylan
Advisor
Advisor

Could you run this:

import maya.cmds as mc

def selectAllControllers():
    """ selects all the Nurbscurves or locators with the same namespace as selection if any of the transform attributes are keyable"""
    
    transforms = ['visibility', 'translateX', 'translateY', 'translateZ', 'rotateX', 'rotateY', 'rotateZ', 'scaleX', 'scaleY', 'scaleZ']
    selectObjs = []
    #get current selection
    sel = mc.ls(sl = True, l = True)
    print("selection = {0}".format(sel))
    for s in sel:
        #get prefix
        characterName = s.split(":")[0]
        print("characterName = {0}".format(characterName))
        #get all objects that contain the prefix
        
        objs = mc.ls("{0}:*".format(characterName), dag = True, lf = True)

        for o in objs:
            
            #check if object is either nurbsCurve or locator
            if mc.objectType(o) == "nurbsCurve" or mc.objectType(o) == "locator":
                #get parent transform and check for keyable transform attributes
                par = mc.listRelatives(o, p = True, f = True)[0]
                keyables = mc.listAttr(par, k= True)
                if any(attr in transforms for attr in keyables):
                    #if there is a keyable transform attribute, add to the list of objects to be added to selection
                    selectObjs.append(par)
    print("selectObjs = {0}".format(selectObjs))           
    mc.select(selectObjs, add = True)
              
selectAllControllers()   

And then Copy and Paste whatever shows up in the scriptEditor?

Message 7 of 20

jopook
Participant
Participant

I have this error :

 

# Error: TypeError: file <maya console> line 26: 'NoneType' object is not iterable #

Message 8 of 20

Kahylan
Advisor
Advisor

This Error normally shows up when you try to iterate from something that is None.So in the code I posted, this could happen in Lines 11,13,19,24 and 26. All of them should have atleast 1 print statement printed before. Did the program print out nothing else?

Message 9 of 20

jopook
Participant
Participant

I changed this line :  characterName = s.split(":*_Ctrl")[0]

Script editor print all the controlers but the selections not done :

 

selectAllControllers()
selection = ['|VillagerHomme5:ControlRig|VillagerHomme5:MAIN_Offset_Ctrl_RestPoseGrp|VillagerHomme5:MAIN_Offset_Ctrl|VillagerHomme5:MAIN_Ctrl_RestPoseGrp|VillagerHomme5:MAIN_Ctrl|VillagerHomme5:MAIN_Inner_Ctrl_RestPoseGrp|VillagerHomme5:MAIN_Inner_Ctrl|VillagerHomme5:ROOTGrp|VillagerHomme5:ROOTParent_CtrlGrp2|VillagerHomme5:ROOT_CtrlGrp|VillagerHomme5:ROOT_Ctrl_RestPoseGrp|VillagerHomme5:ROOT_Ctrl']
characterName = |VillagerHomme5:ControlRig|VillagerHomme5:MAIN_Offset_Ctrl_RestPoseGrp|VillagerHomme5:MAIN_Offset_Ctrl|VillagerHomme5:MAIN_Ctrl_RestPoseGrp|VillagerHomme5:MAIN_Ctrl|VillagerHomme5:MAIN_Inner_Ctrl_RestPoseGrp|VillagerHomme5:MAIN_Inner_Ctrl|VillagerHomme5:ROOTGrp|VillagerHomme5:ROOTParent_CtrlGrp2|VillagerHomme5:ROOT_CtrlGrp|VillagerHomme5:ROOT_Ctrl_RestPoseGrp|VillagerHomme5:ROOT_Ctrl
selectObjs = []

0 Likes
Message 10 of 20

Kahylan
Advisor
Advisor

Ok, that explains it. Charactername is only the prefix that you are looking for, this set of characters will not really work with the split command treats the asteriks (*) as just a symbol. So it tries to split at ":*_CTRL", which obviously doesn't appear in any of your selections, since the asteriks is forbidden in names in Maya. Therefore it just takes the whole string as prefix. Instead if all your controls end with "_Ctrl", you should change line 17:

objs = mc.ls("{0}:*_Ctrl".format(characterName), dag = True, lf = True)

If it still does nothing, let me know what was printed

 

I hope it helps!

0 Likes
Message 11 of 20

jopook
Participant
Participant

This is the print , I think maya select only the hierarchy of the selected control, that mean If I select only one control, it will print me only for the current selection but not from the whole character rig. Actualy nothing is selected, only printed :

 

selection = ['|FouleFemme06:ControlRig|FouleFemme06:MAIN_Offset_Ctrl_RestPoseGrp|FouleFemme06:MAIN_Offset_Ctrl|FouleFemme06:MAIN_Ctrl_RestPoseGrp|FouleFemme06:MAIN_Ctrl|FouleFemme06:MAIN_Inner_Ctrl_RestPoseGrp|FouleFemme06:MAIN_Inner_Ctrl|FouleFemme06:ROOTGrp|FouleFemme06:ROOTParent_CtrlGrp2|FouleFemme06:ROOT_CtrlGrp|FouleFemme06:ROOT_Ctrl_RestPoseGrp|FouleFemme06:ROOT_Ctrl']
characterName = |FouleFemme06:ControlRig|FouleFemme06:MAIN_Offset_Ctrl_RestPoseGrp|FouleFemme06:MAIN_Offset_Ctrl|FouleFemme06:MAIN_Ctrl_RestPoseGrp|FouleFemme06:MAIN_Ctrl|FouleFemme06:MAIN_Inner_Ctrl_RestPoseGrp|FouleFemme06:MAIN_Inner_Ctrl|FouleFemme06:ROOTGrp|FouleFemme06:ROOTParent_CtrlGrp2|FouleFemme06:ROOT_CtrlGrp|FouleFemme06:ROOT_Ctrl_RestPoseGrp|FouleFemme06:ROOT_Ctrl
selectObjs = []

0 Likes
Message 12 of 20

Kahylan
Advisor
Advisor

Did you change line 13 back

characterName = s.split(":")[0]

From the print outs its definitely this line that doesn't do what it should.

0 Likes
Message 13 of 20

jopook
Participant
Participant

Yes I did it,

some "group" node is printed but It's not nurbsCurve XD

still this : 

 

selectAllControllers()
selection = ['|FouleFemme07:ControlRig|FouleFemme07:MAIN_Offset_Ctrl_RestPoseGrp|FouleFemme07:MAIN_Offset_Ctrl|FouleFemme07:MAIN_Ctrl_RestPoseGrp|FouleFemme07:MAIN_Ctrl|FouleFemme07:MAIN_Inner_Ctrl_RestPoseGrp|FouleFemme07:MAIN_Inner_Ctrl|FouleFemme07:r_LegGrp|FouleFemme07:r_Leg_ScaleGrp|FouleFemme07:r_Leg_FootIk_CtrlGrp|FouleFemme07:r_Leg_FootIk_Ctrl_RestPoseGrp|FouleFemme07:r_Leg_FootIk_Ctrl']
characterName = |FouleFemme07
selectObjs = []

0 Likes
Message 14 of 20

Kahylan
Advisor
Advisor

That is actually correct, since this isn't a group node, its just the namespace that your control is in.

I adjusted some print statements so I can tell what is going on, my guess it that the ls command fails to do what it should. The ls command had some major changes from maya 2019 to 2020, what version are you on?

 

import maya.cmds as mc

def selectAllControllers():
    """ selects all the Nurbscurves or locators with the same namespace as selection if any of the transform attributes are keyable"""
    
    transforms = ['visibility', 'translateX', 'translateY', 'translateZ', 'rotateX', 'rotateY', 'rotateZ', 'scaleX', 'scaleY', 'scaleZ']
    selectObjs = []
    #get current selection
    sel = mc.ls(sl = True, l = True)
    print("selection = {0}".format(sel))
    for s in sel:
        #get prefix
        characterName = s.split(":")[0]
        #get all objects that contain the prefix
        
        objs = mc.ls("{0}:*_Ctrl".format(characterName), dag = True, lf = True)
        print("objs = {0}".format(objs))

        for o in objs:
            
            #check if object is either nurbsCurve or locator
            if mc.objectType(o) == "nurbsCurve" or mc.objectType(o) == "locator":
                print("found: {0}".format(mc.objectType(o)))
                #get parent transform and check for keyable transform attributes
                par = mc.listRelatives(o, p = True, f = True)[0]
                keyables = mc.listAttr(par, k= True)
                if any(attr in transforms for attr in keyables):
                    #if there is a keyable transform attribute, add to the list of objects to be added to selection
                    selectObjs.append(par)
                else:
                    print("no Keyables on: {0}".format(o))
    print("selectObjs = {0}".format(selectObjs))           
    mc.select(selectObjs, add = True)
              
selectAllControllers() 

 

0 Likes
Message 15 of 20

jopook
Participant
Participant

maya 2022

0 Likes
Message 16 of 20

Kahylan
Advisor
Advisor

Ok, that is the same Version of Maya as I'm currently on, it's strange, I tested this script by referencing one of my rigs and it works perfectly fine. I'm curious as to what the new script prints...

0 Likes
Message 17 of 20

jopook
Participant
Participant

I get this by only selecting the root :

 

selection = ['|FouleFemme07:ControlRig|FouleFemme07:MAIN_Offset_Ctrl_RestPoseGrp|FouleFemme07:MAIN_Offset_Ctrl|FouleFemme07:MAIN_Ctrl_RestPoseGrp|FouleFemme07:MAIN_Ctrl|FouleFemme07:MAIN_Inner_Ctrl_RestPoseGrp|FouleFemme07:MAIN_Inner_Ctrl|FouleFemme07:ROOTGrp|FouleFemme07:ROOTParent_CtrlGrp2|FouleFemme07:ROOT_CtrlGrp|FouleFemme07:ROOT_Ctrl_RestPoseGrp|FouleFemme07:ROOT_Ctrl']
objs = []
selectObjs = []

0 Likes
Message 18 of 20

Kahylan
Advisor
Advisor
Accepted solution

Ok, so as I was thinking, for some reason the ls command doesn't do the same for you as it does for me. So it took out the flags from this line, so now anything in the scene with that fits this string should be in the objs list:

import maya.cmds as mc

def selectAllControllers():
    """ selects all the Nurbscurves or locators with the same namespace as selection if any of the transform attributes are keyable"""
    
    transforms = ['visibility', 'translateX', 'translateY', 'translateZ', 'rotateX', 'rotateY', 'rotateZ', 'scaleX', 'scaleY', 'scaleZ']
    selectObjs = []
    #get current selection
    sel = mc.ls(sl = True, l = True)
    print("selection = {0}".format(sel))
    for s in sel:
        #get prefix
        characterName = s.split(":")[0]
        #get all objects that contain the prefix
        
        objs = mc.ls("{0}:*".format(characterName))
        children = mc.listRelatives(objs, ad= True)
        objs = children  + objs
        print("objs = {0}".format(objs))

        for o in objs:
            try:
                #check if object is either nurbsCurve or locator
                if mc.objectType(o) == "nurbsCurve" or mc.objectType(o) == "locator":
                    print("found: {0}".format(mc.objectType(o)))
                    #get parent transform and check for keyable transform attributes
                    par = mc.listRelatives(o, p = True, f = True)[0]
                    keyables = mc.listAttr(par, k= True)
                    if any(attr in transforms for attr in keyables):
                        #if there is a keyable transform attribute, add to the list of objects to be added to selection
                        selectObjs.append(par)
                    else:
                        print("no Keyables on: {0}".format(o))
            except:
                pass          
    print("selectObjs = {0}".format(selectObjs))           
    mc.select(selectObjs, add = True)
              
selectAllControllers() 

 

if this still prints out "objs = []" there is some problem with the string itself instead of the flags.

Message 19 of 20

jopook
Participant
Participant

Thank you so much, awesome ! It works fine. Take a little time to load but very nice. 

0 Likes
Message 20 of 20

jopook
Participant
Participant

Is it possible to just change in this line to any kind of attributes in selected channels ? And if more than 1 nurbscurve are selected the command will not repeat on each one with the same namespace ? If 2 or more nurbs curves have differents namespace the script will do the job for both.

transforms = ['visibility', 'translateX', 'translateY', 'translateZ', 'rotateX', 'rotateY', 'rotateZ', 'scaleX', 'scaleY', 'scaleZ']

 

0 Likes