[Bf-extensions-cvs] [94c9c4e] master: Fix T45766: Exported OBJ/MTL files do not include emissive lighting parameters.

Bastien Montagne noreply at git.blender.org
Thu Aug 13 12:09:35 CEST 2015


Commit: 94c9c4ee3370d1feb42fc978a1f0d2db07cb9431
Author: Bastien Montagne
Date:   Thu Aug 13 12:04:04 2015 +0200
Branches: master
https://developer.blender.org/rBA94c9c4ee3370d1feb42fc978a1f0d2db07cb9431

Fix T45766: Exported OBJ/MTL files do not include emissive lighting parameters.

ke/map_ke are not official .mtl specs, but seem to be rather common extension.
map_ke was added three years ago to exporter only, this commit finishes the work
by adding ke/map_ke to importer, and ke to exporter.

NOTES:
* mtl ke is a color, while in Blender emit is only a factor, using diffuse color.
  this implies there is some loss of data during import here.
* I do not have the slightest idea about the range for ke values, so for now assuming
  a direct matching of Blender's emit value.

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

M	io_scene_obj/export_obj.py
M	io_scene_obj/import_obj.py

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

diff --git a/io_scene_obj/export_obj.py b/io_scene_obj/export_obj.py
index 7399c2e..e458f2d 100644
--- a/io_scene_obj/export_obj.py
+++ b/io_scene_obj/export_obj.py
@@ -90,6 +90,9 @@ def write_mtl(scene, filepath, path_mode, copy_set, mtl_dict):
                     fw('Ka %.6f %.6f %.6f\n' % (mat.ambient, mat.ambient, mat.ambient))  # Do not use world color!
                 fw('Kd %.6f %.6f %.6f\n' % (mat.diffuse_intensity * mat.diffuse_color)[:])  # Diffuse
                 fw('Ks %.6f %.6f %.6f\n' % (mat.specular_intensity * mat.specular_color)[:])  # Specular
+                # Emission, not in original MTL standard but seems pretty common, see T45766.
+                # XXX Blender has no color emission, it's using diffuse color instead...
+                fw('Ke %.6f %.6f %.6f\n' % (mat.emit * mat.diffuse_color)[:])
                 if hasattr(mat, "raytrace_transparency") and hasattr(mat.raytrace_transparency, "ior"):
                     fw('Ni %.6f\n' % mat.raytrace_transparency.ior)  # Refraction index
                 else:
diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py
index a988860..a0db075 100644
--- a/io_scene_obj/import_obj.py
+++ b/io_scene_obj/import_obj.py
@@ -120,6 +120,14 @@ def create_materials(filepath, relpath,
             mtex.texture_coords = 'UV'
             mtex.use_map_color_spec = True
 
+        elif type == 'Ke':
+            mtex = blender_material.texture_slots.add()
+            mtex.use_map_color_diffuse = False
+
+            mtex.texture = texture
+            mtex.texture_coords = 'UV'
+            mtex.use_map_emit = True
+
         elif type == 'Bump':
             mtex = blender_material.texture_slots.add()
             mtex.use_map_color_diffuse = False
@@ -190,6 +198,7 @@ def create_materials(filepath, relpath,
             do_glass = False
             do_fresnel = False
             do_raytrace = False
+            emit_colors = [0.0, 0.0, 0.0]
 
             # print('\t\tloading mtl: %e' % mtlpath)
             context_material = None
@@ -203,8 +212,14 @@ def create_materials(filepath, relpath,
                 line_id = line_split[0].lower()
 
                 if line_id == b'newmtl':
-                    # Finalize preview mat, if any.
+                    # Finalize previous mat, if any.
                     if context_material:
+                        emit_value = sum(emit_colors) / 3.0
+                        if emit_value > 1e-6:
+                            # We have to adapt it to diffuse color too...
+                            emit_value /= sum(context_material.diffuse_color) / 3.0
+                        context_material.emit = emit_value
+
                         if not do_ambient:
                             context_material.ambient = 0.0
 
@@ -243,6 +258,7 @@ def create_materials(filepath, relpath,
                     context_material = unique_materials.get(context_material_name)
                     context_material_vars.clear()
 
+                    emit_colors[:] = [0.0, 0.0, 0.0]
                     do_ambient = True
                     do_highlight = False
                     do_reflection = False
@@ -267,6 +283,10 @@ def create_materials(filepath, relpath,
                         context_material.specular_color = (
                             float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3]))
                         context_material.specular_intensity = 1.0
+                    elif line_id == b'ke':
+                        # We cannot set context_material.emit right now, we need final diffuse color as well for this.
+                        emit_colors[:] = [
+                            float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3])]
                     elif line_id == b'ns':
                         context_material.specular_hardness = int((float_func(line_split[1]) * 0.51) + 1)
                     elif line_id == b'ni':  # Refraction index (between 1 and 3).
@@ -351,6 +371,10 @@ def create_materials(filepath, relpath,
                         img_filepath = line_value(line.split())
                         if img_filepath:
                             load_material_image(context_material, context_material_name, img_filepath, 'Kd')
+                    elif line_id == b'map_ke':
+                        img_filepath = line_value(line.split())
+                        if img_filepath:
+                            load_material_image(context_material, context_material_name, img_filepath, 'Ke')
                     elif line_id in {b'map_bump', b'bump'}:  # 'bump' is incorrect but some files use it.
                         img_filepath = line_value(line.split())
                         if img_filepath:



More information about the Bf-extensions-cvs mailing list