Python Script for Querying Joints in a Scene

Python Script for Querying Joints in a Scene

johngrivas582
Observer Observer
365 Views
2 Replies
Message 1 of 3

Python Script for Querying Joints in a Scene

johngrivas582
Observer
Observer

I’m trying to create a loop that will cycle through a scene query for joints and create a locator on each joint, using python, any Ideas ?

0 Likes
366 Views
2 Replies
Replies (2)
Message 2 of 3

16497643852
Contributor
Contributor

Hello @johngrivas582 ,

Hope you're doing fine.

 

This is a very old script I had written.

It does what you've requested, give it a try on your scene file. Let me know if it works for you and yes tweak it for your convenience.

import maya.cmds as mc
import codecs

#jnt_selection
sel = mc.ls(sl = True)
jnt_hie_lst = []
#print(sel)

#for_loop_for_appending_jnt_hie_in_lst_and_creating_controllers
for s in sel:   
    jnt_hie = mc.select(s, hi = True)
    jnt_hie_sel = mc.ls(sl = True)
    print (jnt_hie_sel)
    jnt_hie_lst.append(jnt_hie_sel)
    for j in jnt_hie_sel:
        new_name = codecs.encode(j)
        new_crc = mc.spaceLocator(name = '%s_ctrl' %new_name)
        new_grp = mc.group(name = '%s_ctrl_grp' %new_name)
        new_par = mc.parentConstraint(j, new_grp)
        mc.delete(cn = True)
        app = mc.parentConstraint(new_crc, j)
#print(jnt_hie_lst)

#preserve_ctrls_lst
ctrls_lst = []
#for_loop_for_querying_the_jnt_and_and_appending_the_query_in_the_ctrls_lst
for j in jnt_hie_lst:
    jnt_qry = mc.select(j)
    jnt_qry_sel = mc.ls(sl = True)
    for j_qry in jnt_qry_sel:
        mc.select(j_qry)
        mc.ls(sl = True)
        jnt_par_qry = mc.parentConstraint(tl = True, query = True)
        print(jnt_par_qry)
        jnt_par_qry_pop = jnt_par_qry.pop()
        ctrls_lst.append(jnt_par_qry_pop)
#print(ctrls_lst)

 

16497643852_0-1698238086111.png

 

0 Likes
Message 3 of 3

Kahylan
Advisor
Advisor

Hi!

 

So the code posted way quite flawed.

1) it needs selection to be actve

2) It does preserve some controls you never talked about...

The script you need is very simple:

import maya.cmds as mc

joints = mc.ls(type= "joint")

for j in joints:
    
    loc = mc.spaceLocator(n= "{0}_LOC".format(j))
       
    mat = mc.xform(j, m= True, ws= True, q= True)
    mc.xform(loc, m = mat, ws = True)

 

It's a python script so you'll need to run it from a python tab.

 

I hope it helps!