PDA

View Full Version : getAttr/setAttr, expressions and object names as variables...


spinlock
10-11-2003, 05:26 AM
From what I've been reading I should avoid using getAttr and setAttr in expressions (as well as anything in backticks)... the problem is in my case, I don't really know what else to use.

Here's my set-up: I have an expression set up to be evaluated that simply calls a procedure that contains the real crux of the work:

$exp = "\"constraint_expression(\"" + $grp_child + "\", \"" + $child + "\");\"";
expression -s $exp -o $grp_child -ae 1 -uc all ;

// and later, the proc declaration:
global proc constraint_expression(string $grp_child, string $real_child)
{
...
}


What I'm trying to do is set up a generic proc that I can apply to multiple objects without a) duplicating code and b) having to build the entire thing as a string for feeding to the `expression ...` call.

This setup works and the expression is called correctly, but from within my proc 'constraint_expression' I'm not sure how to get and set attributes when the name of the actual object in question is passed in as a variable...

In the code that is called from the expression, I end up doing things (that work) like:

string $my_last_tran = $grp_child + ".LastTranslate";
float $last_trans[] = `getAttr $my_last_tran`;

// and...
setAttr $my_last_tran -type float3 $trans[0] $trans[1] $trans[2];


I've tried some funky things to get around using the backticks, including several variations of building a string and using 'eval' to trick maya into thinking it is a simple "$val = obj.property;" statement, but I wind up with either compile or run-time errors.

Question 1: So how do I correctly access a property of an object in an expression, when the name of the object in question is a variable?

Question 2: How do you know what Maya commands are safe to use in expressions?

ajk48n
10-11-2003, 05:51 AM
I'm not quite sure if this will work, but what if have a MEL script make a locater, and give it an attribute, that is then connected to your variable object's attribute. Then in the expression editor you can just have your object equal to the locator.attribute which you will already know the name of.

This may not work for the setting the attribute.

spinlock
10-11-2003, 06:30 AM
I see what you're getting at, but then I'll still have a problem if I want to have more than one object in the scene relying on this expression because in this case both objects will be using the name of the same locator.

ajk48n
10-11-2003, 06:52 AM
What if different attributes on the locator controlled different objects in your scene?

spinlock
10-11-2003, 07:51 AM
ajk48n: ok, then in that case I'd still have to call the expression routine with a variable, namely: the property of the locator that deals with this particular instance.

I haven't tried it yet, but that would involve a known object, but variable property in my expressions: ie (theLocator.$prop)

I'm still new to MEL, so will this solve my problems related to ($obj.$prop)? I'm getting ready to go to bed and will try tomorrow :thumbsup:

spinlock
10-12-2003, 08:05 AM
Update: I tried out using a locator with a known name and setting variable attributes on it for use in my expression. Unfortunately, I wind up with errors like: 'Invalid use of Maya object "theloc".' and I am still unable to access a variable attribute during expression runtime. (without using getAttr/setAttr that is...)

CGTalk Moderation
01-16-2006, 07:00 AM
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.