mel Timer

mel Timer

Anonymous
Not applicable
1,523 Views
7 Replies
Message 1 of 8

mel Timer

Anonymous
Not applicable
I want to create a timer in dependant from the time line, that will start timing on a certain action.
Does anyone have any suggestions, I started looking at dgtimer but i have no clue how it works.
0 Likes
1,524 Views
7 Replies
Replies (7)
Message 2 of 8

tkaap2
Autodesk
Autodesk
For a simple task, take a look at the timerX command. (http://download.autodesk.com/us/maya/2008help/Commands/timerX.html)
To get it to start when you do a certain action (playback, some modeling change, some rigging change, etc), make a quick MEL command that will start the timer, do your action, then stop the timer. Use this function instead of your usual action for doing your task.

global proc float sampleTimer()
{
float $t0 = `timerX`;
//do action
polySphere;
float $elapsedTime = `timerX -startTime $t0`;
return $elapsedTime;
}


The dgtimer command is mostly for a much much more detailed printout of where Maya is spending its time. It's very useful for figuring out which nodes and plugins need optimization when using a massive rig with thousands of nodes.

To use that, you need to first turn it on, reset it, do something, turn it off, then query the timer for the information.
dgtimer -on -reset; //(turn timing on and reset)
polySphere; // do something
polyCube;
dgtimer -off; // turn timing off
dgtimer -outputFile "/tmp/dgTimerFile.txt" -query; // the "query" flag usually needs to go at the end for commands that have other flags that take arguments

You can also have this output to the Script Editor, but there's so much information that having it in a text file is pretty useful.

-Tony
0 Likes
Message 3 of 8

Anonymous
Not applicable
Nice, Thank you it helped a lot, is it possible to start timer from a mouse click down?

timeControl
-e -pressCommand
0 Likes
Message 4 of 8

tkaap2
Autodesk
Autodesk
print `scriptJob -le`;

Will show you what events can trigger a script to start by using a scriptJob. Look through that list to see if any of them will work for what you have in mind.

-Tony
0 Likes
Message 5 of 8

Anonymous
Not applicable
So i found the "scriptJob -timeChange" that seems like what i need but for the life of me I cant get it to work. Do you have a working example by any chance.
0 Likes
Message 6 of 8

tkaap2
Autodesk
Autodesk
From your previous comment, it looks like you're trying to get the procedure to run when you click on the timeline. The docs for the scriptJob command say that the -timeChange flag won't work in that specific case, and that you ought to use the "timeChanged" event instead. It's a subtle difference in setup, but the results should be the same.

Try this instead. This will tell Maya to run the "exampleProcedure" function whenever the time changes, except during playback.
-Tony

global proc exampleProcedure(){
print "Hello World\n";
}
int $job = `scriptJob -e "timeChanged" exampleProcedure`;
0 Likes
Message 7 of 8

Anonymous
Not applicable
hey Tony thank for the help
just wondering if you don't mind me asking what's your background and what do you recomend I do to improve my mel
I'm really having a hard time going from C++ to mel
0 Likes
Message 8 of 8

tkaap2
Autodesk
Autodesk
No problem. My background is lots of computer science classes, plenty of C, C++, Java, Scheme, and any other languages that the professor felt like using.

Rather than thinking of MEL as a real language, it helped me to think of it as a macro language. Nearly everything in Maya is started from Maya. The entire UI works as a MEL layer on top of the Maya engine, which does all of the work. If MEL is a macro language, then you can use the best technique for learning a macro language: For any task you want to script, do it once manually, then examine the syntax and commands that Maya uses to do it.

In the Script Editor, turn on History -> Echo All Commands. It will make your script editor pretty verbose, but you can find how Maya calls its functions, then edit those into a script to automate that task. That, plus the MEL and Commands areas of the Maya docs, is how I learned 90% of the MEL I know. ( over the course of 6.5 years...)

Apart from that, the two hardest parts of MEL for me there these:
1) MEL commands are usually called this way: (with optional flags individually specified)
commandName -flag arg1 -flag2 arg objectName;

but MEL functions are usually called this way: (with all arguments mandatory and in order)
functionName (arg1, arg, objectName);

When capturing a return value, these differences come into play again:

string $names[] = `commandName -flag arg1 -flag arg2 objectName`;
string $names[] = functionName (arg1, arg, objectName);

Commands have to be put inside backticks, which work like the "eval" command. You can, of course, put the command inside the eval function if you want, and then call it like a function.

2) The second weird part is return value. The return value isn't always intuitive. Sometimes it's easiest to just call the function once and see what the result is. In a decidedly non-C++ sort of way, It is possible for the return type to -change- depending on the current scene. Some commands can return an integer one time, then a string array next time.

Most functions that create nodes will return a string array, in order to eliminate that kind of confusion. In order to just have the string name of the node that was created, you need to reassign it to another variable.

string $names[] = `polySphere -sx 10 -sy 10`;
string $newName = $names;


Bonus - one more that I remembered:
The Script Editor acts like an open interpreter session - it remembers the variables that are declared in the top scope the whole time that a Maya session is open. If you re-declare a variable as a different type after declaring it, Maya will complain. To avoid this, you can write your whole script in braces:

This will not cause the redeclaration error...

int $names = 4;
{
string $names[] = `polySphere -sx 10 -sy 10`;
string $newName = $names;
}

Bonus Bonus - you don't have to empty out the Script Editor each time you run a script. You can accumulate several test scripts at the same time. You can highlight the few lines you want to execute, then hit Ctrl+Enter to run those without them disappearing from the input box area.


-Tony
0 Likes