I am trying to extend a Blender Object by assigning a Python class to the object using the PointerProperty type. I know that the normal procedure is to simply assign the primitive data to an Object by using the properties that are built into the Blender API. The difficulty lies in the fact that the class structure is needed outside of Blender, and maintaining two versions will not be possible. I have found a way to assign a class; however, it appears that the PointerProperty always points to the same data in memory regardless of the Object. The other concern is that it appears that the class constructor is never called, and, therefore, only the class wide properties and not the instance properties are available. I have included an example that displays the noted behavior below. Does anyone know of a way in which to assign a Python class to a Blender object? Thank you in advance for your assistance.
# class_test.py import bpy # Define a class that exists outside of Blender class test_class(): a_string = "" an_int = 0 def __init__(self,in_str="",in_int=0): self.a_string = in_str self.an_int = in_int return # # Define a class to register with Belnder class BlenderTestClass(bpy.types.PropertyGroup): the_class = test_class() def __init__(self,in_str="",in_int=0): self.the_class = test_class(in_str,in_int) return # # Register the class bpy.utils.register_class(BlenderTestClass) # # Assign the property class to an Object bpy.types.Object.object_test_class = \ bpy.props.PointerProperty(type=bpy.types.BlenderTestClass) # # Remove all of the meshes and add 2 cubes bpy.ops.object.select_by_type(type="MESH") bpy.ops.object.delete() bpy.ops.mesh.primitive_cube_add(location=(-2,0,0)) cube1 = bpy.context.object bpy.ops.mesh.primitive_cube_add(location=(2,0,0)) cube2 = bpy.context.object # # Set cube1 properties cube1.object_test_class.the_class.a_string = "I am cube1" cube1.object_test_class.the_class.an_int = 1 print("Cube1 class data") print("a_string = %s" %cube1.object_test_class.the_class.a_string) print("an_int = %d " %cube1.object_test_class.the_class.an_int) # # Cube2 has the same values as cube1! print("Cube2 class data") print("a_string = %s" %cube2.object_test_class.the_class.a_string) print("an_int = %d " %cube2.object_test_class.the_class.an_int) print(cube1.object_test_class.the_class is cube2.object_test_class.the_class)