add addon pref and file browser wrapper

This commit is contained in:
yyc12345 2023-10-14 11:48:41 +08:00
parent 5584bc66a0
commit b94e7e2b22
4 changed files with 190 additions and 21 deletions

View File

@ -742,9 +742,26 @@ class BBP_PT_virtools_material(bpy.types.Panel):
layout.prop(props, 'z_func')
g_BldClasses: tuple[typing.Any, ...] = (
# basic property
BBP_PG_virtools_material,
# 2 operator used in panel
BBP_OT_apply_virtools_material,
BBP_OT_preset_virtools_material,
# panel
BBP_PT_virtools_material,
)
def register_prop():
def register():
for cls in g_BldClasses:
bpy.utils.register_class(cls)
# add into material metadata
bpy.types.Material.virtools_material = bpy.props.PointerProperty(type = BBP_PG_virtools_material)
def unregister_prop():
def unregister():
# del from material metadata
del bpy.types.Material.virtools_material
for cls in g_BldClasses:
bpy.utils.unregister_class(cls)

View File

@ -0,0 +1,98 @@
import bpy, bpy_extras
## File Browser Usage
# These created file browser is just a futher wrapper of `bpy_extras.io_utils.ExportHelper`
# So user must use it like ExportHelper. It mean inhert it and no need to write invoke function.
#
# These wrapper also provide general visitor for getting input file name or directory:
# * general_get_filename()
# * general_get_directory()
#
# For example:
# ```
# class BBP_OT_custom_import(bpy.types.Operator, UTIL_file_browser.OpenBmxFile)
#
# def execute(self, context):
# print(self.general_get_filename()) # get file name if support
# print(self.general_get_directory()) # get file name if support
#
# ```
class ImportBallanceImage(bpy_extras.io_utils.ImportHelper):
# no need to set file ext because we support multiple file ext.
# see ImportGLTF2 for more info.
# filename_ext = ".bmp"
# set with 2 file ext with ; as spelittor
# see ImportGLTF2 for more info.
filter_glob: bpy.props.StringProperty(
default = "*.bmp;*.tga",
options = {'HIDDEN'}
)
def general_get_filename(self) -> str:
return self.filepath
class ImportBmxFile(bpy_extras.io_utils.ImportHelper):
# set file ext filter
filename_ext = ".bmx"
filter_glob: bpy.props.StringProperty(
default = "*.bmx",
options = {'HIDDEN'}
)
def general_get_filename(self) -> str:
return self.filepath
class ExportBmxFile(bpy_extras.io_utils.ExportHelper):
# set file ext filter
filename_ext = ".bmx"
filter_glob: bpy.props.StringProperty(
default = "*.bmx",
options = {'HIDDEN'}
)
def general_get_filename(self) -> str:
return self.filepath
class ImportVirtoolsFile(bpy_extras.io_utils.ImportHelper):
# we support multiple file ext, set like ImportBallanceImage
# filename_ext = ".nmo"
filter_glob: bpy.props.StringProperty(
default = "*.nmo;*.cmo;*.vmo",
options = {'HIDDEN'}
)
def general_get_filename(self) -> str:
return self.filepath
class ExportVirtoolsFile(bpy_extras.io_utils.ExportHelper):
# only support export nmo file
filename_ext = ".nmo"
filter_glob: bpy.props.StringProperty(
default = "*.nmo",
options = {'HIDDEN'}
)
def general_get_filename(self) -> str:
return self.filepath
class ImportDirectory(bpy_extras.io_utils.ImportHelper):
# add directory prop to receive directory
directory: bpy.props.StringProperty()
# blank filter
filter_glob: bpy.props.StringProperty(
default = "",
options = {'HIDDEN'}
)
def general_get_directory(self) -> str:
return self.directory

View File

@ -0,0 +1,55 @@
import bpy
import os
class RawPreferences():
mBallanceTextureFolder: str
mNoComponentCollection: str
def __init__(self, **kwargs):
self.mBallanceTextureFolder = kwargs.get("mBallanceTextureFolder", "")
self.mNoComponentCollection = kwargs.get("mNoComponentCollection", "")
def has_valid_blc_tex_folder(self) -> bool:
return os.path.isdir(self.mBallanceTextureFolder)
class BBPPreferences(bpy.types.AddonPreferences):
bl_idname = __package__
ballance_texture_folder: bpy.props.StringProperty(
name = "Ballance Texture Folder",
description = "The path to folder which will be used by this plugin to get external Ballance texture.",
subtype='DIR_PATH'
)
no_component_collection: bpy.props.StringProperty(
name = "No Component Collection",
description = "(Import) The object which stored in this collectiion will not be saved as component. (Export) All forced no component objects will be stored in this collection",
)
def draw(self, context):
layout = self.layout
row = layout.row()
col = row.column()
col.label(text = "Ballance Texture Folder")
col.prop(self, "ballance_texture_folder", text = "")
col.label(text = "No Component Collection")
col.prop(self, "no_component_collection", text = "")
def get_preferences() -> BBPPreferences:
return bpy.context.preferences.addons[__package__].preferences
def get_raw_preferences() -> RawPreferences:
pref: BBPPreferences = get_preferences()
rawdata: RawPreferences = RawPreferences()
rawdata.mBallanceTextureFolder = pref.ballance_texture_folder
rawdata.mNoComponentCollection = pref.no_component_collection
return rawdata
def register() -> None:
bpy.utils.register_class(BBPPreferences)
def unregister() -> None:
bpy.utils.unregister_class(BBPPreferences)

View File

@ -23,6 +23,7 @@ if "bpy" in locals():
#endregion
from . import UTIL_preferences
from . import PROP_virtools_material
from . import OP_UV_flatten_uv
@ -52,14 +53,9 @@ def menu_drawer_view3d(self, context):
#region Register and Unregister.
g_Classes: tuple[typing.Any, ...] = (
g_BldClasses: tuple[typing.Any, ...] = (
OP_UV_flatten_uv.BBP_OT_flatten_uv,
BBP_MT_View3DMenu,
PROP_virtools_material.BBP_PG_virtools_material,
PROP_virtools_material.BBP_OT_apply_virtools_material,
PROP_virtools_material.BBP_OT_preset_virtools_material,
PROP_virtools_material.BBP_PT_virtools_material,
)
class MenuEntry():
@ -70,20 +66,22 @@ class MenuEntry():
self.mContainerMenu = cont
self.mIsAppend = is_append
self.mMenuDrawer = menu_func
g_Menus: tuple[MenuEntry, ...] = (
g_BldMenus: tuple[MenuEntry, ...] = (
MenuEntry(bpy.types.VIEW3D_MT_editor_menus, False, menu_drawer_view3d),
)
def register() -> None:
# register all classes
for cls in g_Classes:
# register module
UTIL_preferences.register()
PROP_virtools_material.register()
# register other classes
for cls in g_BldClasses:
bpy.utils.register_class(cls)
# register properties
PROP_virtools_material.register_prop()
# add menu drawer
for entry in g_Menus:
for entry in g_BldMenus:
if entry.mIsAppend:
entry.mContainerMenu.append(entry.mMenuDrawer)
else:
@ -91,16 +89,17 @@ def register() -> None:
def unregister() -> None:
# remove menu drawer
for entry in g_Menus:
for entry in g_BldMenus:
entry.mContainerMenu.remove(entry.mMenuDrawer)
# unregister properties
PROP_virtools_material.unregister_prop()
# unregister classes
for cls in g_Classes:
# unregister other classes
for cls in g_BldClasses:
bpy.utils.unregister_class(cls)
# unregister modules
PROP_virtools_material.unregister()
UTIL_preferences.unregister()
if __name__ == "__main__":
register()