Notifications
Clear all

[Closed] Script sometimes not updating

I have a script that references quite a few scene objects and builds a few arrays dependent on the current animation in the scene.
I’m finding that MAXScript seems to skip building some arrays and thus the rest of the script falls over. :hmm:
I can manually go through and evaluate line-by-line but that is boring. :rolleyes:

What can I use to force MaxScript to eval?

6 Replies
1 Reply
(@bobo)
Joined: 11 months ago

Posts: 0

Something is wrong with your script
When is the evaluation supposed to happen?
Have you checked for scope issues (problem number one in general)?
Can you post any example of what you are doing and where it is failing?

In general, always assume you are doing something wrong before blaming the language, I have discovered over the years that it is the only healthy way to produce working scripts

Thanks Bobo

Learning stuff is fun but filled with little setbacks. I’m quite used to being wrong. :shrug:

 Might be easier to attach the whole thing ... 

It’s not a finished piece by any means … but it works quite well when it does…

1 Reply
(@bobo)
Joined: 11 months ago

Posts: 0

Usually if a script does not run well the first time but does run the second time, there is a scope of variables problem somewhere. It is hard to tell from just looking at it since I don’t have a scene that matches what the script expects to actually run it, but the fact half of the variables are in global scope is already a sign of trouble. Please always encapsulate anything you do in a local scope, starting with ( in the first line of your code and ending with ) at the end. You don’t want your COG and Hips in global scope, imagine if I had a script that assumed these as local – it would collide with yours. If you DO have to use global variables for some reason (either for UIs, inter-script data exchange or library functions), make sure the names are long and unique.

Scope … hmmm … need to do more research…

local or global … (nothing to do with global warming? )

My script has been slapped together from a couple of request from my students and my own animation experience. I’m usually a mouse driver and only recently started peeking under the hood.

With this script I would run a test, see if it would work and then re-load the scene.

It would fail because half the shortcut were looking at what was now a deleted object.

I would run it again and watch it fail go back and line-by-line it and she would be happy again. :applause: I was and I wasn’t.

I’m generally pleased with my progress so far (considering I’m a complete noob ) and had my rig climbing stairs this morning with only five minutes work after running the script.

Thanks for pointing me in a fresh direction.

Thought I might add the max scene I’m working with.

Now we are talking – having a scene really helps.

When I load the unanimated scene and run the script, I get immediately an error about Hip_Switch being undefined (cannot multiply 5 and undefined).
And it IS a variable scoping issue.

Here is the short story:

When you run a script that is not enclosed in (), any variable defined in it becomes global if it is evaluated in that top scope. But anything inside a function or other expression defined inside () will produce implicitly a LOCAL variable which will live only as long as that local scope exists (which is usually only while it is being executed).

So while most of your variables like COG and Hips become global immediately, Hip_Switch does not because it happens to be undeclared before the IF that assigns its value, and when the IF runs either the THEN or the ELSE block, that turns out to be the very first level of () below global and the variable becomes local. The moment the IF is done, the variable is gone from memory and undefined again. 

There are several ways to fix this, but the REALLY GOOD way is to just put one ( in front of your whole code and another at the very end, making the whole run in its own local scope. All variables will now be implicitly local, and the Hip_Switch will end up in the top-level local scope, remaining visible to any code after its definition and assignment. The only drawback of this approach is that each line of the code won't print its result to the listener, so if you want the debug your code, you would have to use format() and print() statements to output what you want.

Alternatively, without using the local scope, just removing the remark from the line
-- 		Hip_Switch = 1
found before the IF statement would pre-declare the variable as global once and for all before its value is modified and then later used by the code.

Yet another alternative would be to say

    		Hip_Switch = if StrideStart == 1 then 1 else -1
which would both make a global variable and assign to it the value without any scope issues.

But I really prefer the "all code in local scope" approach because it is how it is meant to be done.

Hope this helps.

That does help … I’m sure it does … :argh:

One day I will get to grips with MaxScript grammar.

I have trouble enough with Inglish. :drool:

But I think I’m heading in the right direction…

If only I would not keep hitting those walls :banghead:

Thanks for your prompt help and encouragement.