Home Technical Talk

MEL: matrix creation

polycounter lvl 14
Offline / Send Message
fade1 polycounter lvl 14
i have question regarding matrices in MEL. i want to store vertex color information for a mesh in a matrix. as i don't know the size of the matrix, i need to query the vertcount, which will define the dimension of the matrix. somehow MEL does not support variables to define matrix sizes like this example:
int $a = 1; int $b = 2;
matrix $myMatrix [$a][$b];
anybody knows a solution for this?

Replies

  • SyncViewS
    Options
    Offline / Send Message
    SyncViewS polycounter lvl 13
    Hi, as a disclaimer I never coded a single statement in Maya, anyway, if I get the issue right, it is a general programming task.

    If you need to store the color of each vertex of a variable size mesh, the best solution, in my opinion, is to use an array of arrays. It means you create an array as big as vertex count if in Maya you must specify its size on definition, then fill it with 3 or 4 values arrays (depending on the data you need to stor RGB or RGBA) each of which represents the color of the corresponding vertex. I guess you can get access to them in MEL by the double subscript operator: vertColors[vertIndex][channel], or simply by vertColors[vertIndex] to get the corresponding vertex color array.

    I hope this helps, anyway.
  • fade1
    Options
    Offline / Send Message
    fade1 polycounter lvl 14
    @SyncViewS
    right. that's what i'm doing right now. i don't want to mix two problems here, but if i do this i have problems returning the data from an procedure. as far as i know, i just can return one variable from a procedure. if i store my vert data into four float arrays, i have four variables to return, which is something i couldn't get to work in MEL yet. correct me if i'm wrong.
    i'm still mainly an artist, just doing some scripting for supporting our workflow, so sorry for lack of knowledge here and there. ;)
  • SyncViewS
    Options
    Offline / Send Message
    SyncViewS polycounter lvl 13
    Hi, I have to reiterate I don't know MEL, so if anyone proficient with it wants to jump in is more than welcome. Again the issue doesn't seem to be a language specific thing.

    Why do you have to return a single channel value from your procedure? Cannot you return the full color array for a selected vertex - it would be like a single value, or the whole array of arrays? In any language I know, that's possible, usually by pointer or reference (even if I don't know if MEL uses such addressing, should be feasible too).

    If MEL does support structures or classes (it should), you could create a class to store the data types to be returned by the procedure, then use an instance to return needed values, but this could be a little more daunting and not really needed if the script is simple and data is not much complex, like RGBA values.

    Any MEL master would be of great help at this point, I've stretched to the limit of my guessing knowledge on this :)
  • PolyHertz
    Options
    Offline / Send Message
    PolyHertz polycount lvl 666
    Why do it that way? Just have a matrix with 3 (RGB) or 4 (RGBA) arrays in it, with each of those arrays being the size of the number of verts you'll be adding the color values from. Or get their hex colors and use a single array instead.
  • fade1
    Options
    Offline / Send Message
    fade1 polycounter lvl 14
    PolyHertz wrote: »
    Why do it that way? Just have a matrix with 3 (RGB) or 4 (RGBA) arrays in it, with each of those arrays being the size of the number of verts you'll be adding the color values from. Or get their hex colors and use a single array instead.

    but that's not working in mel. when i try to set the matrix size with a variable, maya gives me an error. it just let's me define the matrix with absolute numbers, like matrix $myMatrix [10][3]. of course i can't set the size, before i query the vert count of my mesh.
  • Whargoul
    Options
    Offline / Send Message
    Whargoul polycounter lvl 18
    Matrices in Maya basically suck for that exact reason (can't be resized, must have fixed size at creation). Why not use an array of vectors instead? A vector is a good match for colours. Alternatively you can just use an array of floats 3x the size of # of verts.
    int $numVerts = `blah`;
    float $colArray[3*$numVerts];
    for ($i=0;$i<$numVerts;$i+=3)
    {
        $colArray[$i] = r component of colour
        $colArray[$i+1] = g component
        $colArray[$i+2] = b component
    }
    

    if you want the 312th vert's colour - $colArray[3* 312 +0], $colArray[3* 312 +1], $colArray[3* 312 +2]

    Or use vectors, which is easier:

    vector $vecArray[$numVerts];
    vecArray[123] = <<colour.r, colour.g, colour.b>>;

    Doing this off the top of my head, so the syntax might be off ;)
  • Whargoul
    Options
    Offline / Send Message
    Whargoul polycounter lvl 18
    Oh yes, MEL doesn't have structs either in case people were wondering :(
  • fade1
    Options
    Offline / Send Message
    fade1 polycounter lvl 14
    @whargoul
    good to hear that matrices are limited in mel. in this case it wasn't my lack of knowledge. ;)
    and those ideas for a workaround are great. i think to store eveything in one array is best, as i can pass the alpha, too...thanks a lot.
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Yep, every time I end up thinking I want to use a matrix in MEL, I swiftly come to the conclusion that I really don't. I've tried various things but they are very limited.

    Whargoul's suggestion is pretty much what I end up doing every time I want to do "multi-dimensional arrays" in MEL now. Really makes me miss maxscript for having structs and the capability of having arrays as elements in another array. Could make stuff so much easier.

    Might be worth looking into Python instead? :)
Sign In or Register to comment.