PDA

View Full Version : Adding custom attribute FROM an expression?


victor
04-20-2006, 01:54 AM
I have an expression that I need to keep adding to objects, but it relies on custom attributes.

So far, I've been adding those attributes to the objects first, but that's getting annoying.

Is there a way I can just include something in the expression to add them automatically?

scottiedoo
04-20-2006, 05:41 AM
can you explain a little clearer? this is my understanding of what you are asking...

you have an object with a custom attribute and an expression connected to it... but when you make new objects you would like to automatically like to add a new custom attribute and an expression to it instead of having to do it by hand each time.. am I right?

so what you are looking for is a script to create an predefined expression and for it to also create a new attribute for you on that object. right?

if this is what you are after.. then you could do this:


string $selectedObjects[];
$selectedObjects = `ls -sl`;

for ($object in $selectedObjects) {
addAttr -ln "ATTRIBUTENAME" -at double -min 0 -max 1 -dv 0 $object;
setAttr -e -keyable true ($object + ".ATTRIBUTENAME");

expression -s "// PUT EXPRESSION HERE //" -n ("NAMEOFEXPRESSION_" + $object);
}


you would just have to change a few things for your use, such as the attribute name (both places have to be the same), the type, min, max, default value (dv)... put in the expression you want and the name of it.

So all you would have to do is select that object you want to do it to and run this code..

is that what you want?

-Scott

victor
04-20-2006, 02:47 PM
is that what you want?
That's pretty close. I figured I could do it with a script like that, but didn't bother to try it.

I was mostly hoping that I could just put it all into the expression, and have the attributes be added where needed, since I've been making small modifications each time. Like this, I can make it a shelf button and modify it before use.

Thanks.

scottiedoo
04-20-2006, 05:34 PM
i dont think you want to put that kind of function into an expression. By default, an expression is evaluated when you make a change to its incoming connections and also during playback so if you happen to make this expression, then if you scrub the timeline, that expression will run that command on however many frames you moved.

I think you have the wrong idea about what expressions are used for. Most people do not use them to run mel commands that do what you are trying to do.

Correct me if I am wrong here...

Again you are not being very clear on what you want it to do. Example perhaps?

victor
04-20-2006, 06:06 PM
i dont think you want to put that kind of function into an expression. By default, an expression is evaluated when you make a change to its incoming connections and also during playback so if you happen to make this expression, then if you scrub the timeline, that expression will run that command on however many frames you moved.Maybe I'm just thinking about it the wrong way, but basically I see custom attributes as variables attached to objects. Is that wrong?

And since I can add variables into expressions, I thought there might be a way to do the same for custom attributes.
Again you are not being very clear on what you want it to do. Example perhaps?
Current example would be that I have an expression that relies on the presence of the attribute spriteTwistPP (and others). If I want to add that expression to a new particle shape, I have to first go and add the attributes to the shape. I was hoping to be able to skip that step.

Robert Bateman
04-21-2006, 07:38 AM
Maybe I'm just thinking about it the wrong way, but basically I see custom attributes as variables attached to objects. Is that wrong?

Sort of.

And since I can add variables into expressions, I thought there might be a way to do the same for custom attributes.

the issue here though, is that the variables you create in the expression are temporary. They only exist for the duration over which the expression is run. It's unlikely that you could dynamically add attributes from inside an expression. If you create an expression, then do

print `getAttr expression1.internalExpression`;

you should see the internally evaluated expression.

The following two commands will list the inputs and outputs from the node, which correspond to .I and .O

print `listConnections -d 0 -s 1 -p 1 expression1.input`;
print `listConnections -d 1 -s 0 -p 1 expression1.output`;

So for an expression to add a new attribute, it would not be part of the .I or .O array, and therefore Maya would not know to update the DG for the expression. In *theory* it would be do-able, however you'd have to use getAttr and setAttr to get and set the attribute values. That however will mess up the DG evaluation, so it's not recommended.

Current example would be that I have an expression that relies on the presence of the attribute spriteTwistPP (and others).

I think that pretty much sums up the problem. You'll have to add the attrs first i'm afraid.

CGTalk Moderation
04-21-2006, 07:38 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.