Skip to content

Instantly share code, notes, and snippets.

@robertguetzkow
Created November 9, 2019 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertguetzkow/be4a4f25335be4d584605c168f2784c9 to your computer and use it in GitHub Desktop.
Save robertguetzkow/be4a4f25335be4d584605c168f2784c9 to your computer and use it in GitHub Desktop.
Example of using class and instance attribute, showing the implications in the context of operator lifetime
bl_info = {
"name": "Class vs Instance Attribute",
"author": "Robert Guetzkow",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "",
"description": "Demonstrates the lifetime of the operator.",
"warning": "",
"wiki_url": "",
"category": "3D View"}
import bpy
class EXAMPLE_OT_class_attribute(bpy.types.Operator):
bl_idname = "example.class_attribute"
bl_label = "Increment and print class attribute"
bl_description = "Increment and print instance attribute"
x = 0
def execute(self, context):
EXAMPLE_OT_class_attribute.x += 1
print(f"EXAMPLE_OT_class_attribute: {EXAMPLE_OT_class_attribute.x}")
return {"FINISHED"}
class EXAMPLE_OT_instance_attribute(bpy.types.Operator):
bl_idname = "example.instance_attribute"
bl_label = "Increment and print instance attribute"
bl_description = "Increment and print instance attribute"
def __init__(self):
self.x = 0
def execute(self, context):
self.x += 1
print(f"self.x: {self.x}")
return {"FINISHED"}
classes = (EXAMPLE_OT_class_attribute, EXAMPLE_OT_instance_attribute)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()
for i in range(5):
bpy.ops.example.class_attribute()
for i in range(5):
bpy.ops.example.instance_attribute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment