Maya Python: Reset Selected Controls
Posted on October 01, 2013 - category: python
I have a script snippet to share with you today. This is how I quickly reset all selected objects to 0,0,0,0,0,0,1,1,1 in translate, rotate and scale with the stroke of a hotkey command.
This is fantastically useful when you are animating! But it’s also for riggers, modellers or anyone working in Maya. Softimage XSI has this feature built in (Ctrl-Shift-R), so I made a little snippet to do the same in Maya.
First I’ll show the simple code, explicitly written for each attribute. And then for the Python fans, I’ll show how to shorten it using List Comprehension because why not? After the scripts, I’ll explain a bit more how it works. If you just want the script, just copy/paste this and use it. If you want to geek out about List Comprehension and PyMEL, read on…
Reset Selected: The simple version
import pymel.core as pm
def reset_selected(oColl):
for oNode in oColl:
try: oNode.tx.set(0)
except: pass
try: oNode.ty.set(0)
except: pass
try: oNode.tz.set(0)
except: pass
try: oNode.rx.set(0)
except: pass
try: oNode.ry.set(0)
except: pass
try: oNode.rz.set(0)
except: pass
try: oNode.sx.set(1)
except: pass
try: oNode.sy.set(1)
except: pass
try: oNode.sz.set(1)
except: pass
runthescipt = pm.Callback(reset_selected, pm.selected() )
runthescipt()
Reset Selected: The Fancy List Comprehension Version
import pymel.core as pm
def reset_selected(oColl):
trList = ['.tx','.ty','.tz','.rx','.ry','.rz']
sList = ['.sx','.sy','.sz']
# o is each object, x is each attribute
for attr in [(o, x) for o in oColl for x in trList]:
try: pm.Attribute(attr[0] + attr[1]).set(0)
except: pass
for attr in [(o, x) for o in oColl for x in sList]:
try: pm.Attribute(attr[0] + attr[1]).set(1)
except: pass
runthescipt = pm.Callback(reset_selected, pm.selected() )
runthescipt()
Notes about this script:
- Fully supports undo and redo.
- There are 2 scripts below. Grab either one. They do the same thing.
- Set it to Ctrl-Shift-R in your Maya hotkey editor. Don’t forget to set the hotkey to Python.
- I personally use PyMEL instead of maya.cmds because it can generate a list of object pointers rather than strings, so it is a lot easier to use from an OOP point of view. Some studios do not use PyMEL, so be aware of this.
- The pm.Callback() takes an argument, so instead of pm.selected() you could pass it a list of all your character rig controls, or all the locators in your scene, etc. etc.
- Every iteration has a try/except on it, so that if your attribute is driven or locked the script will continue without failing.
- You can also use makeIdentity() to quickly reset an object, but if some of your attributes are locked, this won’t work.
How list comprehension works:
- List Comprehension lets you build up lists quickly by using [x for x in y]
- But you can also loop over two lists at once with one line:
[(x + y) for x in (1,2,3) y in (4,5,6)] - This acts like a nested iterator which will add each of (1,2,3) with each of (4,5,6) resulting in 9 combinations. (1+4, 1+5, 1+6, 2+4, 2+5, and so on…)
- I then take those two combined elements to gather all the transform attributes for all of my objects.
How pm.Attribute works:
- I’m iterating over two lists with my list comprehension so I need to combine it again, casting it as a pm.Attribute( object name + attribute ).
- However, if you were explicitly setting an attribute it would be as simple as yourObject.tx.set(0)
- You could also create your attribute in the list comprehension, but if you try and run the script on a shape node that has no attributes, then the script will fail.
- You could get around this by including a try/except function inside the list comprehension or by filtering your objects for transform nodes only, but that is just becoming too complex for this little script.
In Summary
I use this hotkey all the time! I missed it from Softimage. Your animators are going to love using it too! Feel free to use it, and if you find bugs, improve it or rewrite it in MEL or maya.cmds let us know in the comments. Thanks!
Comments (closed)
Markus Daum: nice one! :) here’s a simple maya commands version…
import maya.cmds as mc
attrVsDefaultValue = {'sx':1, 'sy':1, 'sz':1, 'rx':0, 'ry':0, 'rz':0, 'tx':0, 'ty':0, 'tz':0}
sel = mc.ls(sl=1)
for obj in sel:
for attr in attrVsDefaultValue:
try:
mc.setAttr('%s.%s'%(obj, attr), attrVsDefaultValue[attr])
except:
pass
Cheers! -Markus
Chris Lesage: Thanks Markus! Your version also respects undo/redo. I’ll have to take a look at mine and remember why I used Callback. :) (Also, it looks like my comments don’t keep tabs in the formatting. I’ll see if I can sort that out. Fixed.)