From 2950857e3dc8e096c7b9d20c407b1e689ba3e793 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Tue, 12 Apr 2022 15:40:06 +0800 Subject: [PATCH] do some improvement work - split blender material creation function. - allow user to apply virtools material to blender nodes in operator. - add quick group picker in add virtools group operator. --- ballance_blender_plugin/BMFILE_import.py | 2 +- ballance_blender_plugin/OBJS_add_floors.py | 2 +- .../PROPS_virtools_group.py | 15 ++++- .../PROPS_virtools_material.py | 20 +++++++ ballance_blender_plugin/UTILS_constants.py | 55 ++++++++++++++++++- ballance_blender_plugin/UTILS_functions.py | 23 ++++++-- .../UTILS_virtools_prop.py | 11 +++- ballance_blender_plugin/__init__.py | 1 + 8 files changed, 117 insertions(+), 12 deletions(-) diff --git a/ballance_blender_plugin/BMFILE_import.py b/ballance_blender_plugin/BMFILE_import.py index 82c8630..4a7ca75 100644 --- a/ballance_blender_plugin/BMFILE_import.py +++ b/ballance_blender_plugin/BMFILE_import.py @@ -168,7 +168,7 @@ def import_bm(context, bmx_filepath, prefs_fncg, prefs_externalTexture, prefs_te continue # try create material nodes - UTILS_functions.create_material_nodes(material_target, + UTILS_functions.create_blender_material(material_target, material_colAmbient, material_colDiffuse, material_colSpecular, material_colEmissive, material_specularPower, textureList[material_texture].blender_data if material_useTexture else None) diff --git a/ballance_blender_plugin/OBJS_add_floors.py b/ballance_blender_plugin/OBJS_add_floors.py index 297c586..7e7ec77 100644 --- a/ballance_blender_plugin/OBJS_add_floors.py +++ b/ballance_blender_plugin/OBJS_add_floors.py @@ -214,7 +214,7 @@ def _create_or_get_material(material_name, prefs_externalTexture): if material_name in try_item['member']: # got it # set material data - UTILS_functions.create_material_nodes(mtl, + UTILS_functions.create_blender_material(mtl, try_item['data']['ambient'], try_item['data']['diffuse'], try_item['data']['specular'], try_item['data']['emissive'], try_item['data']['power'], diff --git a/ballance_blender_plugin/PROPS_virtools_group.py b/ballance_blender_plugin/PROPS_virtools_group.py index 8c92645..64279e2 100644 --- a/ballance_blender_plugin/PROPS_virtools_group.py +++ b/ballance_blender_plugin/PROPS_virtools_group.py @@ -7,6 +7,12 @@ class BALLANCE_OT_add_virtools_group(bpy.types.Operator): bl_label = "Add Virtools Group" bl_options = {'UNDO'} + group_name: bpy.props.EnumProperty( + name="Group Name", + description="Group name. For custom group name, please pick `CustomCKGroup` and change it later.", + items=tuple((x, x, "") for x in UTILS_constants.propsVtGroups_availableGroups), + ) + @classmethod def poll(self, context): return context.object is not None @@ -16,10 +22,17 @@ class BALLANCE_OT_add_virtools_group(bpy.types.Operator): gp = UTILS_virtools_prop.get_virtools_group(obj) item = gp.add() item.name = "" - item.group_name = "CKGroup" + item.group_name = str(self.group_name) return {'FINISHED'} + def invoke(self, context, event): + wm = context.window_manager + return wm.invoke_props_dialog(self) + + def draw(self, context): + self.layout.prop(self, 'group_name') + class BALLANCE_OT_rm_virtools_group(bpy.types.Operator): """Remove a Virtools Group for Active Object.""" bl_idname = "ballance.rm_virtools_group" diff --git a/ballance_blender_plugin/PROPS_virtools_material.py b/ballance_blender_plugin/PROPS_virtools_material.py index c07097d..9977d56 100644 --- a/ballance_blender_plugin/PROPS_virtools_material.py +++ b/ballance_blender_plugin/PROPS_virtools_material.py @@ -1,6 +1,23 @@ import bpy from . import UTILS_constants, UTILS_functions, UTILS_virtools_prop +class BALLANCE_OT_apply_virtools_material(bpy.types.Operator): + """Apply Virtools Material to Blender Material.""" + bl_idname = "ballance.apply_virtools_material" + bl_label = "Apply Virtools Material" + bl_options = {'UNDO'} + + @classmethod + def poll(cls, context): + return context.material is not None + + def execute(self, context): + mtl = context.material + mtl_data = UTILS_virtools_prop.get_virtools_material_data(mtl) + UTILS_functions.create_material_nodes(mtl, *mtl_data) + + return {'FINISHED'} + class BALLANCE_PT_virtools_material(bpy.types.Panel): """Show Virtools Material Properties.""" bl_label = "Virtools Material" @@ -18,9 +35,12 @@ class BALLANCE_PT_virtools_material(bpy.types.Panel): #target = bpy.context.active_object.active_material target = UTILS_virtools_prop.get_virtools_material(context.material) + layout.prop(target, 'texture', emboss=True) layout.prop(target, 'ambient') layout.prop(target, 'diffuse') layout.prop(target, 'specular') layout.prop(target, 'emissive') layout.prop(target, 'specular_power') + layout.operator("ballance.apply_virtools_material", icon="NODETREE") + diff --git a/ballance_blender_plugin/UTILS_constants.py b/ballance_blender_plugin/UTILS_constants.py index aa84fe4..de409b4 100644 --- a/ballance_blender_plugin/UTILS_constants.py +++ b/ballance_blender_plugin/UTILS_constants.py @@ -295,6 +295,59 @@ 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]+)$') - +propsVtGroups_availableGroups = ( + "Sector_01", + "Sector_02", + "Sector_03", + "Sector_04", + "Sector_05", + "Sector_06", + "Sector_07", + "Sector_08", + + "P_Extra_Life", + "P_Extra_Point", + "P_Trafo_Paper", + "P_Trafo_Stone", + "P_Trafo_Wood", + "P_Ball_Paper", + "P_Ball_Stone", + "P_Ball_Wood", + "P_Box", + "P_Dome", + "P_Modul_01", + "P_Modul_03", + "P_Modul_08", + "P_Modul_17", + "P_Modul_18", + "P_Modul_19", + "P_Modul_25", + "P_Modul_26", + "P_Modul_29", + "P_Modul_30", + "P_Modul_34", + "P_Modul_37", + "P_Modul_41", + + "PS_Levelstart", + "PE_Levelende", + "PC_Checkpoints", + "PR_Resetpoints", + + "Sound_HitID_01", + "Sound_RollID_01", + "Sound_HitID_02", + "Sound_RollID_02", + "Sound_HitID_03", + "Sound_RollID_03", + + "DepthTestCubes", + + "Phys_Floors", + "Phys_FloorRails", + "Phys_FloorStopper", + + "CustomCKGroup" +) diff --git a/ballance_blender_plugin/UTILS_functions.py b/ballance_blender_plugin/UTILS_functions.py index 84c2d59..cde045d 100644 --- a/ballance_blender_plugin/UTILS_functions.py +++ b/ballance_blender_plugin/UTILS_functions.py @@ -42,13 +42,29 @@ def get_component_id(name): # ================================= # create material -def create_material_nodes(input_mtl, ambient, diffuse, specular, emissive, +def create_blender_material(input_mtl, ambient, diffuse, specular, emissive, specular_power, texture): # adding material nodes + create_material_nodes(input_mtl, + ambient, diffuse, specular, emissive, specular_power, texture + ) + + # write custom property + UTILS_virtools_prop.set_virtools_material_data(input_mtl, + ambient, diffuse, specular, emissive, specular_power, texture + ) + +def create_material_nodes(input_mtl, ambient, diffuse, specular, emissive, + specular_power, texture): + + # enable nodes mode input_mtl.use_nodes=True + # delete all existed nodes for node in input_mtl.node_tree.nodes: input_mtl.node_tree.nodes.remove(node) + + # create ballance-style blender material bnode = input_mtl.node_tree.nodes.new(type="ShaderNodeBsdfPrincipled") mnode = input_mtl.node_tree.nodes.new(type="ShaderNodeOutputMaterial") input_mtl.node_tree.links.new(bnode.outputs[0],mnode.inputs[0]) @@ -64,11 +80,6 @@ def create_material_nodes(input_mtl, ambient, diffuse, specular, emissive, inode.image = texture input_mtl.node_tree.links.new(inode.outputs[0], bnode.inputs[0]) - # write custom property - 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 9aad32c..816ee1f 100644 --- a/ballance_blender_plugin/UTILS_virtools_prop.py +++ b/ballance_blender_plugin/UTILS_virtools_prop.py @@ -33,6 +33,12 @@ class BALLANCE_PG_virtools_material(bpy.types.PropertyGroup): default=0.0, ) + texture: bpy.props.PointerProperty( + type=bpy.types.Image, + name="Texture", + description="The texture used for Virtools material" + ) + class BALLANCE_PG_virtools_group(bpy.types.PropertyGroup): group_name: bpy.props.StringProperty( name="Group Name", @@ -44,15 +50,16 @@ def get_virtools_material(mtl): def get_virtools_material_data(mtl): data = get_virtools_material(mtl) - return (data.ambient, data.diffuse, data.specular, data.emissive, data.specular_power) + return (data.ambient, data.diffuse, data.specular, data.emissive, data.specular_power, data.texture) -def set_virtools_material_data(mtl, ambient, diffuse, specular, emissive, specular_power): +def set_virtools_material_data(mtl, ambient, diffuse, specular, emissive, specular_power, texture): data = get_virtools_material(mtl) data.ambient = ambient data.diffuse = diffuse data.specular = specular data.emissive = emissive data.specular_power = specular_power + data.texture = texture def get_active_virtools_group(obj): return obj.active_virtools_group diff --git a/ballance_blender_plugin/__init__.py b/ballance_blender_plugin/__init__.py index d212e51..8be5efa 100644 --- a/ballance_blender_plugin/__init__.py +++ b/ballance_blender_plugin/__init__.py @@ -148,6 +148,7 @@ classes = ( PROPS_virtools_group.BALLANCE_OT_rm_virtools_group, PROPS_virtools_group.BALLANCE_UL_virtools_group, PROPS_virtools_group.BALLANCE_PT_virtools_group, + PROPS_virtools_material.BALLANCE_OT_apply_virtools_material, PROPS_virtools_material.BALLANCE_PT_virtools_material )