How to get ComboBox value based on item selection?



  • On 12/11/2017 at 05:28, xxxxxxxx wrote:

    Hi everyone,

    Firstly, thank you for taking the time to create this amazing community, where I learn a lot even tho this is my first post. I'm sure you are able to help me. 🙂

    I've built a dialog where you select the folder in the first comboBox and based on that choice it adds the files to a second comboBox.

    I've manage to have get the directory for the folders, but I can't get the children of the selected folder on the second comboBox. I understand that you can't just get the string of a comboBox (unfortunately).
    So how can I get the values for the selected folder?
    Here's the code I'm currently working on:

    ## eXpressoMaker is created by Andre Anjos v1.0 ##
      
    # Add template modules into Xpresso
      
    # Import modules #
      
    import c4d, os
    from c4d import gui, plugins, documents, storage
      
    ##################
      
    # Global Variables
      
    PLUGIN_ID = 1040128  # Release ID 1040128
    GRP_COMBOX = 1002
    CHILD_VALUES = 1011
    PATH = storage.GeGetStartupWritePath() + '/library/xgroup/'
      
    ##################
      
    def listFolders() :
      
        folders = []
      
        if os.path.exists(PATH) :
      
            for folder in os.listdir(PATH) :
                folders.append(folder)
        
        return folders
        
    def listFiles(folder) :
      
        files = []
      
        for file in os.listdir(PATH + folder) :
            files.append(file)
        
        return files
      
    class eXpressoMakerDialog(gui.GeDialog) :
      
        def CreateLayout(self) :
      
            self.GroupBegin(id=10000, flags=c4d.BFH_SCALEFIT, cols=1, rows=3, title="eXpresso Maker v1.0")
            
            self.GroupBegin(id=10001, flags=c4d.BFH_SCALEFIT, cols=1, rows=1, title="Folder")
            self.AddStaticText(id=1001, flags=c4d.BFH_CENTER, initw=0, inith=20, name="Folder")
            self.AddComboBox(id=GRP_COMBOX, flags=c4d.BFH_SCALEFIT, initw=200, inith=20)
            
            for folder in listFolders() :
                self.AddChild(id=GRP_COMBOX, subid=CHILD_VALUES, child=folder) 
      
            self.GroupEnd()
            
            self.GroupBegin(id=10002, flags=c4d.BFH_SCALEFIT, cols=1, rows=1, title="File")
            self.AddStaticText(id=1003, flags=c4d.BFH_CENTER, initw=0, inith=20, name="File")       
            self.AddComboBox(id=1004, flags=c4d.BFH_SCALEFIT, initw=200, inith=20)
            
            self.GroupEnd()
            
            self.GroupBegin(id=10003, flags=c4d.BFH_SCALEFIT, cols=1, rows=1)
            self.AddSeparatorV(initv=200, flags=c4d.BFH_SCALEFIT)               
            self.AddButton(id=1005, flags=c4d.BFH_SCALEFIT,  initw=200, inith=20, name="Add")
            self.GroupEnd()
            
            self.GroupEnd()
            
            return True
      
        def Command(self, id, msg) :
      
            if (id == GRP_COMBOX) :
            
                if (self.GetInt32(GRP_COMBOX) == CHILD_VALUES) :
                    
                    childSelected = self.GetInt32(GRP_COMBOX) #<---- Get the comboBox value here
      
                    # Here to get value for folderSelected??
                                                 |
                                                 V
                    for file in listFiles(folderSelected) :
                        self.AddChild(id=1004, subid=1012, child=file)
                        
                    return True
                
            
    class eXpressoMakerPlugin(plugins.CommandData) :
      
        dialog = None
        
        # Creates the dialog
        def Execute(self, doc) :
        
            if self.dialog is None:
                self.dialog = eXpressoMakerDialog()
              
            return self.dialog.Open(dlgtype=c4d.DLG_TYPE_ASYNC, pluginid=PLUGIN_ID, defaultw=200, defaulth=150, xpos=-1, ypos=-1)
        
        # Manages the dialog
        def RestoreLayout(self, sec_ref) :
      
            if self.dialog is None:
                self.dialog = eXpressoMakerDialog()
              
            return self.dialog.Restore(pluginid=PLUGIN_ID, secret=sec_ref)
            
    if __name__ == "__main__":
     
        okyn = plugins.RegisterCommandPlugin(PLUGIN_ID, "eXpresso Maker v1.0", 0, None, "eXpresso Maker v1.0", eXpressoMakerPlugin())
        
        if (okyn) :
            print "eXpresso Maker v1.0 Loaded!"
    

    Thank you very much in advance with your help!

    Andre



  • On 13/11/2017 at 01:44, xxxxxxxx wrote:

    Hello and welcome,

    when you are creating the combo box you are assigning each child the same subid (see AddChild()). This is wrong; each child needs it's own subid. This subid is returned when you access the current value of the combo box with GetInt32(). You find an example in the C++ gedialog_gadgets.cpp dialog.

    To get the selected folder or file you can simply store the list returned by your appropriate sub-function as a member of your dialog class. Then you can store the corresponding index as the subid of the child. When you get the currently selected child with GetInt32() this would return the index you need to use to get the correct string from the stored list.

    Also, please do not cross post your questions on multiple forums or at least include a link to your cross posts. This helps everyone to avoid unnecessary work.

    best wishes,
    Sebastian



  • On 13/11/2017 at 01:50, xxxxxxxx wrote:

    Hi Sebastian,

    Thank you very much for the welcoming and quick reply! 🙂

    I see what you mean and makes total sense.

    Apologies for cross posting.

    Have a great day!

    Cheers!

    Andre


Log in to reply