Can't access self.Variable (Maya, Py)

I have a script completely enclosed inside a class.
To get the length of the whole render, and do a couple other things, I have a pre and post MEL script that call on respective methods inside this class.

The problem is they can’t seem to access any variables set inside the class or by each other, which of course makes them significantly less useful.
However they can still call other methods inside the class with self.method()

So I’m guessing the variables are in the wrong space for them to access.

Any ideas on how I might go about fixing this?

Thanks!

Are the variables name mangled with leading underscores, aka Python’s psuedo-private-data syntax?

Can you write getter and setter functions to call to get the member properties?

the variables should be safe names (in this case self.startTime and self.endTime)

Could you elaborate on the getter and setter functions? Sorry,I’m a bit new to scripting.

If I understand correctly I could pass the variables I need as parameters to another function that will set it in the classes scope and then retrieve it again?

class Foo( object ):
   def __init__( self ):
      self.start_time = 0
      self.end_time = 10

   def get_start_time( self ):
      return self.start_time

   def set_start_time( self, time ):
      self.start_time = time

   def get_end_time( self ):
      return self.end_time

   def set_end_time( self, time ):
      self.end_time = time

(same thing on PasteBin)

Thanks for the code. Couldn’t get it working though. Same issue. I don’t know what I did, but they can get any values set in the init but they can’t seem to set or get any other values. :’(

I’ve uploaded the file in case someone can take a look and tell me what I’m failing to do.

Basically, it’s a script that emails the renderlog to the user if the render takes longer than a certain amount of time.

The methods in question are right after the ui method and are called preScript and postScript. preScript needs to pass self.startTime and self.dumpFile to the postScript. These calculate the total renderTime and also read and then delete the log file.

If anyone can figure it out, I’d be very grateful. I’m going to try the setter/getter funcs again and see if I can get it working too.

Many thanks!

Not to come off as short, but can you post a traceback or something? Just telling us it doesn’t work isn’t incredibly helpful…

[QUOTE=djTomServo;13697]Not to come off as short, but can you post a traceback or something? Just telling us it doesn’t work isn’t incredibly helpful…[/QUOTE]

oh yeah sure. Sorry, my bad.


# Error: line 0: 'dagMail' object has no attribute 'startTime'
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
#   File "C:/Users/Dhruv/AppData/Local/Temp\eclipsePython4113460403172148014.py", line 126, in postScript
#     self.totalTime = (self.endTime - self.startTime)/60
# AttributeError: 'dagMail' object has no attribute 'startTime' # 

Using Eclipse as the IDE with cgjedis Maya bridge.

Hmm…what’s the calling context of this? Just from looking at the script, I have some ideas about what’s probably going on, but without seeing the whole stack running it’s hard to tell. Probably wouldn’t be a bad idea to put all your member var decls in init, but just from looking at that traceback and the script, I’m guessing your calling context would still create some issues. Do you have an accessible instance of the class available during operation? If not, that might be the problem.

I’m assigning it as test=dagMail() (which is the class name)
and then calling the window as test.ui()

I’ve tried declaring the variables in the init so atleast the script doesn’t fail, but the methods themselves can’t update the variables in the class.

I’m still getting to grips with py, but I was thinking maybe there’s a way I can maybe set it’s value for that instance? or how might I declare and access a global variable? Probably not good etiquette to do so, but if I give it a unique name it shouldn’t ever conflict with anything else.

Thanks btw for the help

Edit: but I am calling the render scripts to the class directly. I should probably call it via an instance.
I’ll try that when I get home

In your above example, you can assign test as a global, then you should be able to access test.whatever from anywhere else where test is also a global. This is probably better than exposing each individual variable as a global variable. In python, you have to assign globals for each different scope. For example, if some code that runs from a toolbar button assigns test as a global, the script editor window won’t see that as a global unless you also declare it there as such as well. The same goes from MEL. In MEL, you will have to declare the global before you access it. You can use the semicolon in your string of python code so that the global assignment happens just before the command you want to execute.

class dagMail:
    def _init_(self):
        self.thingy = "foo"  #when the instance of dagMail is created, _init_ will be run

    def getThingy(self):  #optional "getter" function as you will see below
        return self.thingy

global test    #declare that all of my soon-to-be dagMail instance is a global
test = dagMail()  #and here is the instance creation

Then from MEL, you can do:

string $melTestThingy;
$melTestThingy= python("global test;test.thingy"); 

…or

string $melTestThingy;
$melTestThingy= python("global test;test.getThingy()"); 

Note that I didn’t actually run this code in Maya, so there may be some syntax errors.

Thanks guys!
It was just because I was calling the class directly rather than through an instance.

But it works now. A few unrelated bugs, but I’ll work through those. Hopefully.

Cheers

And thank you guys for the code explanations too. Learnt from that!