Some years ago I learned some Java and played with inheritance. That was quite a fun because I could create a hierarchy of classes like a template for a building, and then other classes like House, Flat, Tower use that template. So far I barely see examples in addons (or are we using it in fact all the time?) like that way.
Here an example of inheritance in Python:
class Polygon:
def __init__(self, no_of_sides):
self.n = no_of_sides
self.sides = [0 for i in range(no_of_sides)]
def inputSides(self):
self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in range(self.n)]
def dispSides(self):
for i in range(self.n):
print("Side",i+1,"is",self.sides[i])
class Triangle(Polygon):
def __init__(self):
Polygon.__init__(self,3)
def findArea(self):
a, b, c = self.sides
# calculate the semi-perimeter
s = (a + b + c) / 2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
You see that in class Triangle(Polygon) there is a def init(self) function, and that is what I don’t see.
I’ve see one example though of what I think is inheritance like this:
class BToolSetup():
def execute(self, context):
Operation(context, self.mode)
return {"FINISHED"}
def invoke(self, context, event):
if len(context.selected_objects) < 2:
self.report({"ERROR"}, "At least two objects must be selected")
return {"CANCELLED"}
return self.execute(context)
class BTool_Union(Operator, BToolSetup):
bl_idname = "btool.boolean_union"
bl_label = "Brush Union"
bl_description = "This operator add a union brush to a canvas"
bl_options = {"REGISTER", "UNDO"}
mode = "UNION"
But there I don’t see the init definition.
What I have in mind is to code like I did in Java. So I can start with more abstract idea of objects, and refine it in more specific classes. Do we do that in Blender as well?