PDA

View Full Version : Instancing script problem


EightBit
04-09-2009, 10:20 PM
I'm writing a script to create a 2D array of instances of a selected object. I've been able to get the code to run when executing individual sections, and the proc compiles, but when I try to run it, Maya hangs. Can anyone see why?
global proc pixl_instanceArray ()
{
// Create Parent for instances
string $instancePrntTmp[] = `spaceLocator -n "inst_prnt"`;
string $instancePrnt = $instancePrntTmp[0];

// Select object
string $selTmp[] = `ls -sl`;
string $objToInstance = $selTmp[0];

// Get bounding box, including invisible children:
float $xFormBBTmp[] = `xform -q -bbi $objToInstance`;
// Calc X & Z Dimensions:
float $selXDim = abs($xFormBBTmp[1] - $xFormBBTmp[0]);
float $selZDim = abs($xFormBBTmp[5] - $xFormBBTmp[4]);

int $numXInstances = 10;
int $numZInstances = 10;

float $xOffset = $selXDim + $selXDim/2;
float $zOffset = $selZDim + $selZDim/2;


//*** create the instances ***//

for ($i = 1; $i <= $numXInstances; $i++)
{
for ($k = 1; $k <= $numZInstances; $k++)
{
$instanceName = `instance -name ($objToInstance + "X_" + $i + "Z_" + $k) $objToInstance`;
setAttr ($instanceName[0] + ".translateX") ($i * $xOffset);
setAttr ($instanceName[0] + ".translateZ") ($k * $zOffset);
parent $instanceName[0] $instancePrnt;
}
}
}
pixl_instanceArray();

Also, when I use instance, it returns a string, which is the name of the new node. However, in order to address the instance, bracket notation is required, as if addressing a string[] - why? ie:
int $i = 1;
int $k = 7;

float $xOffset = 3;

$instanceName = `instance -name ($objToInstance + "X_" + $i + "Z_" + $k) $objToInstance`;$instanceName = `instance -name ($objToInstance + "X_" + $i + "Z_" + $k) $objToInstance`;
setAttr ($instanceName[0] + ".translateX") ($i * $xOffset); // Works

setAttr ($instanceName + ".translateX") ($i * $xOffset); // Fails:
// Error: Line 3.41: Illegal operation "+" on data of type string[]. //
Thanks for your time.

greatPumpkin
04-11-2009, 05:44 PM
many maya commands will return a string array, this is because they can often times have multiple output nodes. for example if you run the duplicate command on several objects you will need an array to get back all of the resulting nodes.

Since you cannot change return values on the fly in mel, any command that can possibly return an array will always return an array even if only one item is returned.

if you are ever in any doubt the documentation will tell you what variable type a given command will output.

EightBit
04-12-2009, 11:47 PM
Thanks for the reply.
In the manual, instance is said to return string while other commands like ls, return string[], so I was a bit confused by this. Perhaps it is a misprint.
Can you see why the script causes Maya to hang?
Thanks again.

CGTalk Moderation
04-12-2009, 11:47 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.