Solved Unnecessary Executions Under BFM_INPUT_VALUE

Hi,

I wanted to register the "Enter" Key as if I clicked a button.
It works on a singular script, but when I implement it to my code base it errors as
RuntimeError: maximum recursion depth exceeded

If I just click the button where it goes through Command, it executes only once.
If I hit enter where it goes through Message, it executes for more than 200+ times, which I believe what causes the maximum recusion.

Is there a way to limit the execution to only 1? (i.e. when I just hit "Enter")

You can see the singular script code here. Execute it and you'll see more than 200+ entries in the console.

import c4d

rename_pose_rename_string = 101010
rename_pose_btn = 202020


class rename_dialog(c4d.gui.GeDialog):
    
    def __init__(self):
        self.rename_string = ''

    def CreateLayout(self):
        self.SetTitle('Rename')
        self.AddEditText(id=rename_pose_rename_string, flags=c4d.BFH_CENTER, initw=400, inith=0, editflags=0)
        self.AddButton(id=rename_pose_btn, flags=c4d.BFH_CENTER, initw=50, inith=0, name="Rename")

        return True

    def Command(self, id, msg):

        if id == rename_pose_btn:
            self.rename_string = self.GetString(id=rename_pose_rename_string)
            print "Button Clicked"
            self.Close()

        return True

    def Message(self, msg, result):  
    
        bc = c4d.BaseContainer()  
        ok = c4d.gui.GetInputState(c4d.BFM_INPUT_KEYBOARD,c4d.KEY_ENTER, bc)  
        if ok:
            if bc[c4d.BFM_INPUT_VALUE] == 1:
                print "Enter"
                self.rename_string = self.GetString(id=rename_pose_rename_string)
                self.Close()

        return c4d.gui.GeDialog.Message(self, msg, result)   


dlg = rename_dialog()
dlg.Open(dlgtype=c4d.DLG_TYPE_MODAL)
rename_string = dlg.rename_string

Hi,

the recursion limit of Python is system dependent, but usually being set to a extremely cautious value like 10000 or so. You can modify that value with sys.setrecursionlimit , but you need to keep in mind that the recursion limit is there for a reason, i.e. to prevent (call)stack overflows.

The major problem is that your Message method is firing itself without any boundary conditions, forming an infinite loop, so raising the recursion limit will just bring you to the point where you will be getting an actual stack overflow. I am not quite sure which one of the expressions is the culprit without testing it, but the candidates would beGetString and Close I guess. You will need something like if msg == something wrapping around your logic to prevent this from happening.

Cheers,
zipit

MAXON SDK Specialist
developers.maxon.net

@zipit

Thanks for the response.

I followed your advice and used
if msg.GetId() == c4d.BFM_INTERACTSTART:

It works in that I no longer have recursion error but I'm not sure if that's the exact check I should perform.
Wdythink?

Hi,

that seems to be an appropriate message. You could also use the more common BFM_ACTION, although this will be fired twice for each keystroke. Your solution is technically better, BFM_ACTION would be probably more readable for a broader audience.

Cheers,
zipit

MAXON SDK Specialist
developers.maxon.net

@zipit

Thanks for the confirmation! :)