MEL How-To #49

Back · Previous · Next Maya

How do I run a MEL script as a background process?

Well, you can't, really. As long as Maya is executing MEL commands it disallows user input.

Now, there are some sneaky ways around this. At H2O we've implemented a ProgressBar plug-in (just a simple "n% done" window with the typical growing bar) that can be used to track the workings of a time-consuming script. Because the plug-in implements the window in its own thread, a side-effect is that it is possible to manipulate Maya's UI while the MEL script is twiddling the progress bar. This is risky, tho', as if we select or delete something that the script is working on it could cause the script to come screeching to an unceremonious halt.

I suppose if you tried hard you could make a script function that executed in spits and spurts by controlling it via scriptJobs and the like:

// Set up a scriptJob to continue the script task whenever Maya is idle
int $backgroundJob = `scriptJob -idleEvent "continueMyScript()"`;

global proc continueMyScript()
{
  global string $whereWasI;

  eval $whereWasI;
}

global proc spitsAndSpurts( int $step )
{
  global string $whereWasI;

  switch ( $step )
  {
    case 1:
      // Do step 1 here
      $whereWasI = "spitsAndSpurts 2";
      break;

    case 2:
      // Do step 2 here
      $whereWasI = "spitsAndSpurts 3";
      break;

    // and so on...

    default:
      // Done!
      print( "spitsAndSpurts is DONE!\n" );
      $whereWasI = "";
      scriptJob -kill $backgroundJob;
  }
}

Again, a caution that if the MEL script is working on something within the scene, you can't (well, shouldn't) change anything until it's done what it needs to do with it.

Tuesday, December 05, 2000