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

    Python Scripting-Help with GeDialog.AddComboBox add child option

    Cinema 4D SDK
    python
    4
    12
    2.6k
    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.
    • R
      referent
      last edited by s_bach

      Hi everyone,

      I am new with python scripting so I stuck with this and sorry for my question

      I trying to make a script -later plugin also that will rename automatically e.g.spline by selection from combo box menu with new selected name
      is there a way to add children -6+ options to combo box and send rename command based on a selection from combo box? Here is what I mean and a sample .I find 2 ways for this but I would like to have combobox selection and dropping menu 0_1550960540480_07f88f3d-f799-4997-b4a2-6c88be2cb93a-image.png

      Here are 2 examples that I make:

      Example 1:

      import c4d
      from c4d import gui

      #Dialog settings
      class MyDialogs(gui.GeDialog):
      # Dialog layout settings
      def CreateLayout(self):
      self.SetTitle('Spline Renamer 1.1')
      #Setting widget etc.
      self.GroupBegin(10000, c4d.BFH_SCALEFIT, 1, title = 'Select New Spline Name')
      self.GroupBorder(c4d.BORDER_GROUP_IN)
      self.GroupBorderSpace(20, 5, 20, 5)

          #Drop menu
          self.AddComboBox(1000, c4d.BFH_SCALEFIT, 80, 0, False)
          self.AddChild(1000, 0, 'B1')
          self.AddChild(1000, 1, 'B2')
          self.AddChild(1000, 2, 'B3')
          self.AddChild(1000, 3, 'B4')
          self.AddChild(1000, 4, 'Main')
          self.AddChild(1000, 5, 'Outer')
          self.AddChild(1000, 6, 'Patio')
          self.AddChild(1000, 7, 'Bath 1')
          self.AddChild(1000, 8, 'Bath 2')
          self.AddChild(1000, 9, 'Bath 3')
          self.AddChild(1000, 10, 'WIC')
          self.AddSeparatorV(11)
          self.AddDlgGroup(c4d.DLG_OK|c4d.DLG_CANCEL)
          return True
      

      def main():
      dlg = MyDialogs()
      dlg.Open(c4d.DLG_TYPE_MODAL)

      if name=='main':
      main()
      c4d.EventAdd()

      if name=='main':
      main()

      This Example 2 works but I dont have combobox or droping menu:

      import c4d
      from c4d import gui

      def main():
      objs = doc.GetActiveObjects(1)
      if len(objs) == 0:return

      newName = gui.InputDialog("New Name","B1")
      if newName == "":return
      
      num = gui.InputDialog("Are you sure?","B1")
      if num == "":return
      
      doc.StartUndo()
      for obj in objs:
          doc.AddUndo(c4d.UNDOTYPE_CHANGE,obj)
          obj.SetName(newName +" ")
        
      GeDialog.SetPopup("B 12 ", " B 22")
      doc.EndUndo()
      c4d.EventAdd()
      

      Thanks you so much for any of your help and for your time,

      have a great day!

      1 Reply Last reply Reply Quote 0
      • S
        s_bach
        last edited by

        Hello and welcome to the PluginCafe,

        please post your questions regarding the Cinema 4D APIs in the "Cinema 4D Development" category. Also, please use tags and the Q&A system. You find all that information in "Read Before Posting".

        Is your question how to get the currently selected item of the combo box?

        You can implement the "Command" function of your dialog to be informed when the selected value changed. This way you could rename the object when that happens.

        Alternatively, you can access the selected element using a custom member function of the dialog. You can use the returned value to do what you want in the caller code.

        Something like this:

        class TestDialog(gui.GeDialog):
            
            def CreateLayout(self):
        
                self.AddComboBox(5000, c4d.BFH_LEFT, 100, 10, False)
                self.AddChild(5000, 0, "Child 0")
                self.AddChild(5000, 1, "Child 1")
                self.AddChild(5000, 2, "Child 2")
        
                return True
        
            def Command(self, id, msg):
        
                # print currently selected "child""
                if id == 5000:
                    value = self.GetInt32(5000)
                    print("got value: " + str(value))
        
        
                return c4d.gui.GeDialog.Command(self, id, msg) 
            
            # custom function
            def GetSelectedChild(self):
                return self.GetInt32(5000)
        
        def main():
            dialog = TestDialog()
            
            dialog.Open(c4d.DLG_TYPE_MODAL_RESIZEABLE, 132456, -1 ,-1 ,400 ,400)
            
            value = dialog.GetSelectedChild()
            
            op.SetName(str(value))
            c4d.EventAdd()
        

        best wishes,
        Sebastian

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        1 Reply Last reply Reply Quote 3
        • R
          referent
          last edited by

          Hi Sebastian! Thanks a lot for your reply!!!

          and for clarifying me about posting and categories

          I couldn't figure out at the end to add command and that all complete script works by example that you send me here.

          Maybe I wasn't clear enough of what I trying to do and to get as the final result.

          My goal is that I make preset with names e.g child 0-or final name "Bath" and when I run script and chose from dropping menu of combo box e.g name "Bath" or some other next selection child 2 or final name "Patio" by selection from item with ready name "Patio" script and code automatically rename new spline based on selection from combobox eg I draw new Spline> and when I select from combo box child's selection Patio command will automatically rename spline to Patio,if is that possible? Plugin cafe is simply awesome,and I am happy so happy to discover it.
          Thanks a lot for your reply!

          Best Wishes

          Nenad

          1 Reply Last reply Reply Quote 0
          • S
            s_bach
            last edited by

            Hello,

            a GeDialog of a Python script must be a modal dialog. This means that while this dialog is opened, you can only interact with that dialog.

            Therefore you can use your script to change the name of any previously created (active) object.

            best wishes,
            Sebastian

            MAXON SDK Specialist

            Development Blog, MAXON Registered Developer

            R 1 Reply Last reply Reply Quote 2
            • R
              referent @s_bach
              last edited by referent

              @s_bach Hello Sebastian!

              The main issue is when I implement command and custom member function into the whole script how you suggest in to previously reply, nothing happening and the script wouldn't work.Or when I just copy your code example in to script manager nothing happening,I am using R17 if that can maybe be one of the reasons why script not work?

              class TestDialog(gui.GeDialog):

              def CreateLayout(self):
              
                  self.AddComboBox(5000, c4d.BFH_LEFT, 100, 10, False)
                  self.AddChild(5000, 0, "Child 0")
                  self.AddChild(5000, 1, "Child 1")
                  self.AddChild(5000, 2, "Child 2")
              
                  return True
              
              def Command(self, id, msg):
              
                  # print currently selected "child""
                  if id == 5000:
                      value = self.GetInt32(5000)
                      print("got value: " + str(value))
              
              
                  return c4d.gui.GeDialog.Command(self, id, msg) 
              
              # custom function
              def GetSelectedChild(self):
                  return self.GetInt32(5000)
              

              def main():
              dialog = TestDialog()

              dialog.Open(c4d.DLG_TYPE_MODAL_RESIZEABLE, 132456, -1 ,-1 ,400 ,400)
              
              value = dialog.GetSelectedChild()
              
              op.SetName(str(value))
              c4d.EventAdd()
              

              Thank you for reply!

              Regards

              1 Reply Last reply Reply Quote 0
              • S
                s_bach
                last edited by

                Hello,

                you have to add

                if __name__=='__main__':
                    main()
                

                as usual to execute the script.

                best wishes,
                Sebastian

                MAXON SDK Specialist

                Development Blog, MAXON Registered Developer

                1 Reply Last reply Reply Quote 1
                • R
                  referent
                  last edited by

                  7fa52105-f8bc-492d-bd56-e75e84a81179-image.png

                  1 Reply Last reply Reply Quote 0
                  • R
                    referent
                    last edited by

                    import c4d
                    from c4d import gui
                    #Welcome to the world of Python

                    class TestDialog(gui.GeDialog):

                    def CreateLayout(self):
                    
                        self.AddComboBox(5000, c4d.BFH_LEFT, 100, 10, False)
                        self.AddChild(5000, 0, "Child 0")
                        self.AddChild(5000, 1, "Child 1")
                        self.AddChild(5000, 2, "Child 2")
                    
                        return True
                    
                    def Command(self, id, msg):
                    
                        # print currently selected "child""
                        if id == 5000:
                            value = self.GetInt32(5000)
                            print("got value: " + str(value))
                    
                    
                        return c4d.gui.GeDialog.Command(self, id, msg) 
                    
                    # custom function
                    def GetSelectedChild(self):
                        return self.GetInt32(5000)
                    

                    def main():
                    dialog = TestDialog()

                    dialog.Open(c4d.DLG_TYPE_MODAL_RESIZEABLE, 132456, -1 ,-1 ,400 ,400)
                    
                    value = dialog.GetSelectedChild()
                    
                    op.SetName(str(value))
                    c4d.EventAdd()
                    

                    if name=='main':
                    main()

                    1 Reply Last reply Reply Quote 0
                    • R
                      referent
                      last edited by

                      Hello,

                      It is not working, and script isn't executable as I said in previously message,

                      thank you for your reply,

                      Regards

                      1 Reply Last reply Reply Quote 0
                      • CairynC
                        Cairyn
                        last edited by

                        Working fine for me, once I correct all the indentations. I am not sure whether your script's indents are okay, since the code is broken up into several code-quotes, but in case it's not: the methods of the class need to be indented under the class definition.

                        I don't see anything that would work under R19 (my current test) but not under R17 (your version) so this is a bit of guesswork. What is the console saying when you try to execute the code?

                        You will need to add a line to close the dialog, too, and to transform the returned value into the proper string instead of the index number as string... maybe use a dictionary for value/string pairs.

                        import c4d
                        from c4d import gui
                        
                        dropdown_values = {
                                           0:"Child 0",
                                           1:"Child 1",
                                           2:"Child 2"
                                          }
                        
                        class TestDialog(gui.GeDialog):
                        
                            pass_value = 0
                        
                            def CreateLayout(self):
                        
                                self.AddComboBox(5000, c4d.BFH_LEFT, 100, 10, False)
                                self.AddChild(5000, 0, dropdown_values.get(0))
                                self.AddChild(5000, 1, dropdown_values.get(1))
                                self.AddChild(5000, 2, dropdown_values.get(2))
                                return True
                        
                            def Command(self, id, msg):
                                # print currently selected "child""
                                if id == 5000:
                                    value = self.GetInt32(5000)
                                    print("got index: " + str(value) + ", value: " + dropdown_values.get(value))
                                    self.pass_value = value
                                    self.Close()
                                return c4d.gui.GeDialog.Command(self, id, msg) 
                        
                            # custom function
                            def GetSelectedChild(self):
                                return self.pass_value
                        
                        def main():
                            dialog = TestDialog()
                        
                            dialog.Open(c4d.DLG_TYPE_MODAL_RESIZEABLE, 132456, -1 ,-1 ,400 ,400)
                        
                            value = dialog.GetSelectedChild()
                            print "Value after call: ", value
                            op.SetName(dropdown_values.get(value))
                            c4d.EventAdd()
                        
                        if __name__=='__main__':
                            main()
                        
                        1 Reply Last reply Reply Quote 2
                        • R
                          referent
                          last edited by

                          Wow! Thank you soooooooooooo much!

                          Those parts were missing. Works like a charm now! 🙂

                          can't thank you enough!

                          We can close now this topic

                          1 Reply Last reply Reply Quote 0
                          • M
                            m_adam
                            last edited by m_adam

                            Please do not delete topics once they are resolved, this topic may help other people that have the same issue.
                            Instead, use the Q&A functionality to mark a topic as solved.

                            Cheers,
                            Maxime.

                            MAXON SDK Specialist

                            Development Blog, MAXON Registered Developer

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