MEL array sorted don't work


#1

Hi everyone,
I want with mel, an array sorted.
This work fine and return correctly “phong” nodeType for each shader phong.
[```

proc ordine()
{
string $shader[] = ls -type "lambert" -fl;
string $sha2;
for ( $sha2 in $shader )
{
string $TipoMat = nodeType $sha2;
}

}
ordine;

Now I want sort "nodeType", but the script return phongE shader instead phong.

[```

proc ordine()
{
string $shader[] = `ls -type "lambert" -fl`;
string $newOrder[];
int $i;
string $sortedString[];
string $sha2;
       for ( $sha2 in $shader )
       {
            print $i;
            string $TipoMat = `nodeType $sha2`;
         
            $newOrder[$i] = $TipoMat;
            $sortedString = `sortCaseInsensitive $newOrder`;
            print (" " + $sortedString[$i] + " " + $sha2 + "
");
            $i += 1;
      }
   
//clear $shadersSorted;
}
ordine;

Where is the error?
Can you help me?
Thanks guys](http://forums.cgsociety.org/misc.php?do=bbcode#code)


#2

Sorry, but I have problems to understand your script. Your $shader array contains only lambert shaders. So nodeType should always return “lambert”.
That means that all entries in your $newOrder array will contain “lambert”. But even if this is useful for you, how can it be that the resutlt of the script is “phongE” or “phong”?? To me this seems to be impossible unless I misunderstood something.


#3

Hi haggi


string $shader [] = `ls -type" lambert "-fl`;

this row returns each shader, not only lambert.
anyway if I use


`ls -materials -fl`;

the result do not change.


#4

This works for me and the result is: blinn lambert lambert particleCloud phong phongE rampShader


string $s[] = `ls -materials`; 
string $sa[]; 
for( $sh in $s) 
{ 
    string $nt = `nodeType $sh`; 
    $sa[size($sa)] = $nt; 
} 
string $ss[] = `sortCaseInsensitive $sa`; 


#5

Thanks haggi, this work fine but “$ss” ( type shader sorted) is not synchronized with “$sh” ( name shader)


#6

Not sure what you mean by synchronized. $sh is a scalar, $ss is an array. Do you mean that the two arrays $s and $ss have no relationship any more?


#7

YES, I need relationship between array “$ss” ( type shader sorted) and “$sh” (name shader).


#8

That’s a bit more complicated because the type name string array has no information about the original entries. But you can add this info by yourself like this:


string $s[] = `ls -materials`;
string $sa[];
int $i;
for( $i = 0; $i < size($s) ; $i++)
{
    string $nt = `nodeType $s[$i]`;
    $sa[size($sa)] = $nt + "_" + $i;
}
string $ss[] =`sortCaseInsensitive $sa`;

Now we have a sorted array of node types with information about the original position. We can extract this position info and use it:


string $buffer[];
string $sna[];

for( $i = 0; $i < size($ss) ; $i++)
{
   tokenize $ss[$i] "_" $buffer;
    $sna[size($sna)] = $s[int($buffer[1])];
}

We finally should have a sorted material type array in $ss and the corresponding shader names in $sna. Of course the $ss still contains the numbers, but you can simply create another list where you put the same info but without numbers and sort it as well.