Notifications
Clear all

[Closed] Strange problem of Scope ?

I have a strange problem with a macroscript which basically call a scripted render effect : monochrome.
The render effect is in the startup script directory and is visible in the add effect window.
The macroscript is executed via a toolbar button.
In the mcr I call the effect via the addeffect (monochrome()) command in the on execute event handler.

I launch max to be sure all variable are reseted.
When i run the script i receive :
Error Exception: – Type error: Call needs function or class, got: undefined
OK, i can understand that the function monochrome() is undefined in the scope of the macroscript, but as the scripted effect is in the startup directory, the monochrome() should be global ?

Funny things : if i relaunch max (to reset again the variables), open the mcr in the editor and Evaluate it, after i can run it and evrything is fine.

After diging a while i came with this nasty workaround :
I create a simple script which contains the addeffect (monochrome()) command and call it in the mcr (instead of the command) with getfilein and again evry thing works like a charm except that i must create this nasty little script.

So why this strange behaviour which for sure is related to scope of variables ?

Thanks

2 Replies

the reason is quite logical. basically the macroscript is being defined before your startup script which means that “monochrome” will be interpreted as undefined in your macroscript. type “Startup scripts” in the reference search and click on the first result for info about the order that scripts are loaded.

to solve your problem you have 2 options.

  1. add a double colon :: in your macroscript before ::monochrome like that. The double colon forces maxscript to look in the global scope for a variable by that name.
  2. move your script to a location that gets loaded before the macroscripts as defined in the reference topic mentioned above

Thanks, the double colon did the trick.
We learn each day