PDA

View Full Version : [MEL] setAttr vectorArray: how to pass an entire array?


robitabu
11-15-2011, 09:06 AM
Maybe I missed some documentation and you can shed some light.
As of now I have to pass an entire list of vector variables to setAttr if I want to set a vectorArray attribute on a node. That's tedious (really not efficient). I'm pretty shure there's a better way to do that.

I define an array of vectors and I add a new vectorArray to a new locator node.
vector $vettori[];
$vettori[0] = <<5,6,7>>;
... $vettori[8] = <<5,0,1>>;
spaceLocator;
addAttr -dt "vectorArray" -longName "vettori";


Now I set the vectorArray attribute copying all the previous vector array variables' values into it.
This works (not efficient coding):
setAttr "locator1.vettori" -type vectorArray (size($vettori)) ($vettori[0]) ($vettori[1]) ($vettori[2]) ($vettori[3]) ($vettori[4]) ($vettori[5]) ($vettori[6]) ($vettori[7]) ($vettori[8]);

This doesn't (although it would be more flexible and efficient):
setAttr "locator1.vettori" -type vectorArray (size($vettori)) $vettori;
// Error: setAttr: Error reading data element number 4: //

It seems like I can't pass the $vettori vectorArray variable value to the setAttr command as I would.
Am I missing something?

NaughtyNathan
11-15-2011, 10:08 AM
python would probably allow you to pass in a single vector list like that, but MEL does not really have very sophisticated command argument parsing. However, what is efficient in terms of code performance is not always the same in terms of what is efficient for a human to write...

Is there any reason why you want to use the -dt vectorArray attribute? Because it's a dataType it's an atomic entity, you cannot see it in the attribute Editor and you cannot access or modify individual elements of it. Is this really what you want?

:nathaN

robitabu
11-15-2011, 10:40 AM
python would probably allow you to pass in a single vector list like that, but MEL does not really have very sophisticated command argument parsing. However, what is efficient in terms of code performance is not always the same in terms of what is efficient for a human to write...
I'm not going to learn Python right now. I'm concentrated on MEL and I want to see how far can I get with it. And yes, I was talking about "human efficiency" in the post. Fact is, the $vettori[] array may have different size in time, I don't want to hardcode the "($vettori[0]) ... ($vettori[8])" series of values passed to the setAttr command. Next time I use it could be made of more (or less) than 9 elements.

Is there any reason why you want to use the -dt vectorArray attribute? Because it's a dataType it's an atomic entity, you cannot see it in the attribute Editor and you cannot access or modify individual elements of it. Is this really what you want?
Yes Nathan, the idea is that the vectorArray attribute may be updated as a whole, because it describes a set of data which needs to be consistent, and I don't even need to change it anyhow but with MEL (not at the moment), hence simply passing an updated $vettori[] array again and again.

Anyway, I'm considering using a vectorArray attribute and/or a series of distinct vector attributes as well. I'm investigating pros and cons of both approaches. It's a learning process right now. That's all in preparation for doing more complex stuff later on.

Tell me, is there any way I could "build" the "($vettori[0]) ... ($vettori[8])" set of values as a string concatenation somehow, and then "insert" it into the setAttr command in MEL?

NaughtyNathan
11-15-2011, 01:16 PM
Tell me, is there any way I could "build" the "($vettori[0]) ... ($vettori[8])" set of values as a string concatenation somehow, and then "insert" it into the setAttr command in MEL?
yeah, this is the way you will have to do it. I couldn't be bothered going through this in the previous post because I didn't think you'd really want to do it this way. :) but you seem pretty sure, so here goes:
vector $vArray[] = {<<1,1,1>>,<<2,2,2>>,<<3,3,3>>,<<4,4,4>>}; //etc...
string $cmd = "setAttr -type vectorArray ";
$cmd += ($object + "." + $attr); // add the object.attribute
$cmd += ( " " + size($vArray) + " "); // add the array size
for ($item in $vArray)
$cmd += ($item + " "); // add each item in the array
evalEcho($cmd); // eval (execute) the command string

// test it worked?
getAttr ($object + "." + $attr);

:nathaN

robitabu
11-15-2011, 01:35 PM
yeah, this is the way you will have to do it. I couldn't be bothered going through this in the previous post because I didn't think you'd really want to do it this way. :) but you seem pretty sure, so here goes:
vector $vArray[] = {<<1,1,1>>,<<2,2,2>>,<<3,3,3>>,<<4,4,4>>}; //etc...
string $cmd = "setAttr -type vectorArray ";
$cmd += ($object + "." + $attr); // add the object.attribute
$cmd += ( " " + size($vArray) + " "); // add the array size
for ($item in $vArray)
$cmd += ($item + " "); // add each item in the array
evalEcho($cmd); // eval (execute) the command string

// test it worked?
getAttr ($object + "." + $attr);

:nathaN

Yeah!!! Thank you Nathan :-)
I was missing the evalEcho() command, that's the key to my situation here.

And as usual, you were very prolific in detailing your suggestion. I wasn't gonna ask for so much :-) Your proof of concept is not only working, it's complete, detailed and flexible (more than required). :applause:

NaughtyNathan
11-15-2011, 01:46 PM
no problem Roberto, I find it's usually better to pre-empt any further questions that may arise from a less comprehensive reply and, as this forum is also useful for solving possible future questions too, it's good to give more info than maybe necessary in case someone else comes looking later...

incidentally, I only used evalEcho so we could see the resulting command line, you may want to use plain old eval() in a real production setting.

:nathaN

robitabu
11-15-2011, 01:53 PM
incidentally, I only used evalEcho so we could see the resulting command line, you may want to use plain old eval() in a real production setting.

I see. I'm browsing around the docs to read more about eval() and evalEcho() right now ...

CGTalk Moderation
11-15-2011, 01:53 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.