0
$\begingroup$

I would like to know how reference a Class created in a separate Python file. I'm working on an add-on that will let people create custom models using designs I've sculpted. I have a custom UI with buttons that let you choose which models to import and what to do with them. However, I would like to add new models in the future, which would require adding new stl's to the same directory. More importantly, this would mean the UI would need to be updated with new buttons and check marks. Here is the main panel I created. Each of those operators are different models that can be imported.

class BaseCreator(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Base Creator"
bl_idname = "Base_Creator"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Base Creator'

def draw(self, context):
    layout = self.layout

    row = layout.row()
    layout.operator("dungeon.tile")
    row = layout.row()
    layout.operator("initialize.scene")
    row = layout.row()
    layout.operator("terrain_import.rocks")
    row = layout.row()
    layout.operator("terrain_import.stone")
    row = layout.row()
    layout.operator("terrain_import.sand")
    row = layout.row()
    layout.operator("terrain_import.seals")
    row = layout.row()
    layout.operator("terrain_import.industrial")
    row = layout.row()
    layout.operator("change.terrain")
    row = layout.row()
    

    layout.operator("add.assets")
    layout.operator("terrain.combine")

I would like to place this in a separate python file whose name will always be the same (let's say "MyUI.py") and lives in the same directory as the models that can be imported. How would I would I pull in this class in the main code? I know I'll have to import it, but how do I reference the class so that it creates the panel when I run the main script? Do I place the whole class in a separate python file, register it there and then just import that when the main code starts? Or do I leave the class where it is and just place the draw function in a separate file?

I've managed to put variables like lists of asset names in a separate file and call them successfully, but I'm not totally sure how to call a class from a separate python file and make it create the panel successfully. Any help would be appreciated!

$\endgroup$
9
  • 1
    $\begingroup$ This may help with your goal Multi file Addon $\endgroup$
    – Ratt
    Mar 30, 2021 at 18:01
  • $\begingroup$ This is helpful, thank you. I'm assuming I could replace individual files with new ones of the same name, just with updated code, without messing up the program, correct? $\endgroup$ Mar 30, 2021 at 18:06
  • $\begingroup$ Nope, the functions and class names have to match as well so there is no real advantage, related or duplicate: best practice for add-on generation with many functions single file or split it up? $\endgroup$
    – brockmann
    Mar 30, 2021 at 18:16
  • $\begingroup$ Maybe I didn't explain well enough. Each of those operators are functions that import a specific stl and then move and rotate it. Yes, if I add a new operator row I would need to add the function it references as well. I would like to place those in a separate file which the main code will import. That way, when I make new models (and the corresponding function to import them) I can add them to the separate python file. That way, I can send that to someone and they can just replace their existing file with that one, without needing to update the main code $\endgroup$ Mar 30, 2021 at 18:49
  • $\begingroup$ Hard to guess what you mean. (1) "if I add a new operator row I would need to add the function it references as well" - not necessarily. You can declare operator properties like rotation or scale for all models and pass the user values: blender.stackexchange.com/a/2516/31447 (2) That way, I can send that to someone and they can just replace their existing file with that one - You'll have to maintain the add-on, updating is required anyway (blender is constantly changing) so I personally don't see any real advantage by doing that. $\endgroup$
    – brockmann
    Mar 30, 2021 at 19:07

0

You must log in to answer this question.

Browse other questions tagged .