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.
This commit is contained in:
yyc12345 2022-04-12 15:40:06 +08:00
parent dde95c3e4f
commit 2950857e3d
8 changed files with 117 additions and 12 deletions

View File

@ -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)

View File

@ -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'],

View File

@ -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"

View File

@ -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")

View File

@ -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"
)

View File

@ -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

View File

@ -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

View File

@ -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
)