refactor: splitting in multifiles format addon

This commit is contained in:
yannk 2023-03-21 21:39:30 +01:00
parent 0c4e0e476a
commit 8a39fc5378
11 changed files with 165 additions and 82 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
.directory
.vscode/
__pycache__/

View file

@ -28,89 +28,12 @@ bl_info = {
"category": "Generic" "category": "Generic"
} }
import os
import bpy
# global variable to store icons in
custom_icons = {}
# UI__init__.py
class MY_PT_khanat_tools(bpy.types.Panel):
bl_label = 'Khanat tools'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_context = ''
bl_category = 'Khanat'
def draw_header(self, context):
global custom_icons
print(custom_icons)
layout = self.layout
layout.label(icon_value=custom_icons["khanat"].icon_id)
def draw(self, context):
layout = self.layout
row = layout.row()
row.operator(MY_OT_khanat_export_gltf.bl_idname)
layout.separator()
row = layout.row()
row.operator(MY_OT_khanat_get_online_doc.bl_idname, icon_value=72)
layout.separator()
# Operators
class MY_OT_khanat_export_gltf(bpy.types.Operator):
"""Export collections to gltf"""
bl_idname = "object.export2gltf"
bl_label = "Export to gltf"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
return {"FINISHED"}
def invoke(self, context, event):
return self.execute(context)
class MY_OT_khanat_get_online_doc(bpy.types.Operator):
"""Check online documentation"""
bl_idname = "object.readthedocs"
bl_label = "Online documentation"
bl_description = "Go to Khanat Development Guide"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
bpy.ops.wm.url_open('INVOKE_DEFAULT', url='https://git.numenaute.org/khaganat/mmorpg_khanat/khanat_gamedev_guide')
return {"FINISHED"}
def invoke(self, context, event):
return self.execute(context)
# Register / Unregister
def register(): def register():
global custom_icons from addon.register import register_addon
custom_icons = bpy.utils.previews.new() register_addon()
addon_path = os.path.dirname(__file__)
icons_dir = os.path.join(addon_path, "icons")
custom_icons.load("khanat", os.path.join(icons_dir, "khanat.png"), 'IMAGE')
bpy.utils.register_class(MY_OT_khanat_export_gltf)
bpy.utils.register_class(MY_OT_khanat_get_online_doc)
bpy.utils.register_class(MY_PT_khanat_tools)
def unregister(): def unregister():
global custom_icons from addon.register import unregister_addon
bpy.utils.previews.remove(custom_icons) unregister_addon()
bpy.utils.register_class(MY_OT_khanat_export_gltf)
bpy.utils.register_class(MY_OT_khanat_get_online_doc)
bpy.utils.register_class(MY_PT_khanat_tools)
if __name__ == "__main__":
__file__ = bpy.data.filepath
register()

0
addon/__init__.py Normal file
View file

29
addon/icons/__init__.py Normal file
View file

@ -0,0 +1,29 @@
import bpy
import os
icons_collection = None
icons_directory = os.path.dirname(__file__)
def initialize_icons_collection():
import bpy.utils.previews
global icons_collection
print("icons_collection : {}".format(icons_collection))
icons_collection = bpy.utils.previews.new()
def unload_icons():
bpy.utils.previews.remove(icons_collection)
def get_icon_id(identifier):
# The initialize_icons_collection function needs to be called first.
return get_icon(identifier).icon_id
def get_icon(identifier):
if identifier in icons_collection:
return icons_collection[identifier]
return icons_collection.load(identifier, os.path.join(icons_directory, identifier + ".png"), "IMAGE")
def register_icons():
initialize_icons_collection()
def unregister_icons():
unload_icons()

View file

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 5.3 KiB

18
addon/menus/__init__.py Normal file
View file

@ -0,0 +1,18 @@
import bpy
from .panel_main import KH_PT_panel_main
classes = (
KH_PT_panel_main,
)
def register_menus():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
def unregister_menus():
from bpy.utils import unregister_class
for cls in classes:
unregister_class(cls)

26
addon/menus/panel_main.py Normal file
View file

@ -0,0 +1,26 @@
import bpy
from ..operators import readthedocs, export2godot
from ..icons import get_icon_id
class KH_PT_panel_main(bpy.types.Panel):
bl_label = 'Khanat tools'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_context = ''
bl_category = 'Khanat'
def draw_header(self, context):
layout = self.layout
layout.label(icon_value=get_icon_id("khanat"))
def draw(self, context):
layout = self.layout
row = layout.row()
row.operator(export2godot.KH_OP_export2godot.bl_idname)
layout.separator()
row = layout.row()
row.operator(readthedocs.KH_OP_readthedocs.bl_idname, icon_value=72)
layout.separator()

View file

@ -0,0 +1,20 @@
import bpy
from .readthedocs import KH_OP_readthedocs
from .export2godot import KH_OP_export2godot
classes = (
KH_OP_readthedocs,
KH_OP_export2godot
)
def register_operators():
print(classes)
from bpy.utils import register_class
for cls in classes:
register_class(cls)
def unregister_operators():
from bpy.utils import unregister_class
for cls in classes:
unregister_class(cls)

View file

@ -0,0 +1,14 @@
import bpy
class KH_OP_export2godot(bpy.types.Operator):
"""Export collections to Godot throught glTF"""
bl_idname = "kh.export2godot"
bl_label = "Export to gltf"
bl_options = {'REGISTER', 'UNDO'}
def invoke(self, context, event):
return self.execute(context)
def execute(self, context):
print("TOTOR IS NOT HERE")
return {"FINISHED"}

View file

@ -0,0 +1,20 @@
import bpy
class KH_OP_readthedocs(bpy.types.Operator):
"""Check online documentation"""
bl_idname = "kh.readthedocs"
bl_label = "Online documentation"
bl_description = "Go to Khanat Development Guide"
bl_options = {"REGISTER", "UNDO"}
def invoke(self, context, event):
return self.execute(context)
def execute(self, context):
bpy.ops.wm.url_open('INVOKE_DEFAULT', url='https://git.numenaute.org/khaganat/mmorpg_khanat/khanat_gamedev_guide')
return {"FINISHED"}

View file

@ -0,0 +1,30 @@
def register_addon():
# Icons
from ..icons import register_icons
register_icons()
# Operators
from ..operators import register_operators
register_operators()
# Menus
from ..menus import register_menus
register_menus()
def unregister_addon():
# Menus
from ..menus import unregister_menus
unregister_menus()
# Operators
from ..operators import unregister_operators
unregister_operators()
# Icons
from ..icons import unregister_icons
unregister_icons()