[PYTHON] [Blender] Split Blender script into multiple files

Isn't the Blender script source file, which was initially limited in functionality and small in size, growing in size with the addition of functionality? In such a case, let's divide it into multiple source files. That said, some people may not know how to split a Blender script, so here's a summary of how to split a Blender script.

Source before file split

Here is a sample source file before splitting the file. It's just an example, so I don't really think about the contents of the source.

all_in_one.py


import bpy

bl_info = {
# ...
}

class Operator_1(bpy.types.Operator):
    bl_idname = "uv.operator_1"
    bl_label = "Operator 1"
    file_name = "hoge.blend"

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

class Operator_2(bpy.types.Operator):
    bl_idname = "uv.operator_2"
    bl_label = "Operator 2"

    def execute(self, context):
        self.report({'INFO'}, Operator_1.file_name)
        return{'FINISHED'}



def menu_func(self, context):
    self.layout.separator()
    self.layout.menu(Operator_1.bl_idname)
    self.layout.menu(Operator_2.bl_idname)


def register():
    bpy.utils.register_module(__name__)
    bpy.types.VIEW3D_MT_uv_map.append(menu_func)

def unregister():
    bpy.utils.unregister_module(__name__)
    bpy.types.VIEW3D_MT_uv_map.remove(menu_func)


if __name__ == "__main__":
    register()

Source after file split

Indicates a file in which the file is divided for each class. This sample requires all files to be in the same directory. Also, the file name that describes the process when the script is first loaded must be __init__.py. There is a process to read the first split file of __init__.py, but other than that, there is no particular difference compared to the case where it is not split. The explanation of this part, to tell the truth, is the same as the Python package, so it is very familiar to those who know Python.

op1.py


import bpy

class Operator_1(bpy.types.Operator):
    bl_idname = "uv.operator_1"
    bl_label = "Operator 1"
    file_name = "hoge.blend"

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

op2.py


import bpy
from . import op1

class Operator_2(bpy.types.Operator):
    bl_idname = "uv.operator_2"
    bl_label = "Operator 2"

    def execute(self, context):
        self.report({'INFO'}, op1.Operator_1.file_name)
        return{'FINISHED'}

__init__.py


bl_info = {
# ...
}

if "bpy" in locals():
    import imp
    imp.reload(op1)
    imp.reload(op2)
else:
    from . import op1
    from . import op2

import bpy

def menu_func(self, context):
    self.layout.separator()
    self.layout.menu(op1.Operator_1.bl_idname)
    self.layout.menu(op2.Operator_2.bl_idname)


def register():
    bpy.utils.register_module(__name__)
    bpy.types.VIEW3D_MT_uv_map.append(menu_func)

def unregister():
    bpy.utils.unregister_module(__name__)
    bpy.types.VIEW3D_MT_uv_map.remove(menu_func)


if __name__ == "__main__":
    register()

Finally

I showed you how to split a file in a Blender script. If you think of Blender as treating the functionality of a script like a package, it's the same as creating a Python package. Therefore, it may not have been necessary to write an article, but I hope it will be helpful for those who create Blender scripts.

Reference information

Recommended Posts

[Blender] Split Blender script into multiple files
Combine multiple python files into one python file
[Python & Unix] Combine multiple PDF files into one.
Combine multiple Excel files loaded using pandas into one
Rsync multiple files at once
Decompress multiple compressed files (Python)
Split PDF into arbitrary pages
Upload multiple files in Flask
Split screen into 3 with keyhac