PDA

View Full Version : mel listener?


jgibz
04-22-2012, 05:21 AM
Hi,

Is it possible to call a function when a command is executed?

For example, if I have a function that hides an object and adds it to a textScrollList, can I call that function when the HideSelectedObjects command is used?

Likewise, if I undo the HideSelectedObjects command can I remove the item from the list?

thanks.

Nyro
04-22-2012, 07:15 AM
There are no function listeners in MEL. The closest you get to a listener object is a scriptJob. You could create one that is triggered on a condition, e.g. pseudocode: if( isVisible(myObject) == true ), though I haven't gotten scriptJobs to work with conditions personally. I may be doing it wrong though.
The alternative would be a scriptJob that runs all the time during Maya's idle phases, which is basically a listener as it would immediately notice if an object was hidden. Obviously, it's not advisable to have hundreds of these scriptJobs running at once.

NaughtyNathan
04-22-2012, 10:22 AM
one other approach is to overload the function.

Basically, write a new global HideSelectedObjects() proc and add your own code to it. this way whenever you source/execute your main script your version of HideSelectedObjects will be used over Maya's original.

this is not a particularly great approach in general as there may be many different functions to overload, or the functions are actually internal commands you simply cannot replicate, rather than mel scripts or runtimes... but it may suffice purely for this scenario...

Nyro
04-22-2012, 11:34 AM
one other approach is to overload the function.

Is that official terminology? Because I think I just learned something here :)

ginodauri
04-22-2012, 11:47 AM
http://en.wikipedia.org/wiki/Function_overloading

Nyro
04-22-2012, 02:27 PM
Hmm, seems like it officially means something different than what Nate describes in this particular case. None the less, good to know. Just out of curiosity, overloading isn't supported in Python, is it?

haggi
04-22-2012, 03:43 PM
No. Because you don't have explicit types in python, the funtion itself always get an object, not a float, int or string. Of course you can do it yourself in the method or function if you simply check the type of your argument.

NaughtyNathan
04-22-2012, 04:13 PM
yeah, I use the term "overload" in it's bastardised form! :)
It's not true overloading as MEL doesn't support anything like this, but when talking about MEL and scripts I always avoid the (perhaps more accurate) term: "overwrite" as this could be taken to mean that I'm suggesting you should physically overwrite the actual script file in question (which would obviously be very bad!)

Nyro
04-22-2012, 04:19 PM
I'm sure it wouldn't take more than 10 minutes to go online and find somebody to send you the messed up .mel file in question :)

But that idea is definitely interesting, I had never thought of doing that. Will keep it in mind the next time I want to add functionality (instead of doing something from scratch)

jgibz
04-22-2012, 07:07 PM
Thanks for the suggestions, I'll try them out.

jgibz
04-22-2012, 07:12 PM
write a new global HideSelectedObjects() proc and add your own code to it. this way whenever you source/execute your main script your version of HideSelectedObjects will be used over Maya's original.


This works, but is there a way to tell maya to use its native function after overloading?

I guess you'd just rewrite the native function and add your own conditions to it.

NaughtyNathan
04-22-2012, 08:12 PM
Unfortunately there isn't any simple way of reverting back to Maya's original function without restarting Maya. However, if the function you've replaced is from a MEL script, you can simply re-source the original file. The trick is, before you do anything, start by doing:
string $orig = `whatIs someMayaFunction`;
// Result: Script found in: C:/Program Files/Maya/scripts/blahblah.mel //

which will tell you what and more importantly where the original script file is. If you later need or want to reinstate Maya's original function you just source the script returned in $orig (you'll have to do a bit of checking and string extracting on it of course, but this is pretty easy)

jgibz
04-22-2012, 08:48 PM
Sweet, thanks :bowdown:

jgibz
04-23-2012, 01:37 AM
It seems calling the command within the overload function calls the native function


global proc HideSelectedObjects()
{
print "Overload Function";
HideSelectedObjects; //hides the objects

}

CGTalk Moderation
04-23-2012, 01:37 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.