Set transform via expression


I have a list of objects that I want to set the transform scale and rotation to the same as a master object

what I do now is:

slave_01.translateX =masterObject.translateX;
slave_01.translateY =masterObject.translateY;
slave_01.translateZ =masterObject.translateZ;
slave_01.rotateX =masterObject.rotateX;

slave_n.translateX = masterObject.transtateX;

with a lot of slave objects this gets tedious very quickly, In an expression, is there a simpler way to get this done?
like a for loop or something?



Yes there is :slight_smile: This is often not recommended for many good reasons, but I have done this many times myself, and it does work. What you can do is to make a mel or python function that lists and loops all the children of a group. This way you can add as many slaves and masters as you like without changing the expression. You have to make sure that this function is available on all machines that needs to open the Maya file. You can also “bake” animation to make it independent of the expression. Using “getAttr” and “setAttr” is slower to compute than “=”, but I have done this on hundreds of objects without any problems:

import maya.cmds as cmds
def masterSlaveExpression():

    slaves = cmds.listRelatives("slaves", children=True)
    masters = cmds.listRelatives("masters", children=True)

    for s, m in zip(slaves, masters):
        master_translateX = cmds.getAttr(s+".translateX")
        cmds.setAttr(s+".translateX", master_translateX)

Now you can use an expression to run this function. To run python in mel expressions, use the “python” command