fix 2 issues

- update apply_virtools_material. use keywords, not index to visit input and output sockets.
- update blender mesh operator. send a message when mesh::validate() proceed mesh and stop custom normals assignment.
This commit is contained in:
yyc12345 2023-11-14 21:24:09 +08:00
parent bd40e0fdf2
commit 1f0444009f
2 changed files with 35 additions and 16 deletions

View File

@ -465,15 +465,28 @@ def apply_to_blender_material(mtl: bpy.types.Material):
mtl.node_tree.nodes.remove(node)
# create ballance-style blender material
# for sockets name, see `bpy_extras.node_shader_utils` for more infos
bnode: bpy.types.ShaderNodeBsdfPrincipled = mtl.node_tree.nodes.new(type = "ShaderNodeBsdfPrincipled")
mnode: bpy.types.ShaderNodeOutputMaterial = mtl.node_tree.nodes.new(type = "ShaderNodeOutputMaterial")
mtl.node_tree.links.new(bnode.outputs[0], mnode.inputs[0])
mtl.node_tree.links.new(bnode.outputs["BSDF"], mnode.inputs["Surface"])
# set basic colors
mtl.metallic = sum(rawdata.mAmbient.to_const_rgb()) / 3
mtl.diffuse_color = rawdata.mDiffuse.to_const_rgba()
metallic_value = sum(rawdata.mAmbient.to_const_rgb()) / 3
mtl.metallic = metallic_value
bnode.inputs["Metallic"].default_value = metallic_value
diffuse_value = rawdata.mDiffuse.to_const_rgba()
mtl.diffuse_color = diffuse_value
bnode.inputs["Base Color"].default_value = diffuse_value
mtl.specular_color = rawdata.mSpecular.to_const_rgb()
bnode.inputs["Emission"].default_value = rawdata.mEmissive.to_const_rgba()
mtl.specular_intensity = rawdata.mSpecularPower
bnode.inputs["Specular"].default_value = UTIL_functions.clamp_float(
rawdata.mSpecularPower, 0.0, 1.0
)
# set some alpha data
mtl.use_backface_culling = not rawdata.mEnableTwoSided
@ -484,13 +497,13 @@ def apply_to_blender_material(mtl: bpy.types.Material):
# basic texture setter
inode: bpy.types.ShaderNodeTexImage = mtl.node_tree.nodes.new(type = "ShaderNodeTexImage")
inode.image = rawdata.mTexture
mtl.node_tree.links.new(inode.outputs[0], bnode.inputs[0])
mtl.node_tree.links.new(inode.outputs["Color"], bnode.inputs["Base Color"])
# todo: sync texture mapping config here
# link alpha if necessary
if rawdata.mEnableAlphaBlend:
mtl.node_tree.links.new(inode.outputs[1], bnode.inputs[21])
mtl.node_tree.links.new(inode.outputs["Alpha"], bnode.inputs["Alpha"])
#endregion
@ -693,10 +706,10 @@ class BBP_PT_virtools_material(bpy.types.Panel):
layout.label(text="Texture Parameters")
layout.prop(props, 'texture', emboss = True)
if props.texture is not None:
# if we have texture, we show its virtools texture data
PROP_virtools_texture.draw_virtools_texture(props.texture, layout)
layout.separator()
# have texture, show texture settings and enclosed by a border.
boxlayout = layout.box()
boxlayout.label(text="Virtools Texture Settings")
PROP_virtools_texture.draw_virtools_texture(props.texture, boxlayout)
layout.prop(props, 'texture_blend_mode')
layout.prop(props, 'texture_min_mode')

View File

@ -432,7 +432,7 @@ class MeshWriter():
# push material data
for mtl in self.__mMtlSlot:
self.__mAssocMesh.materials.append(mtl)
# add corresponding count for vertex position
self.__mAssocMesh.vertices.add(len(self.__mVertexPos) // 3)
# add loops data, it is the sum count of indices
@ -480,16 +480,22 @@ class MeshWriter():
# validate mesh.
# it is IMPORTANT that do NOT delete custom data
# because we need use these data to set custom split normal later
self.__mAssocMesh.validate(clean_customdata = False)
validation_err: bool = self.__mAssocMesh.validate(clean_customdata = False)
# update mesh without mesh calc
self.__mAssocMesh.update(calc_edges = False, calc_edges_loose = False)
# set custom split normal data
self.__mAssocMesh.normals_split_custom_set(
tuple(_nest_custom_split_normal(self.__mFaceNmlIndices, self.__mVertexNormal))
)
# enable auto smooth. it is IMPORTANT
self.__mAssocMesh.use_auto_smooth = True
# if the validate() change the mesh, skip this and output error.
# this should not happend in normal case,
# just a stupid patch for "线框Level1.Level.NMO" loading.
if not validation_err:
self.__mAssocMesh.normals_split_custom_set(
tuple(_nest_custom_split_normal(self.__mFaceNmlIndices, self.__mVertexNormal))
)
# enable auto smooth. it is IMPORTANT
self.__mAssocMesh.use_auto_smooth = True
else:
print(f'The custom normals of mesh "{self.__mAssocMesh.name}" can not be assigned because validate() changes the mesh. Check your mesh data first!')
def __clear_mesh(self):
if not self.is_valid():