0

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!

  • 1
    This may help with your goal Multi file Addon – Ratt Mar 30 '21 at 18:01
  • 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? – Anthony Joseph Schilling Mar 30 '21 at 18:06
  • 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? – brockmann Mar 30 '21 at 18:16
  • 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 – Anthony Joseph Schilling Mar 30 '21 at 18:49
  • 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: https://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. – brockmann Mar 30 '21 at 19:07
  • Building a dynamic panel based on the count of the models within the folder would make sense to me though... Why stl files? If you'd use eg. obj files you can easily create them using python if necessary. – brockmann Mar 30 '21 at 19:08
  • Ah, thank you for explaining . How would that work? I was trying to think a solution like that but couldn't figure it out. The function I use to import models generally uses the name of the corresponding stl file. Is there a way to create a dnamic panel that creates buttons based on the stl files currently in the directory? Maybe loop through the files and [do something] when it comes across an stl? – Anthony Joseph Schilling Mar 30 '21 at 19:19
  • Yep, multiple ways to do that... for now I think one easy way is adding a folder property to the panel and add an operator to "generate the ui" within a for-loop, should be easy to do based on the following Q&A: https://blender.stackexchange.com/questions/57306/how-to-create-a-custom-u – brockmann Mar 30 '21 at 19:24
  • Hmm, I think that sounds doable. The only other issue is, several of my buttons don't correspond to only one stl. For example, I have one that imports plant models. But I have multiple plant models (Plant1.stl, Plant2.stl, etc.) and it grabs one randomly from a list of file names. I suppose it could just loop through the directory, name the button after the word before the number and add list of the full name of all Plant stls and randomly import for that when the button is pressed – Anthony Joseph Schilling Mar 30 '21 at 19:33

0 Answers0