Problem reading user data when a button is pressed

On 15/07/2014 at 10:35, xxxxxxxx wrote:

Hi,
 
Having problems with a script I'm writing. I've stripped it right down to the relevant bits. Basically what I'm doing is to do with swapping lots of different materials on objects and rendering various passes automatically. I'm storing the settings for the various passes in some nulls which can get saved with the scene (this is better for my particular job than using the render settings presets).
 
Anyway, the trouble with this script is that when you hit the button, it only reads the data from the null that was selected when the script was executed (check the console), and I want it to get the data from the currently selected null object whenever the button is pressed.
 
Also, if it is executed without anything selected, it fails. 
 
It seems to read the data from the scene only when the script is run, and not when the button is pressed.
 
Any ideas?

Thanks to anyone who can help. I'm pretty new to python...

 
    
    
    import c4d
    import os
    from c4d import gui
    
    BUTTON1=1002
    GROUP_ID1=1000
    
    ###################### READ USER DATA ####################################
                
    def read_user_data() :
        
        doc = c4d.documents.GetActiveDocument()
        selectedPass  = doc.GetActiveObject()
        selUserData = selectedPass.GetUserDataContainer()
        
        flags = {
            'mul' : 0,
            'RGB' : 0,
            'spe' : 0,
            'dif' : 0,
            'ref' : 0
        }
    
        print "*"*10
        print selUserData
        for descId, container in selUserData:
            index = descId[1].id
            print index
            print "*"*5
            for key, val in container:
                for txt, i in c4d.__dict__.iteritems() :
                    if key == i and txt == "DESC_SHORT_NAME":
                        if val == "Multi": 
                            flags['mul'] = op[c4d.ID_USERDATA, index]
                            print txt,"=",val, ", flag = ", flags['mul']
                        elif val == "RGBA":
                            flags['RGB'] = op[c4d.ID_USERDATA, index]
                            print txt,"=",val, ", flag = ", flags['RGB']
                        elif val == "specular":
                            flags['spe'] = op[c4d.ID_USERDATA, index]
                            print txt,"=",val, ", flag = ", flags['spe']
                        elif val == "Diffuse":
                            flags['dif'] = op[c4d.ID_USERDATA, index]
                            print txt,"=",val, ", flag = ", flags['dif']
                        elif val == "Reflection":
                            flags['ref'] = op[c4d.ID_USERDATA, index]
                            print txt,"=",val, ", flag = ", flags['ref']
                    else:
                        pass      
        c4d.EventAdd()
        return flags
    
    ############################## SETUP DIALOG  #############################
                
    class MaterialSwapDlg(gui.GeDialog) :
    
        def CreateLayout(self) :
            #create the layout
            self.GroupBegin(GROUP_ID1, c4d.BFH_LEFT, 1, 1)
            self.AddButton(BUTTON1, c4d.BFH_LEFT, name="test")
            self.GroupEnd()
            return True
        
        def InitValues(self) :
            return True
        
        def Command(self, id, msg) :
            #handle user input
            if id==BUTTON1:
                print "*"*10
                user_flags = read_user_data()
                print "user flags = ", user_flags
            return True      
        
    
    ##################################### MAIN ###############################
    
    if __name__=='__main__':
         
        dlg = MaterialSwapDlg()
        dlg.Open(c4d.DLG_TYPE_ASYNC, defaultw=50, defaulth=50)