2
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

As the Blender Documentation states:

Those of you familiar with other Python API’s may be surprised that new data-blocks in the bpy API can’t be created by calling the class.

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.

$\endgroup$
  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.