MEL Global Procedure mixin.

MEL Global Procedure mixin.

sean_willis
Participant Participant
689 Views
3 Replies
Message 1 of 4

MEL Global Procedure mixin.

sean_willis
Participant
Participant

Hello! I'm looking for a solution to the problem of plugins modifying default MEL scripts upon-installation.

Specifically, the global ones like updateAE.mel or unifiedRenderGlobals.mel that the renderers hook into for custom functionality.

 

This article outlines methodology on how to override / clobber the procedures but not to inherit the prior function to extend the head or tail of it.

Are you looking to rewrite some of our Maya default scripts? - Around the Corner (typepad.com)

 

In python it's super simple: 

def globalFunc():
    print('A globalFunc() called')

global_func = globalFunc

def localFunc():
    print('B localFunc() called')
    print('pre')
    global_func()
    print('post')

globalFunc = localFunc

globalFunc()

Results:

>>> B localFunc() called
>>> pre
>>> A globalFunc() called
>>> post

 

Is it possible to create mixin's like this for MEL global procedures? 

And if there isn't a way to do that is there a way to get read access the global procedures in plain text so they can be cached and executed within the override function?

Thank you for you time.

-Sean

0 Likes
Accepted solutions (1)
690 Views
3 Replies
Replies (3)
Message 2 of 4

brentmc
Autodesk
Autodesk
Accepted solution

Hi,

There is no MEL equivalent to the Python example.

However, you can achieve something similar in MEL with a bit more work:
1) locate the original MEL file's location using the whatIs command
2) reading the contents of the MEL file into memory
3) modifying the global proc name in the script
4) calling eval to register the new global proc 

Here is an example that makes a copy of the global function doGroup called doGroup_Original

{
    string $function = "doGroup";        
    string $what = whatIs($function);
    string $prefix = "Script found in: ";
    if (startsWith($what, $prefix)) {
        string $filename = stringRemovePrefix($what, $prefix);
        string $script = freadAllText($filename);
        string $regex = "global[ \t]+proc[ \t]+" + $function + "[ \t]*[(]";
        string $newScript= substitute($regex, $script, "global proc doGroup_Original(");
        eval($newScript);
    }
}

 

Brent McPherson
Principle Engineer
Message 3 of 4

sean_willis
Participant
Participant
Thank you very much this is great!
I found out updateAE has a callback that can be registered.
I will certainly use this for all the others scripts that do not have accessible builtin callbacks.
0 Likes
Message 4 of 4

brentmc
Autodesk
Autodesk

Yes, it is better to use callbacks. I'm not sure I would recommend overriding builtin scripts this way but it was an interesting experiment to see how this could be achieved in MEL. 🙂

Brent McPherson
Principle Engineer
0 Likes