diff options
author | Julien Duroure <julien.duroure@gmail.com> | 2018-12-15 08:30:27 +0300 |
---|---|---|
committer | Julien Duroure <julien.duroure@gmail.com> | 2018-12-15 08:30:27 +0300 |
commit | 6d933c0e3ac8048e8a04f2b42bcd21a667818c2e (patch) | |
tree | 95799aa9467fc4bdfa69e7a5192411fd312e7e8a /io_scene_gltf2/blender/imp/gltf2_blender_primitive.py | |
parent | b55002b7c33154d2b66166aaf0c2a0b287ec9e42 (diff) |
glTF importer: fix material import / vertex color
Fixed a bug when a same material sometimes uses vertex color, sometimes doesn't
Diffstat (limited to 'io_scene_gltf2/blender/imp/gltf2_blender_primitive.py')
-rwxr-xr-x | io_scene_gltf2/blender/imp/gltf2_blender_primitive.py | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_primitive.py b/io_scene_gltf2/blender/imp/gltf2_blender_primitive.py index 52987383..93bab542 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_primitive.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_primitive.py @@ -57,13 +57,18 @@ class BlenderPrimitive(): # manage material of primitive if pyprimitive.material is not None: - # Create Blender material - # TODO, a same material can have difference COLOR_0 multiplicator - if gltf.data.materials[pyprimitive.material].blender_material is None: - vertex_color = None - if 'COLOR_0' in pyprimitive.attributes.keys(): - vertex_color = pyprimitive.attributes['COLOR_0'] - BlenderMaterial.create(gltf, pyprimitive.material, vertex_color) + vertex_color = None + if 'COLOR_0' in pyprimitive.attributes.keys(): + vertex_color = 'COLOR_0' + + # Create Blender material if needed + if vertex_color is None: + if None not in gltf.data.materials[pyprimitive.material].blender_material.keys(): + BlenderMaterial.create(gltf, pyprimitive.material, vertex_color) + else: + if vertex_color not in gltf.data.materials[pyprimitive.material].blender_material.keys(): + BlenderMaterial.create(gltf, pyprimitive.material, vertex_color) + return verts, edges, faces @@ -126,7 +131,7 @@ class BlenderPrimitive(): offset = offset + pyprimitive.vertices_length return offset - def set_UV_in_mat(gltf, pyprimitive, obj): + def set_UV_in_mat(gltf, pyprimitive, obj, vertex_color): """After nodetree creation, set UVMap in nodes.""" if pyprimitive.material is None: return @@ -137,29 +142,34 @@ class BlenderPrimitive(): and gltf.data.materials[pyprimitive.material].extensions[ 'KHR_materials_pbrSpecularGlossiness' ]['diffuse_type'] in [gltf.TEXTURE, gltf.TEXTURE_FACTOR]: - BlenderMaterial.set_uvmap(gltf, pyprimitive.material, pyprimitive, obj) + BlenderMaterial.set_uvmap(gltf, pyprimitive.material, pyprimitive, obj, vertex_color) else: if pyprimitive.material is not None \ and gltf.data.materials[pyprimitive.material].extensions[ 'KHR_materials_pbrSpecularGlossiness' ]['specgloss_type'] in [gltf.TEXTURE, gltf.TEXTURE_FACTOR]: - BlenderMaterial.set_uvmap(gltf, pyprimitive.material, pyprimitive, obj) + BlenderMaterial.set_uvmap(gltf, pyprimitive.material, pyprimitive, obj, vertex_color) else: if pyprimitive.material is not None \ and gltf.data.materials[pyprimitive.material].pbr_metallic_roughness.color_type in \ [gltf.TEXTURE, gltf.TEXTURE_FACTOR]: - BlenderMaterial.set_uvmap(gltf, pyprimitive.material, pyprimitive, obj) + BlenderMaterial.set_uvmap(gltf, pyprimitive.material, pyprimitive, obj, vertex_color) else: if pyprimitive.material is not None \ and gltf.data.materials[pyprimitive.material].pbr_metallic_roughness.metallic_type in \ [gltf.TEXTURE, gltf.TEXTURE_FACTOR]: - BlenderMaterial.set_uvmap(gltf, pyprimitive.material, pyprimitive, obj) + BlenderMaterial.set_uvmap(gltf, pyprimitive.material, pyprimitive, obj, vertex_color) def assign_material(gltf, pyprimitive, obj, bm, offset, cpt_index_mat): """Assign material to faces of primitives.""" if pyprimitive.material is not None: - obj.data.materials.append(bpy.data.materials[gltf.data.materials[pyprimitive.material].blender_material]) + + vertex_color = None + if 'COLOR_0' in pyprimitive.attributes.keys(): + vertex_color = 'COLOR_0' + + obj.data.materials.append(bpy.data.materials[gltf.data.materials[pyprimitive.material].blender_material[vertex_color]]) for vert in bm.verts: if vert.index in range(offset, offset + pyprimitive.vertices_length): for loop in vert.link_loops: |