Unlocking attributes via python

Unlocking attributes via python

Craig_Lamorte
Advocate Advocate
25,099 Views
5 Replies
Message 1 of 6

Unlocking attributes via python

Craig_Lamorte
Advocate
Advocate

Hi,

 

How would i go about unlocking translate, rotate, scale if they have been locked (like if you right click them in channel box and click lock.  There will be a gray box surrounding that attribute when locked)

 

I have been trying to find out how to do it myself without much success.  The closest I have gotten was doing this command

 

pm.setAttr('poly.translate', lock=False)

 

 

So this command did not work unlocking the attributes when I had locked them in the channel box through the UI.  The only way that command will work is if lock it by running the command with lock=True first...  Which is not very useful if it only unlocks it under certain conditions.

 

I cannot find any command that will unlock them after locking them through the channel box ui.

 

Thanks

Craig

Accepted solutions (1)
25,100 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
Accepted solution

The problem you are encountering is that you are trying to unlock translate and not translateX, translateY, and translateZ all separately.

I typically build a list of axis

 

axis = ['X', 'Y', 'Z']

 

and a list of transform attrs

 

attrs = ['t', 'r', 's']

 

and then run a nested for loop to unlock all of them.

 

for ax in axis:
    for attr in attrs:
        cmds.setAttr(obj+'.'+attr+ax, lock=0)

which basically ends up with

cmds.setAttr(obj+'.tX', lock=0)

etc

0 Likes
Message 3 of 6

Craig_Lamorte
Advocate
Advocate

awesome that totally worked! thanks!

 

guess it doesn't like when i use just translate / scale / rotate to grab all 3

0 Likes
Message 4 of 6

Craig_Lamorte
Advocate
Advocate

**edit - nevermind its working now, not sure why it wasn't working in the first place

 

One more question...  Trying to query whether the attribute is locked or not.  I tried this -

 

cmds.getAttr(obj+'.tx', lock=True)

 But all i am getting his False no matter if its locked or not...

 

 

0 Likes
Message 5 of 6

morten_bohne
Advocate
Advocate

This works for me:

 

import maya.cmds as cmds
objs = cmds.ls(sl=True)
for obj in objs:
    attrs = cmds.listAttr(obj)
    for attr in attrs:
        try:
            if cmds.getAttr("{0}.{1}".format(obj, attr), lock=True) == True:
                print "{0}.{1} is Locked".format(obj, attr)
            else:
                print "{0}.{1} is unlocked".format(obj, attr)
        except ValueError:
            print "Couldn't get locked-state of {0}.{1}".format(obj, attr)

Or to unlock all attributes on selected objects in pymel: 

import pymel.core as pm

for obj in pm.selected():
    for a in pm.listAttr(obj):
        obj.attr(a).unlock()

shorter, faster but less readable:

import pymel.core as pm
[obj.attr(a).unlock() for a in pm.listAttr([obj for obj in pm.selected()])]
Message 6 of 6

p.ivanov2KWFH
Observer
Observer

your code was not completely correct
I've refined it a little)

 

objs = cmds.ls(sl=True)
axis = ['x', 'y', 'z']
attrs = ['t', 'r', 's']
for ax in axis:
    for attr in attrs:
    	for obj in objs:
    		cmds.setAttr(obj+'.'+attr+ax, lock=0)