How to execute a script on loading a scene?

How to execute a script on loading a scene?

Anonymous
Not applicable
391 Views
6 Replies
Message 1 of 7

How to execute a script on loading a scene?

Anonymous
Not applicable
I made a little script that switches fog on/off depending on the z position of a camera. (A greenish fog is switched on when the camera goes below sea level...)


callbacks.addScript #preRenderEval "mlbSwitchUnderwater()" id:#mlbUW

fn mlbSwitchunderwater =
(
mlbCamera = $Camera04
mlbFog = getAtmospheric(1)

if mlbCamera.position.z > 0 then
setactive mlbFog false

else
setactive mlbFog true
)



However i keep forgetting to run the script when I load the scene. So I rendered an animation and it turns out the fog didn't do its thing.

Is there a way to embed a script in a scene, so it is executed when the scene is loaded??

MLB
0 Likes
392 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
Have you tried this callback?

#filePostOpen:


I've used the following to add a popup when a file is loaded

clbckExecSTR = "messageBox \""+ message_body + "\" title:\"" + message_title + "\""
callbacks.addScript #filePostOpen clbckExecSTR id:#onload_message persistent:true


Hope this helps.
0 Likes
Message 3 of 7

Anonymous
Not applicable
I tried it. Like so:

callbacks.addScript #filePostOpen "mlbUnderwater()" id:#mlbUW persistent:true

fn mlbUnderwater =
(

callbacks.addScript #preRenderEval "mlbSwitchUnderwater()" id:#mlbSUW

fn mlbSwitchunderwater =
(
mlbCamera = $Camera04
mlbFog = getAtmospheric(1)

if mlbCamera.position.z > 0 then
setactive mlbFog false

else
setactive mlbFog true
)

)


now it tries to execute something when i open the file, but i get this error message:


-- Error occurred in anonymous codeblock
-- Frame:
-- mlbUnderwater: undefined
>> MAXScript Callback script Exception: -- Type error: Call needs function or class, got: undefined <<
-- called in anonymous codeblock
-- Frame:
-- mlbUnderwater: undefined
>> MAXScript Callback script Exception: -- Type error: Call needs function or class, got: undefined <<
-- called in anonymous codeblock
-- Frame:
-- mlbUnderwater: undefined
>> MAXScript Callback script Exception: -- Type error: Call needs function or class, got: undefined <<


Any ideas?

MLB
0 Likes
Message 4 of 7

Anonymous
Not applicable
Now whenever i open the file it tries to execute something. How do I remove the code from the file?

MLB
0 Likes
Message 5 of 7

Anonymous
Not applicable
To remove your script from a file, use

callbacks.removeScripts #filePostOpen id:#mlbSUW
callbacks.removeScripts #preRenderEval id:#mlbSUW


I am looking at your problem, but haven't figured out why it's not working yet (might have to wait for my coffee break, have meetings this afternoon)

EDIT: not sure if the above is correct, but it's callbacks.removescripts that you need to look at in the help for the exact syntax.

EDIT2: There is a box for scripts to be run before rendering in the render rollout, don't know it that's suitable for your code for this one.
0 Likes
Message 6 of 7

Anonymous
Not applicable
Thanks!

I managed to remove the scripts following your suggestion.

Also your sugggestion to use the render rollout works fine!

MLB
0 Likes
Message 7 of 7

Anonymous
Not applicable
Yay! glad it all worked out (and also glad I don't have to fix your script, I hate callbacks, I always manage to make a mess of it)

🐵
0 Likes