MEL: Trying to get values from an array.
Hi, all. I hope someone can point me in the right direction on this.
I'm trying to perform an operation on each spring in a spring object, and I need to specify which particle the spring's first endpoint is attached to.
Spring objects have an attribute called '.ptz' which stores exactly this information as an Int32Array. That is, the object spring1 has an attribute, spring1.ptz, which is an integer array that looks (for example) like this:
4 5 2
Which indicates that there are 3 springs in the object, and that the first one starts at particle 4 (of the particle object the springs are attached to), the second one starts at particle 5, and the third one starts at particle 2. Simple, right?
But I have no idea how to read these values within Maya!! --The ONLY way I have found so far is to save the whole bloody scene as a Maya ASCII and then find the line where the spring object is created and the value of .ptz is set. I can't use getAttr or getParticleAttr or anything else I can think of to return the values while the scene is open. But surely there must be SOME way to read an attribute that has the array format?! Anyone have any suggestions? --I'm hoping it's just something simple I've missed, but the docs on arrays don't seem to be too helpful, as they all deal with user-defined array variables and not object attributes.
Thanks in advance,
Does it give an error when you try to getAttr, what does it say?
I've never tried this and I'm not at a computer with maya right now so I may be of no help...but, Have you tried declaring the variable to catch the values as an array first. Then getAttr directly into that variable?
$springPts = `getAttr spring1.ptz`;
if that doesn't work, perhaps you can use a for loop to step through the whole array and drop the values into an array variable:
for($i 0; $i < size(spring1.ptz); $i++)
$springPts[$i] = `getAttr spring1.ptz[$i]`;
Dunno if either of those would work but its worth a try. My syntax might be off on the size() statement, it may need to be size(`getAttr spring1.ptz`); might or might not work.
Hi, Sean, and Happy New Year to you!
Thanks very much for the suggestions, but I think Maya has us both beat this time.
According to the Script Editor, integer arrays can't be printed, 'getAttr'ed, or otherwise accessed straightforwardly, either with expressions or interactively. Take a look (the long name for the .ptz attribute is '.point0'):
expression -e -s "print(springShape.ptz);" -o "" -ae 1 -uc all testExpression;
// Error: Attributes must be of float, integer, or boolean types: point0 //
// Error: Expression invalid after edit. //
// Error: line 1: The data is not a numeric or string value, and cannot be displayed. //
And so on.
Because the .ptz attribute data type is NOT a multi attribute, you can't use the '.attribute[#]' syntax to access individual elements ('// Error: Attribute not found or variable missing '$': springShape.ptz //'
). I think that the data in an integer array can be accessed using Maya API, but that's out of my sphere.
So I'm ready to admit defeat. Happily, the script I need this for only runs once, as a modelling tool, and not as a dynamic part of the scene. That means I can use the incredibly awkward workaround of exporting the spring object as a Maya ASCII file, then parsing, tokenizing, etc., to get the data back in.
Which seems kind of like cracking open an egg, pouring the contents into an egg-shaped container, hard-boiling it in that, and then carefully putting it back into the original shell. Sigh.
Thanks again for the suggestions, Sean. Wish me luck!
I have some really good egg-shaped bottles, selling cheap...