0
$\begingroup$

I have made some python script which all contain some operators and a panel to execute the operators (see template below). Each script is in a seperate file. So right now I have to open and run each script manually every time I open a new instance of Blender.

I would like to bring it all together. I thought about making a single file and then importing each script:

import os
import sys    
scriptpath = ".../python scripts"
sys.path.append(os.path.abspath(scriptpath))
import Some Script1.py
#import all the rest...

But I'm not sure how all the registration is handled. Import only gives access to the classes and methods in the file, right? I could of cause just copy/paste averything in to a single script. But that makes it more rigid and unscalable (I might want to add more panels later on). Maybe there is some way to collect in an addon? I'm rather new to both Blender and Python, so any help would be very well received.

Script template:

# Filename/path is be: ".../python scripts/Some_Script1.py" Can I have spaces in the filename?
class TEMPLATE_OT_operator1(bpy.types.Operator):
    bl_idname = "mesh.operator1"
    bl_options = {'REGISTER', 'UNDO'} # Must have REGISTER!   

    def execute(self, context):
        # Do stuff
    return {'FINISHED'}

class TEMPLATE_PANEL_PT_panel1(bpy.types.Panel):
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'

    def draw(self, context):
        # Draw panel

classes = (
    TEMPLATE_OT_operator1,
    TEMPLATE_PANEL_PT_panel1
)

def register():
    bpy.types.Scene.SomeProp1 = bpy.props.FloatProperty(name='SomeProp1')
    for cls in classes:
        bpy.utils.register_class(cls)

def unregister():
    del bpy.types.Scene.SomeProp1 
    for cls in classes:
        bpy.utils.unregister_class(cls)

if __name__ == "__main__":
    register()
$\endgroup$
4

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.