Simply Maya User Community

Simply Maya User Community (https://simplymaya.com/forum/index.php)
-   Programming (https://simplymaya.com/forum/forumdisplay.php?f=32)
-   -   Execution order? (https://simplymaya.com/forum/showthread.php?t=3205)

kbrown 14-01-2003 10:02 AM

Execution order?
 
Is there a way to tell maya which expression gets executed first?

My Stormy Ocean project is full of expressions and one of them contains some global variable initializations. Now every time I load the scene I get a bunch of errors because some of the expressions, which uses those global variables, gets executed before the variables are defined.

I've worked my way out of this by going through the expressions and hitting the edit button in the expression editor (the global thingie first). It's not a biggie but kind of annoying. :banghead:

I hope you understood...

:beer:

mark_wilkins 14-01-2003 10:41 AM

Nooooooo

you can't rely on expressions being executed in any particular order.

The way to do this is to make a script node with your initializations and set its execution attribute to run the script on scene open. Then, you're guaranteed that everything will end up initialized.

However, I'm curious -- why not use custom attributes instead of global variables? This would allow Maya to be more intelligent about recalculating your expressions -- in fact, I'm surprised you can get away with using global variables that way at all, because if the global variable changes value there's nothing (meaning no connection in the DG) to let Maya know that all the dependent expressions need to be recalculated, so it must just recalculate all of them every time...

-- Mark

Kevin 14-01-2003 10:52 AM

nice answer mark!

say mate just noticed you book is only 34.97 at amazon!!

I will be purchasing next week - say the only trouble is ..if i ever bring out a tutorial that goes over mels ...you will be thinking I ripped you off!

lol

Still not to worry mate if it ever comes to that I can always give you a good plug for your book!

kbrown 14-01-2003 12:48 PM

Thanks for the prompt answer Mark. I knew I was missing something. I think I now know how to do it properly.

Why do I use global variables? I use them to store some global information like wind direction which is calculated in an expression based on a helper object's (an arrow) rotation. My particle system and fields uses then this information extensively. I got this idea from the Secrets Of The Pros book. It's true I could use custom attributes directly but I prefer it this way (at least for this kind of implementation). I don't know if it's any faster but at least it keeps the channel boxes clean :)

mark_wilkins 14-01-2003 07:27 PM

Remember, custom attributes do not have to appear in the channel box, nor do they have to be keyable. If you make them non-keyable, they won't appear in the channel box in any case. Also, if you use the addAttr command to make them, you can use the -hidden option which will leave the attribute keyable but will keep it from appearing in either the attribute editor or the channel box.

Let me be more clear about why using global variables in this way concerns me: Maya does a fairly complex analysis of the connections between nodes in your scene to make sure that it's recalculating what needs to be recalculated for each frame. If the *attributes* connected to an object expression node (which is not strictly applicable to your case because you're using particle expressions, which are different) do not change, that expression will never be recalculated, even if the expression itself depends on data you've gotten from other kinds of sources, like from a call to a MEL command like getAttr (which does not form a connection in the DG to your expression node) or from a global variable.

Also, Maya uses these connections to figure out in what order to execute expressions. When you use global variables or getAttr to grab data from elsewhere in the scene, Maya can't always figure out how to correctly evaluate the scene, which is what you've experienced. This means that when your scene does update it may update incorrectly.

The reason you've not had the first kind of problem (where things don't update at all, instead of updating incorrectly) is that you're using this approach with particle expressions, which are guaranteed to run every frame for every particle, instead of with object expressions, which are not so guaranteed. Even so, you still can't count on your scene being evaluated in any particular order unless you have the DG connections in place to let Maya know that it needs to evaluate things a certain way -- and that might mean making custom attributes.

There may be a safe way to use global variables in this way, and I have no doubt that it works some of the time, but I'm not sure that I know what the preconditions are for such an approach working and I VERY much doubt that Secrets of the Pros discusses that aspect of the problem. :)

-- Mark

kbrown 14-01-2003 10:05 PM

:wow: I stand corrected.

I tried the script node approach you mentioned earlier and ran in to other trouble. I now believe you're totally right. Many thanks for the time you spent answering.

Trial and Error is my second and third name. You just got to push every button and see what happens :D

Thanks again,

mark_wilkins 14-01-2003 10:59 PM

well, let us know what you figure out!! :-)

I should experiment a bit with the global-variables-and-expressions thing just to see what I can find out about using them...

-- Mark


All times are GMT. The time now is 02:59 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Simply Maya 2018