[Bf-extensions-cvs] [4fd8ee12] master: Fix T71618: Can't import OBJs that have single-value colors in their MTL file.

Bastien Montagne noreply at git.blender.org
Mon Nov 18 10:45:14 CET 2019


Commit: 4fd8ee12dbaba9fb21dad8f3c2a8718fb69f27a0
Author: Bastien Montagne
Date:   Mon Nov 18 10:41:01 2019 +0100
Branches: master
https://developer.blender.org/rBA4fd8ee12dbaba9fb21dad8f3c2a8718fb69f27a0

Fix T71618: Can't import OBJs that have single-value colors in their MTL file.

MTL standard does consider g and b values as optional...

===================================================================

M	io_scene_obj/__init__.py
M	io_scene_obj/import_obj.py

===================================================================

diff --git a/io_scene_obj/__init__.py b/io_scene_obj/__init__.py
index 399a4d29..e121bb66 100644
--- a/io_scene_obj/__init__.py
+++ b/io_scene_obj/__init__.py
@@ -21,7 +21,7 @@
 bl_info = {
     "name": "Wavefront OBJ format",
     "author": "Campbell Barton, Bastien Montagne",
-    "version": (3, 7, 0),
+    "version": (3, 8, 0),
     "blender": (2, 81, 6),
     "location": "File > Import-Export",
     "description": "Import-Export OBJ, Import OBJ mesh, UV's, materials and textures",
diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py
index 4d640d44..0c2d6995 100644
--- a/io_scene_obj/import_obj.py
+++ b/io_scene_obj/import_obj.py
@@ -326,23 +326,30 @@ def create_materials(filepath, relpath,
 
 
                 elif context_material:
+                    def _get_colors(line_split):
+                        # OBJ 'allows' one or two components values, treat single component as greyscale, and two as blue = 0.0.
+                        ln = len(line_split)
+                        if ln == 2:
+                            return [float_func(line_split[1])] * 3
+                        elif ln == 3:
+                            return [float_func(line_split[1]), float_func(line_split[2]), 0.0]
+                        else:
+                            return [float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3])]
+
                     # we need to make a material to assign properties to it.
                     if line_id == b'ka':
-                        refl = (float_func(line_split[1]) + float_func(line_split[2]) + float_func(line_split[3])) / 3.0
+                        refl =  sum(_get_colors(line_split)) / 3.0
                         context_mat_wrap.metallic = refl
                         context_material_vars.add("metallic")
                     elif line_id == b'kd':
-                        col = (float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3]))
-                        context_mat_wrap.base_color = col
+                        context_mat_wrap.base_color = _get_colors(line_split)
                     elif line_id == b'ks':
-                        spec_colors[:] = [
-                            float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3])]
+                        spec_colors[:] = _get_colors(line_split)
                         context_material_vars.add("specular")
                     elif line_id == b'ke':
                         # We cannot set context_material.emit right now, we need final diffuse color as well for this.
                         # XXX Unsupported currently
-                        col = (float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3]))
-                        context_mat_wrap.emission_color = col
+                        context_mat_wrap.emission_color = _get_colors(line_split)
                     elif line_id == b'ns':
                         # XXX Totally empirical conversion, trying to adapt it
                         #     (from 0.0 - 900.0 OBJ specular exponent range to 1.0 - 0.0 Principled BSDF range)...



More information about the Bf-extensions-cvs mailing list