With bpy, how could I make a subclass of an Object and use it ?

I made a subclass of bpy.types.Object, but I don't know how to add it to my scene. The only way I know to add objects is using bpy.data.objects.new.

But I don't see any way of adding a subclass.


As the Blender Documentation states:


As you stated, that makes bpy.data.objects.new the only way to create new objects.

For my purposes, I created a new class which did not inherit from bpy.type.Object but defined a self.object property and initialized it with the new function. I then wrote all the methods I needed and modified self. object inside them.

I defined one base class, and then one additional class for cameras, lamps and objects:

class ExtendedObject():
    def __init__(self,name,  obj, loc):
        self.name = name
        self.object = obj
        self.object.location = loc
        # additional properties

class ExtendedCamera(ExtendedObject):
    def __init__(self, name, loc):
        self.data = bpy.data.camera.new(name)
        super(ExtendedObject, self).__init__(name, bpy.data.objects.new(name, self.data), loc)

# classes for lamps and 'normal' objects.

I don't know if this is the best way to do it, but it worked out for me. Hope that can help you. If not, try to describe me the purpose of your class.

  • 1
    $\begingroup$ Thank you. The goal was to make an armature specialized in folding hard surfaces, and which builds its bones alone by analyzing the object. I ended doing something simalar to you! $\endgroup$ – Maxx Sep 7 '17 at 14:40

Your Answer

