[Maya MEL] Slicing to a list/array?


#1

Hi

Is there a way to slice a list/array in Maya? Much the same way as Python.

For instance, with a list/array of:
$data[] = {“Cube”, “Box”, “Cylinder”, “Cone”, “Sphere”};

I want to exclude the first entry which is “Cube” in this cube resulting to a new list of
{“Box”, “Cylinder”, “Cone”, “Sphere”}

In Python, this would be something like data[:1] (Correct me if I’m wrong).

I think there is no such direct command in Maya but is there an alternative?

Thank you for looking at the problem.


#2

in python it would be data[1:] the 1 is defining the start index, and the lack of a number after the : means just go to the end of the list.

but in mel i am no longer familiar how its done, but i think you have to do something like create a new array using the new length, so in your case old length - 1, then you would need to loop over the first one ignoring the index’s you don’t want.

But if you can avoid mel and use python you are best off to do so, the cmds module of python contains pretty much everything mel has.


#3

Hi @passerby
Thanks for the response but I am having a problem. Can you take a look at this code:

$array[] = {5,4,3,2,1};
$size = size($array);
		
for($i=1;$i<$size;++$i) {
	$new_array += $array[ $i ];

}

print $new_array;

#4

Are you getting a particular error with that code? (Been years since I used mel so some extra context would be helpful)


#5

I don’t think mel supports += on arrays.

How about something like this:

int $array[] = {5,4,3,2,1};
int $size = size($array);
int $start = 1;
int $end = 3;
int $result[] = {};
for ($j = $start; $j <= $end; $j++)
{
    $result[size($result)] = $array[$j];
};

print $result;

It would be easy to turn it into a slice-like function.

But why mel at all? It’s 2019!


#6

Possibly stuck with Maya LT? :frowning:


#7

Thank you for the response @Theodox
It works as expected

RE: why mel at all
I’m trying to have a quick fix on an old script. Will start posting Python question soon. I promise! Thanks again! :slight_smile:


#8

quick fix

You’ve been asking months of MEL questions… by choice? eww. (#kidding-not-kidding)