View Full Version : average pivot script

 DualOpAmp12 December 2007, 05:37 PMI wrote this code in order to average the pivot between components. That way, I can use an averaged pivot when modelling with MEL. The problem is that the script keeps telling me that \$components is not initialized. Can anyone help? global float[] proc centerComponentPivot() { // Lists the selection. \$components = `ls -sl -fl`; float \$averageXYZ[] = {0, 0, 0}; // Itterates through the selection. for(\$i = 0; \$i < size(\$components); \$i ++) { // Finds the position of a component, and assigns it to a float array. float \$XYZ[] = `pointPosition \$components[\$i]`; print("Position " + \$XYZ[0] + " " + \$XYZ[1] + " " + \$XYZ[2] + "\n"); // Adds the axis' positions together. For example, averageXYZ[0] // would be the total X-axis positions of the components. \$averageXYZ[0] = \$averageXYZ[0] + \$XYZ[0]; \$averageXYZ[1] = \$averageXYZ[1] + \$XYZ[1]; \$averageXYZ[2] = \$averageXYZ[2] + \$XYZ[2]; } // Divides each axis' position by the number of components. This is the // equation for finding an average. By finding the average of each axi, // I can now position the selected components' pivot. \$averageXYZ[0] = \$averageXYZ[0] / size(\$components); \$averageXYZ[1] = \$averageXYZ[1] / size(\$components); \$averageXYZ[2] = \$averageXYZ[2] / size(\$components); print \$averageXYZ; }
Gravedigger
12 December 2007, 05:48 PM
well what should i say... hmmm...
maya's right \$components isn't initialized!

\$components = `ls -sl -fl`;

this is your first use of \$components so you need to initialize it. the ls command will return an array and you're using it later on in the script as an array so the line should look like this:

string \$components[] = `ls -sl -fl`;

i'm not sure if there are some more mistakes but this is surely what maya meant.
please try the script with those changes and ask if it still isn't working.

DualOpAmp
12 December 2007, 06:17 PM
Nope, tried it and the error is still the same.

Error Report:
// Error: global float[] proc centerComponentPivot()
//
// Error: Syntax error //
// Warning: float \$averageXYZ[] = {0, 0, 0};
//
// Warning: Global variable is already initialized; this occurrence is ignored. //
// Error: for(\$i = 0; \$i < size(\$components); \$i ++)
//
// Error: "\$components" is an undeclared variable. //
// Error: float \$XYZ[] = `pointPosition \$components[\$i]`;
//
// Error: "\$components" is an undeclared variable. //
// Error: \$averageXYZ[0] = \$averageXYZ[0] / size(\$components);
//
// Error: "\$components" is an undeclared variable. //
// Error: \$averageXYZ[1] = \$averageXYZ[1] / size(\$components);
//
// Error: "\$components" is an undeclared variable. //
// Error: \$averageXYZ[2] = \$averageXYZ[2] / size(\$components);
//
// Error: "\$components" is an undeclared variable. //
// Error: }; //
// Error: Syntax error //

Gravedigger
12 December 2007, 06:24 PM
global float[] proc centerComponentPivot()

wrong way. do it like this:

global proc float[] centerComponentPivot()

you do not return a value or better your float array. under "print \$averageXYZ;" at the very bottom of your scirpt add the following:

return \$averageXYZ;

this should really do the trick

DualOpAmp
12 December 2007, 06:59 PM
I can't believe I missed that. I feel like a dunce.

Thanks a lot, it works now.

CGTalk Moderation
12 December 2007, 06:59 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.

1