Notifications
Clear all

[Closed] Execute function when dialog gets focus

Hi,

I’m currently creating a script in which I have a dropdownlist which lists the cameras in the scene. The list is filled by a function.
But, when you have started the script, and then created another cam, the list is outdated. So what I would like to do, is have the function processed each time the script window gets focus, so that the list is updated nicely without having to restart the script.
Any ideas on how to do this?

Thanks!

7 Replies

an easy way would be to implement an ‘update cameras’ button.
or you could use a timer to check the cameras every second or so…

something like:

rollout RO_cameras “cameras”
(
local cams=#()
dropdownlist ddl_cams “Cameras”
timer cam_timer interval:1000
fn updateCameras =
(
cams = for c in cameras where classof c !=targetObject collect c
ddl_cams.items=for c in cams collect c.name
)
on cam_timer tick do updateCameras()
on Ro_cameras open do updateCameras()
on ddl_cams selected i do select cams[i]
)

Thanks for your answer.
I think that I’ll go for the update button, seems by far the simplest solution, and it’s not so much more ‘work’ for the user in the end.

Oh and thanks for that bit of code too. I didn’t know about the ‘collect’ statement before, that seems quite useful.

I’d recommend using a #nodeCreated callback instead, and have the callback string test whether the new node is a camera. Your function for updating the listbox will need to be global to be called from the callback. Below is an example:

(
local cbString = "if ((superClassOf (callbacks.notificationParam()))==camera) do (MyUpdateFn())"
callbacks.addscript #nodeCreated cbString id:#MyToolsTempCB
)

You will need to make sure that this callback exists only while your script is running. If you don’t remove it before your script finishes closing, it will cause an error when the next camera node is created, as MyUpdateFn() will no longer be defined/functional. Below is an example of what needs to be in your script’s on…close do () event handler:

on MyScriptRollout close do 
 (
 callbacks.removeScripts id:#MyToolsTempCB
 )

That sounds great!
I’m going to look into callbacks today to see if I can get it working. (I’m rather new to maxscript)
Thanks a lot

It looks like I’m really missing some point somewhere, but I can’t get the on <rollout> open do and on <rollout> close do to work. I’ve tried it in the most basic script, but I always keep getting the following error:

  Rollout:testRollout
     true
     -- Type error: Call needs function or class, got: true
     -- Syntax error: at end, expected while
     --  In line:
rollout testRollout "test" (
     )
     
     CreateDialog testRollout
     
     on testRollout close do (
     	print "closed"
     )

EDIT: I’ve solved the problem. The on close should be inside the rollout declaration.

And another thing, how do I make sure a function becomes global?

Yes, and you don’t want the callback created when the rollout is “opened” or “rolled out”. (Maxscript has some confusing and overlapping terminology here.) You want to create the callbacks when your script loads and only have them removed/deleted when the on…close do () event handler runs.

There are 2 ways to make any variable or function exist in the global scope. The first is implicit declaration, which should be used only while briefly testing fragments of code. The second is explicit declaration, which is always preferable during script development and especially in any final code you use. It explicitly controls the scope of the variables, and allows for more stable code.

Implicit Variable Declaration:
The following code is in the global context, so the variables “MyVar” and “MyFn” are implicitly global.

MyVar = 33
fn MyFn = (Your code here)

The following code is in a local context, so the variables “MyVar” and “MyFn” are implicitly local, and are only accessible from within that particular local scope.

( 
MyVar = 33
fn MyFn = (Your code here)
)

Explicit Variable Declaration:

( 
local MyVar = 33--simultaneous declaration and definition
global MyFn--explicit declaration of function scope is always separate from....
fn MyFn = (Your code here)--....function definition
)

Thanks a lot for the very clear explanation!