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

View File

@ -480,16 +480,22 @@ class MeshWriter():
# validate mesh. # validate mesh.
# it is IMPORTANT that do NOT delete custom data # it is IMPORTANT that do NOT delete custom data
# because we need use these data to set custom split normal later # 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 # update mesh without mesh calc
self.__mAssocMesh.update(calc_edges = False, calc_edges_loose = False) self.__mAssocMesh.update(calc_edges = False, calc_edges_loose = False)
# set custom split normal data # set custom split normal data
self.__mAssocMesh.normals_split_custom_set( # if the validate() change the mesh, skip this and output error.
tuple(_nest_custom_split_normal(self.__mFaceNmlIndices, self.__mVertexNormal)) # this should not happend in normal case,
) # just a stupid patch for "线框Level1.Level.NMO" loading.
# enable auto smooth. it is IMPORTANT if not validation_err:
self.__mAssocMesh.use_auto_smooth = True 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): def __clear_mesh(self):
if not self.is_valid(): if not self.is_valid():