Maya Python: Reset Selected Controls

Posted on October 01, 2013 - category: python

Reset Selected Script

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:

How list comprehension works:

How pm.Attribute works:

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