PDA

View Full Version : Utilizing Python Threading in Mel


Impus
08-27-2008, 12:46 PM
Thought some of you might be interested in this:

There’s been many times when I wanted to latch a command onto a timer that runs independently of the main thread - still allowing control and interaction with maya while the timer is ticking.

The best example of this is an auto scene backup script. Most, if not all, auto scene backup scripts out there latch onto some arbitrary events in the scriptJob command and execute the backup time check when these are triggered (selection change, tool change etc).

This typically does the job but can lead to some funny behavior depending on the events you’ve attached the backup to.



How could it be better?

In any other programming language that supports threads or even attaching timers to UI elements (ala maxScript) this would be very simple and is also simple enough problem inside a maya plugin - spawn a new thread, wait the given time then execute the command.

However I figured with Python’s implementation into Maya and it’s native ability for threading I should be able to achieve this without having to develop or distribute a compiled plugin.



It turns out it’s very simple.


[read tutorial] (http://www.rodgreen.com/?p=191)

DOor
08-27-2008, 04:17 PM
Good link, thanks for the info!

berniebernie
09-03-2008, 03:08 PM
Hey there I thanked you on your blog, and here once again =0


I've got a small question regarding threading. Do you know if the command can 'override' maya and force a command ? Here's an example, which works well, but only if you are not click-dragging the mouse (or rotating around the scene):


global proc startTimer(int $runTime, string $command)
{
global int $stopped;
if(!$stopped){
python("timer.startTimerObj( " + $runTime + ", \"" + encodeString($command) + "\")");
}else{
print("Stopped the loop!\n");
}
}
global proc repeat()
{
global int $stopped;
string $command = "headsUpMessage -time 2.0 \"2 seconds.\";print(\"Working...\n\");";
//string $command = "print(\"Times Up!\n\");print(\"Hello World!\n\");";
startTimer(3, $command + "repeat;");
}


And here's the code I use to 'control'


python("import timer"); // source the script at launch
repeat; // launch the proc
$stopped = 1; // set to 1 or 0 if I want the loop to stop/start


Now I've got the feeling that it doesn't have to do with the threading specifically, but any insight would be more than welcomed.



thank you

Impus
09-03-2008, 06:49 PM
You're only able to execute maya commands inside the main thread and so you wont be able to 'execute' the repeat command without the main thread being the one to call it.

If you're dragging the mouse, saving or any other action that will tie up the main thread this will block the execution of the command.

As far as I know there's no way to override this :S sorry.

berniebernie
09-04-2008, 08:37 AM
Oh thanks for the heads up.

I hope you come up with more goodies !

matt

CGTalk Moderation
09-04-2008, 08: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.