[Maya/Python] - Referencing session object variable

#1

In a open session of Maya I have a variable object declared. Then I’m creating a panel that calls in an external class object. I’d like to reference the Maya session variable object from the external class object being called from the newly created panel, created after the session variable object is defined, without passing any arguments into the external class being called from the panel.

What would be the best way of going about this without passing any init arguments down to the class that needs to reference the session variable? Let me know if I’m not describing my query very well. Thank you.

Side-note: I did look at optionVar, but the defined session variable isn’t a simple data-type, but a python object. Cheers.

#2

I drew a picture that might explain what I’m after better. Tank() inherets a singleton setup. In the script editor of Maya, I can access internal variables of the Jobmanager() class that Tank() initializes by “Tank().jobmanager.property” - I need to access the same property from the widget being initialized by the panel class that the Jobmanager() class initializes.

Another way of explaining this is if userSetup.py initialized a string variable such as - ’ zip = “foo” '. How would I go about accessing zip from the “Widget()” instance in my example?

#3
global zip
zip = "bar"

Do you want to modify zip from Widget instance?

#4

I’d like to reference the Maya session variable object from the external class object being called from the newly created panel, created after the session variable object is defined, without passing any arguments into the external class being called from the panel.

You don’t want to use option var but you want essentially a constant defined in the viewport that is hard coded into your class. Either make it an optionVar or pass it into your class. Whats to stop anything from overriding that variable?

Make a class method on job manager have tank and widget inherit from it or instantiate its bound instance as a property. As it seems an important variable make it an optionVar and test for it.

class JobManager(object):

  @classmethod
  def set_project_folder(cls, folder=None):

    if not folder:
       
       # test if the optionVar is valid.
       folder = "OptionVar"
    
    cls._folder = folder
    
  @property
  def project_folder(self):
    return self._folder

class Tank(JobManager):
  pass

class Widget(JobManager):
  pass

# or

class Bob(object):
  
  def __init__(self):
    self._job_manager = JobManager()
    
  @property 
  def project_folder(self):
    return self._job_manager.project_folder

JobManager.set_project_folder("my_folder") 

w = Widget()
print w.project_folder

b = Bob()
print b.project_folder

Don’t create global variables - they have a pesky ability of breaking other important globals. Instead make them a class or bounded instance variable and reference them through an inheritance paradigm or compositional one.

-c

#5

Thank you both for the replies - I ended up just passing jobmanager down the chain an init argument for all the classes that need access to jobmanager below the first initialization of jobmanager.

Thank you again!