Neo Conker
Neo Conker

Reputation: 169

Efficient way to pass gui variables to classes?

I'm using the program Maya to make a rather large project in python. I have numerous options that will be determined by a GUI and input by the user. One example of an option is what dimensions to render at. However I did not make a GUI yet and am still in the testing faze.

What I ultimately want is a way to have variables be able to be looked up and used by various classes/methods within multiple modules. And also that there be a way that I can test all the code without having an actual GUI.

Should I directly pass all data to each method? My issue with this is if method foo relies on variable A, but method bar needs to call foo, it could get real annoying passing these variables to Foo from everywhere its called.

Another way I saw was passing all variables through to each class instance itself and using instance variables to access. But what if an option changes, then i'd have to put reload imports every time it runs.

For testing what I use now is a module that gets variables from a config file with the variables, and i import that module and use the instance variables throughout the script.

def __init__(self):
        # Get and assign all instance variables.
        options = config_section_map('Attrs', '%s\\ui_options.ini' %(data_path))
        for k, v in options.items():
            if v.lower() == 'none':
                options[k] = None
        self.check_all = int(options['check_all'])
        self.control_group = options['control_group']

Does anyone have advice or can point me in the right direction dealing with getting/using ui variables?

Upvotes: 0

Views: 375

Answers (1)

theodox
theodox

Reputation: 12218

If the options list is not overly long and won't change, you can simply set member variables in the class initializer, which makes the initialization easy for readers to understand:

class OptionData(object):


     def __init___(self):
         #set the options on startup

         self.initial_path = "//network"
         self.initial_name = "filename"
         self.use_hdr = True
         # ... etc

If you expect the initializations to change often you can split out the initial values into the constructor for the class:

class OptionData(object):


     def __init___(self, path = "//network", name = "filename", hdr=True)

     self.initial_path = path
     self.initial_name = name
     self.use_hdr = hdr

If you need to persist the data, you can fill out the class reading the cfg file as you're doing, or store it in some other way. Persisting makes things harder because you can't guarantee that the user won't open two Maya's at the same time, potentially changing the saved data in unpredictable ways. You can store per-file copies of the data using Maya's fileInfo.

In both of these cases I'd make the actual GUI take the data object (the OptionData or whatever you call yours) as an initializer. That way you can read and write the data from the GUI. Then have the actual functional code read the OptionData:

 def perform_render(optiondata):
     #.... etc

That way you can run a batch process without the gui at all and the functional code will be none the wiser. The GUI's only job is to be a custom editor for the data object and then to pass it on to the final function in a valid state.

Upvotes: 2

Related Questions