PDA

View Full Version : Check or ignore locked coordinate attributes?


Horganovski
11-09-2010, 05:37 PM
Hey Folks,

First time posting here, I'm a C4D user taking 1st steps with Maya, as a little crash course I've been trying to reproduce a little script I use in C4D to help mirror hip/spine/head poses for characters when building walkcycles etc.

This is a generic script that doesn't need to be added to a rig, to use it I just select the objects and run it and it will mirror the translate X and rotate Y and Z.

It almost works fine, but I've run into one situation that I can't find a solution for yet :
If I use this on a rig where some of the translate channels (say the position for FK controllers in the spine for example) are locked, the script will fail as it returns an error that it can't 'setAttr' for this channel. Is there a way I can tell it to either ignore the error and keep going setting the other coordinates, or can I build in a little check to see if the channel is locked before trying to set the attribute?

Any ideas much appreciated, here's the existing code (my 1st Mel script! : ) )
I'm sure this could be a lot more efficient in places, but I'm just trying to get the basics working right now.


string $sel[] = `ls -sl`; int $selCount = size($sel);
print("Number of selected object(s) is " + $selCount);
if ($selCount == 1) {
vector $pos = getAttr($sel[0] + ".translate");
vector $rot = getAttr($sel[0] + ".rotate");
string $object = $sel[0];
float $xPos = $pos.x;
float $yPos = $pos.y;
float $zPos = $pos.z;
float $xRot = $rot.x;
float $yRot = $rot.y;
float $zRot = $rot.z;
$xPos = $xPos *-1;
$yRot = $yRot *-1;
$zRot = $zRot *-1;
print($object);
setAttr ($object+ ".rx") ($xRot);
setAttr ($object+ ".ry") ($yRot);
setAttr ($object+ ".rz") ($zRot);
setAttr ($object+ ".tx") ($xPos);
setAttr ($object+ ".ty") ($yPos);
setAttr ($object+ ".tz") ($zPos);
}
else {
for ($rc = 0; $rc <= size($sel); $rc++) {
vector $pos = getAttr($sel[$rc] + ".translate");
vector $rot = getAttr($sel[$rc] + ".rotate");
string $object = $sel[$rc];
float $xPos = $pos.x;
float $yPos = $pos.y;
float $zPos = $pos.z;
float $xRot = $rot.x;
float $yRot = $rot.y;
float $zRot = $rot.z;
$xPos = $xPos *-1;
$yRot = $yRot *-1;
$zRot = $zRot *-1;
print($object);
setAttr ($object+ ".rx") ($xRot);
setAttr ($object+ ".ry") ($yRot);
setAttr ($object+ ".rz") ($zRot);
setAttr ($object+ ".tx") ($xPos);
setAttr ($object+ ".ty") ($yPos);
setAttr ($object+ ".tz") ($zPos);
}
}



Cheers,
Brian

Horganovski
11-09-2010, 06:04 PM
Digging around a bit I found this which will tell me if the attribute is locked or not (returns 0 or 1);


getAttr -lock "pCone1.ty";


but my knowledge of Mel syntax is letting me down when I try and assign that to a variable like this -

int %lVar = `getAttr -lock "pCone1.ty"`;


I'm sure it's something simple I'm doing wrong here?

Cheers,
Brian

mduvekot
11-09-2010, 06:12 PM
Here's what I would use:


string $list[] = {"pCube1.translateX","pCube1.translateY","pCube1.translateZ"};
float $value = 3.567;

for ($attr in $list){
if (catch (`setAttr $attr $value`)) {
print ("Could not set " + $attr + "\n");
}
else {
// continue as usual
}
}


but to answer your second question, you could also use something like

int $lock = `getAttr -l $attr`;


string $list[] = {"pCube1.translateX","pCube1.translateY","pCube1.translateZ"};
float $value = 0.1234;

for ($attr in $list){
if (`getAttr -l $attr`) {
print ($attr + " is locked \n");
}
else {
setAttr $attr $value;
}
}

Horganovski
11-09-2010, 06:23 PM
Many thanks Michiel, I think I get that, but need to try it out on my code above to be sure. I'll try it out and see if I can get it working.

- Edit - I've managed to bodge the last example into my script now and it's working, but I did it in a pretty messy way.. I'll tidy it up a bit before I show the result :)

Thanks again,

Cheers,
Brian

CGTalk Moderation
11-09-2010, 06:23 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.