MEL procedure with any number of arguments?

423 views
Skip to first unread message

Fredrik Averpil

unread,
Dec 30, 2013, 5:09:26 PM12/30/13
to maya...@googlegroups.com
Hi,

I'm trying to define a procedure in MEL which would accept any number of arguments... but I can't find anything on this in the docs. Any ideas?

Regards,
Fredrik

Ryan O'Phelan

unread,
Dec 30, 2013, 6:10:17 PM12/30/13
to maya...@googlegroups.com
Maybe this would help?



Writing it in python might be easier, using *args, or **kwargs, etc. 

Ryan


--
You received this message because you are subscribed to the Google Groups "maya_he3d" group.
To unsubscribe from this group and stop receiving emails from it, send an email to maya_he3d+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Fredrik Averpil

unread,
Dec 30, 2013, 6:18:59 PM12/30/13
to maya...@googlegroups.com
Well, I'm making a pre/post render script consolidator. Basically, give it any number of scripts, be it Python or MEL and execute them:

So in order to keep it simple, I'm writing it in MEL, so that all you need to do is to keep the RenderScriptConsolidator.mel file in the MAYA_SCRIPT_PATH and execute Maya with -preRender "RenderScriptConsolidator(\"c:/somepythonscript.py c:/fixstuff.mel c:/anotherscript.mel\")" scene_file.ma

But right now I don't know if it's even possible to feed the proc an unknown number of arguments (such as with *args or **kwargs in Python). So right now I'm using one string... which is just not as nice. :) But it works. Ideally I would have wanted to call the proc like this, without specifying the number of arguments the proc would accept:
RenderScriptConsolidator("C:/somescript.py", "C:/someotherscript.mel", "C:/yetanotherone.py");


// Fredrik

David Johnson

unread,
Dec 30, 2013, 7:25:07 PM12/30/13
to maya...@googlegroups.com
As far as I know it is not possible to use varying arg lists in mel. But the link that Ryan posted shows two good, and commonly used workarounds. If you do not like tokenizing a single string, then try the string array approach (example 2 in that link). Your code would then look something like this...


RenderScriptConsolidator({"C:/somescript.py", "C:/someotherscript.mel", "C:/yetanotherone.py"});

and your proc would be...

global proc RenderScriptConsolidator(string $args[])

I've used this approach, along with an option parsing proc so that can I can process keyword args as well...

RenderScriptConsolidator({"preScript1=C:/somescript.py", "preScript2=C:/someotherscript.mel", "postScript1=C:/yetanotherone.py"});


David

Andres Weber

unread,
Dec 31, 2013, 10:23:47 AM12/31/13
to maya...@googlegroups.com
The last thing you can do is the "Builder Design Pattern".  Basically the idea is to create an object (in your case, create basically a dictionary) that you can then parse to create an object.

So the way I've seen it done is basically passing a string to your mel function:
string $argsv="-testVal 4 -otherVal 30";
global proc test(string $argsv){
    int $testVal = argParse($argsv, "-testVal");
}

You write the parser to basically look for the flag, match the string and then pass the resulting value and detect the type of value.  You'd also write a function which would create this string more efficiently as well possibly.  Then you can reuse it for every single thing you want.

Here's a little reading you can do on the subject:
http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern

But yes, as everyone said Python would DEFINITELY make your life easier with dictionaries/better data handling.  Good luck!
(Special thanks to our R&D guy for letting me know about the words "builder pattern" in order to sound smart haha.)

Fredrik Averpil

unread,
Jan 1, 2014, 6:49:24 AM1/1/14
to maya...@googlegroups.com
Hi David,

That's an elegant solution. I'm going to try that. Thanks.

Regards,
Fredrik

Fredrik Averpil

unread,
Jan 1, 2014, 6:51:52 AM1/1/14
to maya...@googlegroups.com
Hi Andres,

Interesting read. Thanks!

Regarding writing it in Python, that's what I would have wanted to do. But there's no way to call a Python script immediately from the command line (e.g. -preRender), as far as I know. That's why I'm bothering with MEL to begin with :)

Regards,
Fredrik
Reply all
Reply to author
Forward
0 new messages