Integrating 3D models with photography
Interested in integrating your 3D work with the real world? This might help
# 1 07-06-2015 , 08:51 PM
Registered User
Join Date: Jun 2014
Posts: 23

Is there a way to multiply an atribute by -1?

Hello, I just wanted to know how I would go about a script with the function of setting multiplying Translate X and Rotate Y by -1.

On a side note, I accidentally made a hotkey that overwrote the - and = keys, because I forgot how useful changing the size of the modifier is. But I don't remember what the of the commands for that used to be, so I can't fix that.

# 2 08-06-2015 , 03:43 PM
NextDesign's Avatar
Technical Director
Join Date: Feb 2004
Posts: 2,988
One way to multiply the values by -1 would be:

setAttr("pCube1.tx", getAttr("pCube1.tx") * -1);
setAttr("pCube1.ry", getAttr("pCube1.ry") * -1);
- and + can be found in the Hotkey Editor under Manipulator, under the names DecreaseManipulatorSize and IncreaseManipulatorSize, respectively.

Imagination is more important than knowledge.

Last edited by NextDesign; 08-06-2015 at 03:47 PM.
# 3 08-06-2015 , 05:52 PM
Registered User
Join Date: Jun 2014
Posts: 23
Thanks for the size manipulator name, I was looking in the wrong places.

As for that -1 code. I think I can see where it's going. Though from the looks of it, it seems like it'd only work for an object whose name is "pCube1" (also tested it to make sure). Is there a way to make it work with the object selected? Let me explain the application:

I wanted to make a symmetrical pose, for my character, and I realized that, if I move the foot curve to a position, and give it a rotation, I can copy the key of the first foot, past it to the other foot, and then go into the channel box, and mulutiply the tranx and roty (and apparently rotz) by -1, and it will produce the mirror transformation.

Soooo, what I want to do is get a code that I can use for a hotkey such that after selecting the first foot and copying the keyframe from the slider: once I select the other foot, I can simply hit this hotkey and it will:
1) Past the keyframe data
and 2) multiply the tranx, roty, and rotz by -1.

# 4 08-06-2015 , 07:40 PM
Gen's Avatar
Super Moderator
Join Date: Dec 2006
Location: South FL
Posts: 3,518
"Cube1" is just to provide a working example, I have a sneaking suspicion that he's no noob user added image

But seriously, if you want the script to act on selection lists then you should check out the "ls" command

// list whatever the user has selected
string $mySel[] = `ls -selection`;
This creates a list of objects that starts counting from zero. So the first object will be $mySel[0].

Perhaps you can approach it where you can select two objects, the first object selected will provide the data and the last will recieve it. It'll cut down some of the manual effort. It may be a bit more elegant to assign them more descriptive variable names though.

string $foot1 = $mySel[0];
string $foot2 = $mySel[1];
The copyKey and pasteKey commands should come in handy here. You could probably query the current frame with:

int $frame = `currentTime -q`;
and use that value with the copyKey command to get the appropriate information. Well that's assuming I understand the situation entirely ha!user added image

- Genny
::|| My CG Blog ||::
::|| My Maya FAQ ||::
# 5 08-06-2015 , 07:59 PM
NextDesign's Avatar
Technical Director
Join Date: Feb 2004
Posts: 2,988
You could also take a look at the Mirror Animation tool provided in Maya Bonus Tools. (Separate download)

Imagination is more important than knowledge.
# 6 09-06-2015 , 04:50 AM
Registered User
Join Date: Jun 2014
Posts: 23
Yea, I'm a complete and total noob. Seems like things are working now though, so thanks!

But I do have one more question, if I may.

How would I go about a "flip-flop"?

See the thing is that even though I had asked for it in the previous post, I actually found that it was more comfortable to just make a code based on the first responce, here is what it looks like:
setAttr("cn_Hand_L.tx", getAttr("cn_Hand_R.tx") * -1);
setAttr("cn_Hand_L.ty", getAttr("cn_Hand_R.ty") );
setAttr("", getAttr("") );
setAttr("cn_Hand_L.rx", getAttr("cn_Hand_R.rx") );
setAttr("cn_Hand_L.ry", getAttr("cn_Hand_R.ry") * -1);
setAttr("cn_Hand_L.rz", getAttr("cn_Hand_R.rz") * -1);
this makes it to where cn_Hand_L will look like a mirror image of cn_Hand_R. But my question is: how would I go about having that happen, but also record the t(xyz) and r(xyz) of cn_Hand_L and get the applied to cn_Hand_R? See that's what I mean by "flip-flop" make it so that after the code is executed, both hands will become a mirror of the other (the way they looked before the code was applied). I'm imagining the practical application of this, being an easy way for me to get a pose to just be mirrored.

If I can get that, then I think I'll finally be ready to animate.

# 7 09-06-2015 , 08:41 PM
NextDesign's Avatar
Technical Director
Join Date: Feb 2004
Posts: 2,988
First off, you don't have to set ty, tz or rx, as you're not changing anything.

The easiest way to do this would be to use two shelf buttons or hotkeys with the names changed. There is another more elegant solution which requires a bit more work however.

If you follow the cn_name_L/R naming convention, you can check if the object selected has L in it, and flip the attributes to R, and vice-versa. The code below will do this for you.

Simply select a side, and run the script - the selected object will be mirrored to the other side.

// get the active selection
string $sel[] = `ls -sl`;
string $nameTokenBuffer[];

// split the name of the first selected object by "_". L/R (should) be the last entry.
int $numTokens = tokenize($sel[0], "_", $nameTokenBuffer);

// extract the last entry.
string $selectedSide = $nameTokenBuffer[$numTokens - 1];
string $mirroredSize = "";

// set the side of the opposite control. L->R / R->L
if (tolower($selectedSide) == "l")
    $mirroredSide = "R";
else if (tolower($selectedSide) == "r")
    $mirroredSide = "L";
    error("Unknown side \"" + $selectedSide + "\"");

// build the name of the opposite control using the mirrored side, and modify the values of its xform
setAttr("cn_Hand_" + $mirroredSide + ".tx", getAttr("cn_Hand_" + $selectedSide + ".tx") * -1);
setAttr("cn_Hand_" + $mirroredSide + ".ry", getAttr("cn_Hand_" + $selectedSide + ".ry") * -1);
setAttr("cn_Hand_" + $mirroredSide + ".rz", getAttr("cn_Hand_" + $selectedSide + ".rz") * -1);

Imagination is more important than knowledge.

Last edited by NextDesign; 09-06-2015 at 08:50 PM.
Posting Rules Forum Rules
You may not post new threads | You may not post replies | You may not post attachments | You may not edit your posts | BB code is On | Smilies are On | [IMG] code is On | HTML code is Off

Similar Threads