diff --git a/io_mesh_w3d/__init__.py b/io_mesh_w3d/__init__.py index a83904ee..4b2f2c91 100644 --- a/io_mesh_w3d/__init__.py +++ b/io_mesh_w3d/__init__.py @@ -298,7 +298,7 @@ def draw(self, context): col = layout.column() col.prop(mat, 'surface_type') col = layout.column() - col.prop(mat, 'blend_mode') + col.prop(mat, 'blend_method') col = layout.column() col.prop(mat, 'ambient') @@ -370,7 +370,7 @@ def draw(self, context): col = layout.column() col.prop(mat, 'damaged_texture') col = layout.column() - col.prop(mat, 'secondary_texture_blend_mode') + col.prop(mat, 'secondary_texture_blend_method') col = layout.column() col.prop(mat, 'tex_coord_mapper_0') col = layout.column() diff --git a/io_mesh_w3d/common/utils/helpers.py b/io_mesh_w3d/common/utils/helpers.py index 66360bd8..3c422209 100644 --- a/io_mesh_w3d/common/utils/helpers.py +++ b/io_mesh_w3d/common/utils/helpers.py @@ -82,6 +82,8 @@ def create_uvlayer(context, mesh, b_mesh, tris, mat_pass): context.warning('only one texture stage per material pass supported') if tx_coords is None: + if mesh is not None: + uv_layer = mesh.uv_layers.new(do_init=False) return uv_layer = mesh.uv_layers.new(do_init=False) @@ -96,6 +98,7 @@ def create_uvlayer_2(context, mesh, b_mesh, tris, mat_pass): if mat_pass.tx_coords_2: tx_coords_2 = mat_pass.tx_coords_2 else: + uv_layer = mesh.uv_layers.new(do_init=False) return uv_layer = mesh.uv_layers.new(do_init=False) diff --git a/io_mesh_w3d/common/utils/material_import.py b/io_mesh_w3d/common/utils/material_import.py index 5e7730c9..0f673f30 100644 --- a/io_mesh_w3d/common/utils/material_import.py +++ b/io_mesh_w3d/common/utils/material_import.py @@ -13,24 +13,71 @@ # vertex material ########################################################################## -def create_vertex_material(context, principleds, structure, mesh, b_mesh, name, triangles): - for vertMat in structure.vert_materials: - (material, principled) = create_material_from_vertex_material(name, vertMat) - mesh.materials.append(material) - principleds.append(principled) - - for mat_pass in structure.material_passes: - create_uvlayer(context, mesh, b_mesh, triangles, mat_pass) - create_uvlayer_2(context, mesh, b_mesh, triangles, mat_pass) - - if mat_pass.tx_stages: - tx_stage = mat_pass.tx_stages[0] - mat_id = mat_pass.vertex_material_ids[0] - tex_id = tx_stage.tx_ids[0][0] +def create_vertex_material(context, principleds, structure, mesh, b_mesh, name, triangles, mesh_ob): + + if len(structure.material_passes) == 1 and len( + structure.textures) > 1: # condition for multiple materials per single mesh object + # Create the same amount of materials as textures used for this mesh + source_mat = structure.vert_materials[0] + for texture in structure.textures: + source_mat.vm_name = texture.id + (material, principled) = create_material_from_vertex_material(name, source_mat) + mesh.materials.append(material) + principleds.append(principled) + + create_uvlayer(context, mesh, b_mesh, triangles, structure.material_passes[0]) + + # Load textures + for tex_id, texture in enumerate(structure.textures): texture = structure.textures[tex_id] tex = find_texture(context, texture.file, texture.id) - principleds[mat_id].base_color_texture.image = tex - principleds[mat_id].base_color_texture.image.name = texture.file + node_tree = mesh.materials[tex_id].node_tree + bsdf_node = node_tree.nodes.get('Principled BSDF') + texture_node = node_tree.nodes.new('ShaderNodeTexImage') + texture_node.image = tex + texture_node.location = (-350, 300) + links = node_tree.links + links.new(texture_node.outputs['Color'], bsdf_node.inputs['Base Color']) + links.new(texture_node.outputs['Alpha'], bsdf_node.inputs['Alpha']) + + # Assign material to appropriate object faces + bpy.ops.object.mode_set(mode='EDIT') + bm = bmesh.from_edit_mesh(mesh_ob.data) + bm.faces.ensure_lookup_table() + for i, face in enumerate(bm.faces): + if(i < len(structure.material_passes[0].tx_stages[0].tx_ids[0])): + bm.faces[i].material_index = structure.material_passes[0].tx_stages[0].tx_ids[0][i] + else: + bm.faces[i].material_index = structure.material_passes[0].tx_stages[0].tx_ids[0][0] + bpy.ops.object.mode_set(mode='OBJECT') + else: + for vertMat in structure.vert_materials: + (material, principled) = create_material_from_vertex_material(name, vertMat) + mesh.materials.append(material) + principleds.append(principled) + + for mat_pass in structure.material_passes: + create_uvlayer(context, mesh, b_mesh, triangles, mat_pass) + + if mat_pass.tx_stages: + tx_stage = mat_pass.tx_stages[0] + mat_id = mat_pass.vertex_material_ids[0] + tex_id = tx_stage.tx_ids[0][0] + texture = structure.textures[tex_id] + tex = find_texture(context, texture.file, texture.id) + node_tree = mesh.materials[tex_id].node_tree + bsdf_node = node_tree.nodes.get('Principled BSDF') + texture_node = node_tree.nodes.new('ShaderNodeTexImage') + texture_node.image = tex + texture_node.location = (-350, 300) + links = node_tree.links + links.new(texture_node.outputs['Color'], bsdf_node.inputs['Base Color']) + links.new(texture_node.outputs['Alpha'], bsdf_node.inputs['Alpha']) + + # Iterate through all materials and set their blend mode to Alpha Clip for transparency + for material in mesh.materials: + if material: + material.blend_method = 'CLIP' def create_material_from_vertex_material(name, vert_mat): @@ -43,7 +90,6 @@ def create_material_from_vertex_material(name, vert_mat): material = bpy.data.materials.new(name) material.material_type = 'VERTEX_MATERIAL' material.use_nodes = True - material.blend_method = 'BLEND' material.show_transparent_back = False attributes = {'DEFAULT'} @@ -91,7 +137,6 @@ def create_material_from_shader_material(context, name, shader_mat): material = bpy.data.materials.new(name) material.material_type = 'SHADER_MATERIAL' material.use_nodes = True - material.blend_method = 'BLEND' material.show_transparent_back = False material.technique = shader_mat.header.technique diff --git a/io_mesh_w3d/common/utils/mesh_import.py b/io_mesh_w3d/common/utils/mesh_import.py index b7f714c9..ea7c37e6 100644 --- a/io_mesh_w3d/common/utils/mesh_import.py +++ b/io_mesh_w3d/common/utils/mesh_import.py @@ -83,14 +83,14 @@ def create_mesh(context, mesh_struct, coll): if mesh_struct.vert_materials: create_vertex_material( - context, principleds, mesh_struct, mesh, b_mesh, actual_mesh_name, triangles) + context, principleds, mesh_struct, mesh, b_mesh, actual_mesh_name, triangles, mesh_ob) for i, shader in enumerate(mesh_struct.shaders): set_shader_properties(mesh.materials[min(i, len(mesh.materials) - 1)], shader) elif mesh_struct.prelit_vertex: create_vertex_material(context, principleds, mesh_struct.prelit_vertex, - mesh, b_mesh, actual_mesh_name, triangles) + mesh, b_mesh, actual_mesh_name, triangles, mesh_ob) for i, shader in enumerate(mesh_struct.prelit_vertex.shaders): set_shader_properties(mesh.materials[i], shader) diff --git a/tests/w3d/helpers/mesh_structs/material_pass.py b/tests/w3d/helpers/mesh_structs/material_pass.py index b4ec5bb9..e4541f2a 100644 --- a/tests/w3d/helpers/mesh_structs/material_pass.py +++ b/tests/w3d/helpers/mesh_structs/material_pass.py @@ -30,7 +30,7 @@ def get_per_face_txcoords(): def get_texture_stage(index=0): return TextureStage( - tx_ids=[[index]], + tx_ids=[[index] + [index] * max(0, index - 1)], per_face_tx_coords=[get_per_face_txcoords()], tx_coords=[get_uvs()])