PDA

View Full Version : using attributes in expressions


sagmam
12-03-2004, 03:47 PM
Hi all, I have a question about using attributes:

Let's say I create a simple cube, and add an attribute to it, called "MyObject" of type string.
I write a name of another object in the scene inside that attribute.
Now, I add another attribute to the cube, called "SomeVal", and I want to write an expression for it, involving the object whose name is in the MyObject attribute. Something like:

SomeVal = MyObject.tx/MyObject.ty

Only the above doesn't work of course... I'm looking for the right way to do this.
Any ideas?
thanks...

mhovland
12-03-2004, 04:28 PM
It looks like you would have to get the value of the attribute "MyObject", and then construct your expression using the value.

Something like this:

string $name = `getAttr pCube1.MyObject`;

setAttr pCube1.SomeVal (($name+".tx")/($name+".ty"));

That should do it, but all that is doing is setting attribute values. If you want the attribute "SomeVal" to be controlled by an expression (i.e. right clicking on the attribute and selecting "expression") well, someone else will have to chime in on how to do that. :)

mark_wilkins
12-06-2004, 01:29 AM
Any ideas?
thanks...

Since expressions are nodes whose connections are defined by the specific object names you use in the expressions, you can't use an attribute to redirect those connections. It just won't work.

Failing that, you have to use Mike's getAttr method, which unfortunately does not allow Maya to predict when your expression has to be recalculated, so updates may not happen in real time.

Usually, the solution to this is to make a MEL script that builds a custom expression and installs it using the expression command. This allows your expression to have the exact object name (rather than trying to read it from an attribute.) If you want to change the object that's connected to your expression, you can use a different MEL script to delete the expression and make another one.

-- Mark

sagmam
12-06-2004, 07:15 AM
So in this case, I'd be better off building some MEL script to do the calcs for me, and simply run it when I need the results. Correct?

mark_wilkins
12-06-2004, 07:20 AM
So in this case, I'd be better off building some MEL script to do the calcs for me, and simply run it when I need the results. Correct?

Not exactly -- I'd use a MEL script that hooked up an expression that uses the exact name of the node you want. If you don't use an expression, it won't update in real time.

-- Mark

CGTalk Moderation
01-20-2006, 01: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.