0
$\begingroup$

I have four classes:

  1. Panel class
  2. Property Group class
  3. Operator class
  4. Object Builder class (in a separate file)

These classes connect together to create an object builder panel, which takes 2D geometry from a template Blender file, imports it into the current scene, and performs a series of operations on it to create full 3D models. That's not the bit I need help with really, that's just some background.

What I want to do is have separate instances of all of my classes so that I can build multiple objects at once with different property values, all from a bunch of different panels.

So object-1 might have a size_x of 10, size_y of 11, and size_z of 5, and object-2 might have a size_x of 5, size_y of 2, and size_z of 3. Here is my code:

 ###### IMPORTS ######
import bpy, sys, importlib, pandas as pd
from bpy.types import Operator, Panel, PropertyGroup, Menu
from bpy.props import *
from bpy.utils import register_class, unregister_class
from pathlib import Path

###### IMPORT FUNCTIONS SUB-SCRIPT FROM RELATIVE PATH ######
path = Path(bpy.data.filepath)
sys.path.append(str(path.parent.parent.parent.parent.parent) + "\\Blender Scripts")
import functions
importlib.reload(functions)


C = bpy.context
D = bpy.data
O = bpy.ops
P = bpy.props
T = bpy.types

###### PROPERTY GROUP ######

class OBJECTTOOL_Settings(PropertyGroup):
 
    size_x: IntProperty(
        name = "size_x",
        description = "size_x",
        min = 6,
        max = 26,
        default = 12,
        )

    size_y: FloatProperty(
        name = "size_y",
        description = "size_y",
        min = 3.5,
        max = 26,
        default = 9.0,
        step = 50
        )

    size_z: IntProperty(
        name = "size_z",
        description = "size_z",
        min = 4,
        max = 12,
        default = 8,
        )

###### PANEL CLASS ######

class OBJECTTOOL_PT_main_panel(Panel):
    bl_label = "Object Builder Tool"
    bl_idname = "OBJECTTOOL_PT_main_panel"
    bl_space_type = "VIEW_3D"   
    bl_region_type = "UI"
    bl_category = "Object Builder Tool"
    bl_context = "objectmode"

    def draw(self, context):
        layout = self.layout
        scene = context.scene
        objecttools = scene.objecttools
        row = layout.row()
        row.prop(objecttools, "size_x", text="size_x")
        row = layout.row()
        row.prop(objecttools, "size_y", text="size_y")
        row = layout.row()
        row.prop(objecttools, "size_z", text="size_z")
        row = layout.row()
        row.operator('objecttool.build', text="Build")

###### OPERATOR CLASS ######

class OBJECTTOOL_OT_build_object(Operator):
    bl_label = "Object Builder Tool"
    bl_idname = "objecttool.build"
    bl_description = "Build Drum"
    bl_options = {'REGISTER', 'UNDO'}
    
    def execute(self, context):
        size_x = int(C.scene.objecttools.size_x)
        size_y = float(C.scene.objecttools.size_y)
        size_z = int(C.scene.objecttools.size_z)
        ###### THIS IS WHERE I CALL MY EXTERNAL BUILD FUNCTION ######
        ###### THIS IS WHERE I CALL MY EXTERNAL BUILD FUNCTION ######
        ###### THIS IS WHERE I CALL MY EXTERNAL BUILD FUNCTION ######
        builder = functions.builder(size_x=size_x, size_y=size_y)
        return {'FINISHED'}

###### REGISTRATION ######

classes = (
    OBJECTTOOL_OT_build_object,
    OBJECTTOOL_Settings,
    OBJECTTOOL_PT_main_panel,
)

def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)

    bpy.types.Scene.objecttools = PointerProperty(type=OBJECTTOOL_Settings)

def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)
    del bpy.types.Scene.objecttools
    
if __name__ == "__main__":
    register()

I guess what I need then is an instance of each class for each object that I want to build. Is there anyway to wrap these classes in another class so that registering a single class will register them all? That way I would guess I could use the Python type function as outlined by batFINGER here: How can I control the number of sub-panel instances from an IntProperty?

I don't want to build each object individually because the plan is to eventually set all of their positions relative to one another, programatically.

$\endgroup$

Your Answer

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

Browse other questions tagged or ask your own question.