Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple materials per single mesh object support #251

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 40 additions & 14 deletions io_mesh_w3d/common/utils/material_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,48 @@
# 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)

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[tex_id].base_color_texture.image = tex

# 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):
bm.faces[i].material_index = structure.material_passes[0].tx_stages[0].tx_ids[0][i]
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)
principleds[mat_id].base_color_texture.image = tex


def create_material_from_vertex_material(name, vert_mat):
Expand Down
4 changes: 2 additions & 2 deletions io_mesh_w3d/common/utils/mesh_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ def create_mesh(context, mesh_struct, coll):
b_mesh.from_mesh(mesh)

if mesh_struct.vert_materials:
create_vertex_material(context, principleds, mesh_struct, mesh, b_mesh, name, triangles)
create_vertex_material(context, principleds, mesh_struct, mesh, b_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, name, triangles)
create_vertex_material(context, principleds, mesh_struct.prelit_vertex, mesh, b_mesh, name, triangles, mesh_ob)

for i, shader in enumerate(mesh_struct.prelit_vertex.shaders):
set_shader_properties(mesh.materials[i], shader)
Expand Down