3ds max callbacks gets called twice instead of one

Hello everybody,

I am currently working with 3ds max callbacks and I noticed a very anoying problem.

When I register my callback like this :

from pymxs import runtime as rt

def ProjFoldChanged():
    print("yop")

rt.pcb = ProjFoldChanged
rt.callbacks.addScript(rt.Name('postProjectFolderChange'), "pcb()", id=rt.Name('PostProjectforlderChange'))

And I change my project forlder il max (for this exemple):
Screenshot 2021-04-15 163728

The function that I registered to be called, once I change my project folder, is called twice instead of one time like this :

Do you have any suggestions ? Why this is happening ?
Thanks you,
Theo Bourille

same behavior across all max versions. only way I can see to stop is to throw a comparison in like so:

global current_project_folder
current_project_folder = ""

def ProjFoldChanged():
    global current_project_folder
    if current_project_folder != rt.pathConfig.getCurrentProjectFolder():
        print("yop")
        current_project_folder = rt.pathConfig.getCurrentProjectFolder()
2 Likes

Only thing I can think of is that in the listener when I clicked the project folder I noticed that it also printed
macros.run "Tools" "SetProjectFolder"
So I wonder if that is behind the scenes running as well and maybe that is producing a second “click” essentially?

Hello, thanks you for your anwser, yeah I had to make that workaround too, but it’s definetly taking more steps, and when you are working on big scenes, you wants to remove any extra steps, so that why I wanted to know if it was possible to get this event fired once :slight_smile:

Yes I noticied that too, I thing the best thing to do is to make you own setproject wiget, because when you execute :

 rt.pathConfig.setCurrentProjectFolder("c:\MyProject\")

The event is trigered once.
Thanks all of you for your anwser, hopefully It will be fixed in futures max versions !
Regards.

Just out of interest what if you try set the callback directly in maxscript - I’ve never used that callback specifically before but no other callback has fired twice that I have done in maxscript directly.

While this won’t solve your problem (it’s a Max bug, also happens with Maxscript) your syntax looks unnecessary here:

rt.pcb = ProjFoldChanged
rt.callbacks.addScript(rt.Name('postProjectFolderChange'), "pcb()", id=rt.Name('PostProjectforlderChange'))

You don’t need to create a Maxscript side reference to your Python function and pass an executing string "pcb()" to the addScript call. Just pass a direct reference to the Python function you defined.
eg

rt.callbacks.addScript(rt.Name('postProjectFolderChange'), ProjFoldChanged, id=rt.Name('PostProjectforlderChange'))

See here

1 Like