Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    How to Generate Dynamic Functions?

    General Talk
    r21 python
    2
    3
    515
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      bentraje
      last edited by

      Hi,

      With the help of Pim Grooff's

      , I have a GUI that generates buttons at run time.

      My problem is it doesn't dynamic. How do I generate dynamic functions also to coincide with the dynamic buttons (along with their dynamic ids).

      You can see the problem here: Button 1, 2, 3 works but not Button 4 and above.
      https://www.dropbox.com/s/svimsrbhx35egtk/c4d203_generate_dynamic_functions.mp4?dl=0

      You can check the working code here:

      import c4d
      from c4d import gui, plugins, bitmaps, utils, documents
      
      PLUGIN_ID = 1450011099
      
      # Based from Pim Grooff's code 
      # 
      def print_A(): return "1011 Button" def print_B(): return "1021 Button" def print_C(): return "1031 Button" class MyDialog(gui.GeDialog): def InitValues(self): self.newfieldindex = 0 self.button_commands = {1031: print_A(), 1032: print_B(), 1033: print_C()} return True def CreateLayout(self): self.SetTitle("Add/Remove Enable/Disable Dialog") self.GroupBegin(id=1002, flags=c4d.BFH_MASK, cols=2) self.AddButton(1021, c4d.BFV_MASK, initw=145, name="Add") self.AddButton(1022, c4d.BFV_MASK, initw=145, name="Remove") self.GroupEnd() self.GroupBegin(id=1030, flags=c4d.BFH_MASK, cols=1) self.GroupEnd() return True def Command (self, id ,msg): if (id==1021): self.LayoutFlushGroup(id=1030) self.newfieldindex += 1 if (self.newfieldindex < 0): self.newfieldindex = 0 for newid in range(1,self.newfieldindex+1): button_id = newid + 1030 fieldname = "Add-" + str(newid) self.AddButton(button_id, c4d.BFV_MASK, initw=145, name=fieldname) self.LayoutChanged(id=1030) return True if (id==1022): self.LayoutFlushGroup(id=1030) self.newfieldindex -= 1 if (self.newfieldindex < 0): self.newfieldindex = 0 for newid in range(1,self.newfieldindex+1): button_id = newid + 1030 fieldname = "Add-" + str(newid) self.AddButton(button_id, c4d.BFV_MASK, initw=145, name=fieldname) self.LayoutChanged(id=1030) return True if id in self.button_commands: print self.button_commands[id] return True class TestDialog(plugins.CommandData): dialog = None def Execute(self, doc): if self.dialog is None: self.dialog = MyDialog() return self.dialog.Open(dlgtype=c4d.DLG_TYPE_ASYNC, pluginid=PLUGIN_ID) def RestoreLayout(self, sec_ref): if self.dialog is None: self.dialog = MyDialog() return self.dialog.Restore(pluginid=PLUGIN_ID, secret=sec_ref) if __name__ == "__main__": okyn = plugins.RegisterCommandPlugin(PLUGIN_ID, "Enable Disable Dynamic",0, None, "Enable Disable Dynamic", TestDialog()) if (okyn): print "Enable Disable Initialized"
      1 Reply Last reply Reply Quote 0
      • M
        mp5gosu
        last edited by mp5gosu

        I modified your code to dynamically react to IDs. (Btw, it's now a script for simple demonstration).
        edit: Actually, this is just shifting the problem. There's no way in python I know of to bind functions directly. (Wouldn't make sense on dynamically added Gadgets anyways, imho.)

        import c4d
        from c4d import gui
        PLUGIN_ID = 1450011099
        
        # Based from Pim Grooff's code 
        # 
        def print_me(id): print id class MyDialog(gui.GeDialog): def InitValues(self): self.newfieldindex = 0 return True def CreateLayout(self): self.SetTitle("Add/Remove Enable/Disable Dialog") self.GroupBegin(id=1002, flags=c4d.BFH_MASK, cols=2) self.AddButton(1021, c4d.BFV_MASK, initw=145, name="Add") self.AddButton(1022, c4d.BFV_MASK, initw=145, name="Remove") self.GroupEnd() self.GroupBegin(id=1030, flags=c4d.BFH_MASK, cols=1) self.GroupEnd() return True def Command (self, id ,msg): if (id==1021): self.LayoutFlushGroup(id=1030) self.newfieldindex += 1 if (self.newfieldindex < 0): self.newfieldindex = 0 for newid in range(1,self.newfieldindex+1): button_id = newid + 1030 fieldname = "Add-" + str(newid) self.AddButton(button_id, c4d.BFV_MASK, initw=145, name=fieldname) self.LayoutChanged(id=1030) return True elif (id==1022): self.LayoutFlushGroup(id=1030) self.newfieldindex -= 1 if (self.newfieldindex < 0): self.newfieldindex = 0 for newid in range(1,self.newfieldindex+1): button_id = newid + 1030 fieldname = "Add-" + str(newid) self.AddButton(button_id, c4d.BFV_MASK, initw=145, name=fieldname) self.LayoutChanged(id=1030) return True else: print_me(id) return True if __name__ == "__main__": dlg = MyDialog() dlg.Open(dlgtype=c4d.DLG_TYPE_ASYNC, pluginid=PLUGIN_ID)
        1 Reply Last reply Reply Quote 2
        • B
          bentraje
          last edited by

          Gotcha. Thanks for the code and confirmation.
          For my code, it's not really a simple printing the ID. Each ID corresponds with a separate function/script.
          I guess I'll just store them in a separate list. Separate ID list and separate function script list. Then execute them by matching index.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post