From dde95c3e4fa4000b76811e9a494b39541a7df146 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Sat, 9 Apr 2022 14:41:32 +0800 Subject: [PATCH] finish vt_mtl and vt_group Panel system --- ballance_blender_plugin/BMFILE_export.py | 27 +++++++++--------- ballance_blender_plugin/BMFILE_import.py | 6 ++-- .../NAMES_rename_system.py | 14 +++------- .../PROPS_virtools_group.py | 28 ++++++++++++++++--- ballance_blender_plugin/UTILS_constants.py | 6 +++- ballance_blender_plugin/UTILS_functions.py | 12 ++++---- .../UTILS_virtools_prop.py | 11 +++++--- 7 files changed, 62 insertions(+), 42 deletions(-) diff --git a/ballance_blender_plugin/BMFILE_export.py b/ballance_blender_plugin/BMFILE_export.py index dcfaff4..03bd376 100644 --- a/ballance_blender_plugin/BMFILE_export.py +++ b/ballance_blender_plugin/BMFILE_export.py @@ -2,7 +2,7 @@ import bpy,bmesh,bpy_extras,mathutils import pathlib,zipfile,time,os,tempfile,math import struct, shutil from bpy_extras import io_utils, node_shader_utils -from . import UTILS_constants, UTILS_functions, UTILS_file_io, UTILS_zip_helper +from . import UTILS_constants, UTILS_functions, UTILS_file_io, UTILS_zip_helper, UTILS_virtools_prop class BALLANCE_OT_export_bm(bpy.types.Operator, bpy_extras.io_utils.ExportHelper): """Save a Ballance Map File (BM file spec 1.4)""" @@ -118,8 +118,7 @@ def export_bm(context, bmx_filepath, prefs_fncg, opts_exportMode, opts_exportTar object_isHidden = not obj.visible_get() # try get grouping data - object_groupList = _try_get_custom_property(obj, 'virtools-group') - object_groupList = _set_value_when_none(object_groupList, []) + object_groupList = UTILS_virtools_prop.get_virtools_group_data(obj) # ======================= # write to files @@ -242,15 +241,17 @@ def export_bm(context, bmx_filepath, prefs_fncg, opts_exportMode, opts_exportTar UTILS_file_io.write_uint64(finfo, fmaterial.tell()) # try get original written data - material_colAmbient = _try_get_custom_property(material, 'virtools-ambient') - material_colDiffuse = _try_get_custom_property(material, 'virtools-diffuse') - material_colSpecular = _try_get_custom_property(material, 'virtools-specular') - material_colEmissive = _try_get_custom_property(material, 'virtools-emissive') - material_specularPower = _try_get_custom_property(material, 'virtools-power') + (material_colAmbient, material_colDiffuse, material_colSpecular, + material_colEmissive, material_specularPower) = UTILS_virtools_prop.get_virtools_material_data(material) # get basic color mat_wrap = node_shader_utils.PrincipledBSDFWrapper(material) if mat_wrap: + # we trying get texture data from Principled BSDF + # because bpy.types.Material.virtools_material now can provide + # Virtools material data stablely, so i annotate following code + # only keep texture data + ''' use_mirror = mat_wrap.metallic != 0.0 if use_mirror: material_colAmbient = _set_value_when_none(material_colAmbient, (mat_wrap.metallic, mat_wrap.metallic, mat_wrap.metallic)) @@ -260,6 +261,7 @@ def export_bm(context, bmx_filepath, prefs_fncg, opts_exportMode, opts_exportTar material_colSpecular = _set_value_when_none(material_colSpecular, (mat_wrap.specular, mat_wrap.specular, mat_wrap.specular)) material_colEmissive = _set_value_when_none(material_colEmissive, mat_wrap.emission_color[:3]) material_specularPower = _set_value_when_none(material_specularPower, 0.0) + ''' # confirm texture tex_wrap = getattr(mat_wrap, "base_color_texture", None) @@ -288,11 +290,14 @@ def export_bm(context, bmx_filepath, prefs_fncg, opts_exportMode, opts_exportTar else: # no Principled BSDF. write garbage + # same reason for disabling following code + ''' material_colAmbient = _set_value_when_none(material_colAmbient, (0.8, 0.8, 0.8)) material_colDiffuse = _set_value_when_none(material_colDiffuse, (0.8, 0.8, 0.8)) material_colSpecular = _set_value_when_none(material_colSpecular, (0.8, 0.8, 0.8)) material_colEmissive = _set_value_when_none(material_colEmissive, (0.8, 0.8, 0.8)) material_specularPower = _set_value_when_none(material_specularPower, 0.0) + ''' material_useTexture = False material_textureIndex = 0 @@ -356,12 +361,6 @@ def _mesh_triangulate(me): bm.to_mesh(me) bm.free() -def _try_get_custom_property(obj, field): - try: - return obj[field] - except: - return None - def _set_value_when_none(obj, newValue): if obj is None: return newValue diff --git a/ballance_blender_plugin/BMFILE_import.py b/ballance_blender_plugin/BMFILE_import.py index 31694da..82c8630 100644 --- a/ballance_blender_plugin/BMFILE_import.py +++ b/ballance_blender_plugin/BMFILE_import.py @@ -4,7 +4,7 @@ import struct, shutil from bpy_extras import io_utils,node_shader_utils from bpy_extras.io_utils import unpack_list from bpy_extras.image_utils import load_image -from . import UTILS_constants, UTILS_functions, UTILS_file_io, UTILS_zip_helper +from . import UTILS_constants, UTILS_functions, UTILS_file_io, UTILS_zip_helper, UTILS_virtools_prop class BALLANCE_OT_import_bm(bpy.types.Operator, bpy_extras.io_utils.ImportHelper): """Load a Ballance Map File (BM file spec 1.4)""" @@ -327,7 +327,9 @@ def import_bm(context, bmx_filepath, prefs_fncg, prefs_externalTexture, prefs_te # write custom property if len(object_groupList) != 0: - object_target['virtools-group'] = tuple(object_groupList) + UTILS_virtools_prop.set_virtools_group_data(object_target, tuple(object_groupList)) + else: + UTILS_virtools_prop.set_virtools_group_data(object_target, None) # update view layer after all objects has been imported blender_viewLayer.update() diff --git a/ballance_blender_plugin/NAMES_rename_system.py b/ballance_blender_plugin/NAMES_rename_system.py index 54436ae..1949754 100644 --- a/ballance_blender_plugin/NAMES_rename_system.py +++ b/ballance_blender_plugin/NAMES_rename_system.py @@ -1,5 +1,5 @@ import bpy -from . import UTILS_constants, UTILS_functions +from . import UTILS_constants, UTILS_functions, UTILS_virtools_prop class rename_system_props(bpy.types.Operator): name_standard: bpy.props.EnumProperty( @@ -114,12 +114,6 @@ class _NameInfoHelper(): def _get_selected_objects(): return bpy.context.view_layer.active_layer_collection.collection.objects -def _try_get_custom_property(obj, field): - try: - return obj[field] - except: - return None - def _get_sector_from_ckgroup(group_set): # this counter is served for stupid # multi-sector-grouping accident. @@ -248,8 +242,8 @@ def _get_name_info_from_imengyu_name(obj_name): return None def _get_name_info_from_group(obj): - group_list = _try_get_custom_property(obj, 'virtools-group') - if group_list is None: + group_list = UTILS_virtools_prop.get_virtools_group_data(obj) + if len(group_list) == 0: # name it as a decoration return _NameInfoHelper(_ObjectBasicType.DECORATION) @@ -442,7 +436,7 @@ def _set_for_group(obj, name_info): # apply to custom property - obj['virtools-group'] = tuple(gps) + UTILS_virtools_prop.set_virtools_group_data(obj, tuple(gps)) # ========================================== # assemble funcs diff --git a/ballance_blender_plugin/PROPS_virtools_group.py b/ballance_blender_plugin/PROPS_virtools_group.py index fb8d0ec..8c92645 100644 --- a/ballance_blender_plugin/PROPS_virtools_group.py +++ b/ballance_blender_plugin/PROPS_virtools_group.py @@ -13,7 +13,11 @@ class BALLANCE_OT_add_virtools_group(bpy.types.Operator): def execute(self, context): obj = context.object - UTILS_virtools_prop.set_virtools_group_data(obj, ("aaa", "bbb", "ccc")) + gp = UTILS_virtools_prop.get_virtools_group(obj) + item = gp.add() + item.name = "" + item.group_name = "CKGroup" + return {'FINISHED'} class BALLANCE_OT_rm_virtools_group(bpy.types.Operator): @@ -24,16 +28,32 @@ class BALLANCE_OT_rm_virtools_group(bpy.types.Operator): @classmethod def poll(self, context): - return context.object is not None + if context.object is None: + return False + try: + obj = context.object + gp = UTILS_virtools_prop.get_virtools_group(obj) + active_gp = UTILS_virtools_prop.get_active_virtools_group(obj) + data = gp[active_gp] + except: + return False + else: + return True + def execute(self, context): obj = context.object - print(UTILS_virtools_prop.get_virtools_group_data(obj)) + gp = UTILS_virtools_prop.get_virtools_group(obj) + active_gp = UTILS_virtools_prop.get_active_virtools_group(obj) + idx = int(active_gp) + + active_gp -= 1 + gp.remove(idx) return {'FINISHED'} class BALLANCE_UL_virtools_group(bpy.types.UIList): def draw_item(self, context, layout, data, item, icon, active_data, active_propname): - layout.prop(item, 'group_name', icon='GROUP', text="") + layout.prop(item, 'group_name', icon='GROUP', emboss=False, text="") class BALLANCE_PT_virtools_group(bpy.types.Panel): """Show Virtools Group Properties.""" diff --git a/ballance_blender_plugin/UTILS_constants.py b/ballance_blender_plugin/UTILS_constants.py index deb59cf..aa84fe4 100644 --- a/ballance_blender_plugin/UTILS_constants.py +++ b/ballance_blender_plugin/UTILS_constants.py @@ -293,4 +293,8 @@ rename_regexYYCComponent = re.compile('^(' + '|'.join(rename_normalComponentsGro rename_regexYYCPC = re.compile('^PC_TwoFlames_(0[1-7])$') rename_regexYYCPR = re.compile('^PR_Resetpoint_(0[1-8])$') rename_regexImengyuComponent = re.compile('^(' + '|'.join(rename_normalComponentsGroupName) + '):[^:]*:([1-9]|[1-9][0-9])$') -rename_regexImengyuPCRComp = re.compile('^(PC_CheckPoint|PR_ResetPoint):([0-9]+)$') \ No newline at end of file +rename_regexImengyuPCRComp = re.compile('^(PC_CheckPoint|PR_ResetPoint):([0-9]+)$') + + + + diff --git a/ballance_blender_plugin/UTILS_functions.py b/ballance_blender_plugin/UTILS_functions.py index 4ec0fb9..84c2d59 100644 --- a/ballance_blender_plugin/UTILS_functions.py +++ b/ballance_blender_plugin/UTILS_functions.py @@ -3,7 +3,7 @@ import struct, shutil, os from bpy_extras.io_utils import unpack_list from bpy_extras.image_utils import load_image from bpy_extras import io_utils, node_shader_utils -from . import UTILS_file_io, UTILS_constants +from . import UTILS_file_io, UTILS_constants, UTILS_virtools_prop # ================================= # scene operation @@ -65,12 +65,10 @@ def create_material_nodes(input_mtl, ambient, diffuse, specular, emissive, input_mtl.node_tree.links.new(inode.outputs[0], bnode.inputs[0]) # write custom property - input_mtl['virtools-ambient'] = ambient - input_mtl['virtools-diffuse'] = diffuse - input_mtl['virtools-specular'] = specular - input_mtl['virtools-emissive'] = emissive - input_mtl['virtools-power'] = specular_power - + UTILS_virtools_prop.set_virtools_material_data(input_mtl, + ambient, diffuse, specular, emissive, specular_power + ) + # ================================= # load component diff --git a/ballance_blender_plugin/UTILS_virtools_prop.py b/ballance_blender_plugin/UTILS_virtools_prop.py index 493154c..9aad32c 100644 --- a/ballance_blender_plugin/UTILS_virtools_prop.py +++ b/ballance_blender_plugin/UTILS_virtools_prop.py @@ -54,6 +54,8 @@ def set_virtools_material_data(mtl, ambient, diffuse, specular, emissive, specul data.emissive = emissive data.specular_power = specular_power +def get_active_virtools_group(obj): + return obj.active_virtools_group def get_virtools_group(obj): return obj.virtools_group @@ -64,10 +66,11 @@ def set_virtools_group_data(obj, new_data): data = get_virtools_group(obj) data.clear() - for item in new_data: - it = data.add() - it.name = "" - it.group_name = item + if new_data is not None: + for item in new_data: + it = data.add() + it.name = "" + it.group_name = item def register_props(): bpy.types.Object.virtools_group = bpy.props.CollectionProperty(type=BALLANCE_PG_virtools_group)