Pages

Tuesday, April 3, 2012

Quick Script Recover 2D Array with different number rows

Hi,
I'm planning on using this in transfer of broad animator controls to individual animator controls and I think I would need anim curves which could be different numbers for different parts. Suppose we want a multi-dimensional stored in a 2D array and multi dimensional array could have different number of columns per row here are some ideas to do that in MEL for Maya.



/**result recover elements at specified index from $array
@result subset of $array for given $index
@param int $index index we wish to recover elements
@param float $array[] list of all possible elements
@param int $num[] number of elements per each row, since it may be different
@param int $sum[] cummulative sum of elements per each row it helps speed up calculation
*/
global proc float[] na_get2DArrayDiffSize(int $index, float $array[], int $num[], int $sum[] )
{
float $result[] = {};

//assert data okay to use
na_get2DArrayDiffSize_assert($index,$array,$num,$sum);
int $i = 0;

for($j=0; $j < $num[$index]; $j++)
{
//use the cumulative sum and the number for a given row to figure out recovered element
$i = $sum[$index] - ( $num[$index] - $j );
$result[size($result)] = $array[ $i ];

}
return $result;
}

/**
*/
global proc
na_get2DArrayDiffSize_assert(int $index, float $array[], int $num[], int $sum[])
{
if($index < 0){ error("error expecting non-negative index"); }
if(size($array) == 0){ error("error expecting an array with elements"); }
if($index >= size($array) ){ error("error expecting smaller index"); }
if( size($num) == 0 ){ error("error some number of elements per row"); }
if( size($num) != size($sum) ){error("error expecting sum and num same length");}
if( $num[$index] <= 0 ){error("error expecting number of elements > 0");}
if( $sum[$index] <= 0 ){error("error expecting sum of elements > 0");}
}
/**
*/
global proc
na_get2DArrayDiffSize_unitTest()
{
float $array[] = {1,2,1,4,5,6};
int $num[] = {3,1,2};
int $sum[]= {3,4,6};

print( na_get2DArrayDiffSize(0,$array,$num,$sum) );
print("\n");
print( na_get2DArrayDiffSize(1,$array,$num,$sum) );
print("\n");
print( na_get2DArrayDiffSize(2,$array,$num,$sum) );
print("\n");
print( na_get2DArrayDiffSize(5,$array,$num,$sum) );
print("\n");
/*
na_get2DArrayDiffSize_unitTest();
1
2
1

4

5
6

// Error: error expecting number of elements > 0 //
*/
}





Regards,
Nate