User defined operator classes, inherited from bpy.types.Operator, are static. Accordingly, only static variables can be defined in them.
Static variable sampleVar definition in SampleClass class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class SampleClass(bpy.types.Operator): bl_idname = "sample.sample_class" sampleVar = 5 def execute(self, context): print(self.sampleVar) self.__class__.sampleVar += 1 print(self.sampleVar) print(self.__class__.sampleVar) def sampleFunc(): SampleClass.sampleVar += 1 print(SampleClass.sampleVar) |
Expression:
1 |
sampleVar = 5 |
defines a static variable sampleVar with a default value of 5.
Since at the time of operator executrion a temporary instance of its class creates with local copyes of the class variables. So getting the value of the desired class variable is possible from the current instance of the class:
1 |
print(self.sampleVar) |
or through the class itself:
1 |
print(self.__class__.sampleVar) |
Class instance exists only during operator execution, so setting class static variable value through the current instance is impossible. That case value stores only to the local instance variable sampleVar copy, not to the static class variable, and will be removed with current instance after operator completion. Therefore setting a value to static variable is possible only through an appeal of the class.
1 |
self.__class__.sampleVar += 1 |
Accessing static class variables outside the class is possible via its classname:
1 2 |
SampleClass.sampleVar += 1 print(SampleClass.sampleVar) |