Home Website Youtube GitHub

Reset rig python command

Hi there , sorry for the super simple question but i’ve been digging around the scripts trying to find this without much luck so far , how do i reset the rig with a python command?
i’m writing a simple scipt to automate a pre roll from default pose

As far as I know, there is no mGear command to do this. The menu command only resets custom attributes if you have them selected in the channel box.

Here is a quick script that should work. You might have to edit it depending on your naming conventions. Or use a selection set, instead of *_ctl.

There might be a better way. If you have a lot of controls, there might be a way to find a Maya command that can reset multiple objects at once in one single command. But since you have to query the custom attributes for each control, this is the only way I can think of:

import pymel.core as pm
from mgear.core import attribute
controllers = pm.ls('*_ctl', type='transform')
for eachControl in controllers:
    # returns the main transforms
    mainAttrs = pm.listAttr(eachControl, keyable=True)
    # returns any userDefined that are in the channelbox
    userAttrs = pm.listAttr(eachControl, userDefined=True, channelBox=True)
    channelAttrs = mainAttrs + userAttrs
    attribute.reset_selected_channels_value([eachControl], attributes=channelAttrs)

edit: note that this script will also reset non-keyable attributes, so it will change any settings on the world_ctl, like joint visibility or whatever. You might want to skip attributes that are non-keyable. That depends on your rigs.

4 Likes

There is another option available to do this. When the rig build is done, it generates a dagPose node and stores all the initial positions of the controls. You can even add your own if you need to.

import maya.cmds as cmds

# -- get the root node of the rig
rig_node = cmds.ls("*.is_rig")
if rig_node:
    # -- get the name of the rig
    rig_node = rig_node[0].split(".")[0]
    # -- query all connections, as you might add your own
    dag_poses = cmds.listConnections("{0}.rigPoses".format(rig_node))
    if dag_poses:
        # -- restore the saved positions
        cmds.dagPose(dag_poses[0], restore=True)
7 Likes

oh wow , thanks guys. I’m going to test this out asap

cheers
Sam

1 Like

@chrislesage

how could i make this script work just to reset selected controls / attributes?

Simple answer: For selected controllers, change:

controllers = pm.ls('*_ctl', type='transform')

to

controllers = pm.selected(type='transform')


Longer answer. Here is a script that tests if you have channels selected. If not, it resets all keyable channels. Otherwise it only resets the channels you have selected.

I no longer remember if I wrote this, or got it from the menu commands, or if I changed the menu version. But this is what I use, more or less:

import pymel.core as pm
from mgear.core import attribute

def reset_attributes(oColl):
    for oNode in oColl:
        params = [
                x.attrName() for x in oNode.listAttr(keyable=True)
                if not x.attrName() in ['v','tx','ty','tz','rx','ry','rz','sx','sy','sz']
                ]
        transformAttrs = [
                x.attrName() for x in oNode.listAttr(keyable=True)
                if x.attrName() in ['tx','ty','tz','rx','ry','rz','sx','sy','sz']
                ]
        # mGear smart_reset() only does SRT if you have nothing selected. So hijack the function it calls.
        # You must pass a list to the function, otherwise it will try to iterate over the object's name.
        attributes = attribute.getSelectedChannels()
        if attributes:
            attribute.reset_selected_channels_value([oNode], attributes)
        else:
            reset_srt(oNode)
            if transformAttrs:
                attribute.reset_selected_channels_value([oNode], transformAttrs)
            if params:
                attribute.reset_selected_channels_value([oNode], params)


def reset_srt(oNode):
    defaultValues = {'sx':1, 'sy':1, 'sz':1, 'rx':0, 'ry':0, 'rz':0, 'tx':0, 'ty':0, 'tz':0}
    # o is each object, x is each attribute
    for attr in defaultValues:
        try: oNode.attr(attr).set(defaultValues[attr])
        except: continue


reset_attributes(pm.selected())
3 Likes

thank you @chrislesage but if i have no controllers selected, its not reseting …

Sorry I wasn’t clear. “it resets everything” as in everything in your channel box. I’ve edited it to be more clear.

tests if you have channels selected. If not, it resets all keyable channels. Otherwise it only resets the channels you have selected.

got it, thank you Chris!