[Bf-extensions-cvs] [7194d22] master: Fix T45403: Error on OBJ export.

Bastien Montagne noreply at git.blender.org
Sat Jul 11 16:51:19 CEST 2015


Commit: 7194d2205ecb9920449cc5fac5b0f9418be5c173
Author: Bastien Montagne
Date:   Sat Jul 11 00:25:55 2015 +0200
Branches: master
https://developer.blender.org/rBA7194d2205ecb9920449cc5fac5b0f9418be5c173

Fix T45403: Error on OBJ export.

Own mistake in recent corrections in regarding material's ambient handling.

Also, took the oportunity to fix a bit how shadings modes are handled (previously
if read early, they could be overwritten by other later settings...).

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

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

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

diff --git a/io_scene_obj/__init__.py b/io_scene_obj/__init__.py
index e6452eb..b356da7 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": (2, 1, 2),
+    "version": (2, 1, 3),
     "blender": (2, 74, 0),
     "location": "File > Import-Export",
     "description": "Import-Export OBJ, Import OBJ mesh, UV's, "
diff --git a/io_scene_obj/export_obj.py b/io_scene_obj/export_obj.py
index 00733dd..d47555c 100644
--- a/io_scene_obj/export_obj.py
+++ b/io_scene_obj/export_obj.py
@@ -87,7 +87,7 @@ def write_mtl(scene, filepath, path_mode, copy_set, mtl_dict):
             if use_mirror:
                 fw('Ka %.6f %.6f %.6f\n' % (mat.raytrace_mirror.reflect_factor * mat.mirror_color)[:])
             else:
-                fw('Ka %.6f %.6f %.6f\n' % mat.ambient[:])  # Do not use world color!
+                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
             if hasattr(mat, "raytrace_transparency") and hasattr(mat.raytrace_transparency, "ior"):
diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py
index b3f8154..77c2cd4 100644
--- a/io_scene_obj/import_obj.py
+++ b/io_scene_obj/import_obj.py
@@ -181,6 +181,14 @@ def create_materials(filepath, relpath,
         if not os.path.exists(mtlpath):
             print("\tMaterial not found MTL: %r" % mtlpath)
         else:
+            do_ambient = True
+            do_highlight = False
+            do_reflection = False
+            do_transparency = False
+            do_glass = False
+            do_fresnel = False
+            do_raytrace = False
+
             # print('\t\tloading mtl: %e' % mtlpath)
             context_material = None
             mtl = open(mtlpath, 'rb')
@@ -193,15 +201,62 @@ def create_materials(filepath, relpath,
                 line_id = line_split[0].lower()
 
                 if line_id == b'newmtl':
+                    # Finalize preview mat, if any.
+                    if context_material:
+                        if not do_ambient:
+                            context_material.ambient = 0.0
+
+                        if do_highlight:
+                            # FIXME, how else to use this?
+                            context_material.specular_intensity = 1.0
+
+                        if do_reflection:
+                            context_material.raytrace_mirror.use = True
+                            context_material.raytrace_mirror.reflect_factor = 1.0
+
+                        if do_transparency:
+                            context_material.use_transparency = True
+                            context_material.transparency_method = 'RAYTRACE' if do_raytrace else 'Z_TRANSPARENCY'
+                            if "alpha" not in context_material_vars:
+                                context_material.alpha = 0.0
+
+                        if do_glass:
+                            if "ior" not in context_material_vars:
+                                context_material.raytrace_transparency.ior = 1.5
+
+                        if do_fresnel:
+                            context_material.raytrace_mirror.fresnel = 1.0  # could be any value for 'ON'
+
+                        """
+                        if do_raytrace:
+                            context_material.use_raytrace = True
+                        else:
+                            context_material.use_raytrace = False
+                        """
+                        # XXX, this is not following the OBJ spec, but this was
+                        # written when raytracing wasnt default, annoying to disable for blender users.
+                        context_material.use_raytrace = True
+
                     context_material_name = line_value(line_split)
                     context_material = unique_materials.get(context_material_name)
                     context_material_vars.clear()
 
+                    do_ambient = True
+                    do_highlight = False
+                    do_reflection = False
+                    do_transparency = False
+                    do_glass = False
+                    do_fresnel = False
+                    do_raytrace = False
+
+
                 elif context_material:
                     # we need to make a material to assign properties to it.
                     if line_id == b'ka':
                         context_material.mirror_color = (
                             float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3]))
+                        # This is highly approximated, but let's try to stick as close from exporter as possible... :/
+                        context_material.ambient = sum(context_material.mirror_color) / 3
                     elif line_id == b'kd':
                         context_material.diffuse_color = (
                             float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3]))
@@ -215,12 +270,12 @@ def create_materials(filepath, relpath,
                     elif line_id == b'ni':  # Refraction index (between 1 and 3).
                         context_material.raytrace_transparency.ior = max(1, min(float_func(line_split[1]), 3))
                         context_material_vars.add("ior")
-                    elif line_id == b'd':  # dissolve (trancparency)
+                    elif line_id == b'd':  # dissolve (transparency)
                         context_material.alpha = float_func(line_split[1])
                         context_material.use_transparency = True
                         context_material.transparency_method = 'Z_TRANSPARENCY'
                         context_material_vars.add("alpha")
-                    elif line_id == b'tr':  # trancelucency
+                    elif line_id == b'tr':  # translucency
                         context_material.translucency = float_func(line_split[1])
                     elif line_id == b'tf':
                         # rgb, filter color, blender has no support for this.
@@ -228,14 +283,6 @@ def create_materials(filepath, relpath,
                     elif line_id == b'illum':
                         illum = int(line_split[1])
 
-                        do_ambient = True
-                        do_highlight = False
-                        do_reflection = False
-                        do_transparency = False
-                        do_glass = False
-                        do_fresnel = False
-                        do_raytrace = False
-
                         # inline comments are from the spec, v4.2
                         if illum == 0:
                             # Color on and Ambient off
@@ -287,45 +334,9 @@ def create_materials(filepath, relpath,
                         elif illum == 10:
                             # Casts shadows onto invisible surfaces
 
-                            # blender cant do this
+                            # blender can't do this
                             pass
 
-                        if do_ambient:
-                            context_material.ambient = 1.0
-                        else:
-                            context_material.ambient = 0.0
-
-                        if do_highlight:
-                            # FIXME, how else to use this?
-                            context_material.specular_intensity = 1.0
-
-                        if do_reflection:
-                            context_material.raytrace_mirror.use = True
-                            context_material.raytrace_mirror.reflect_factor = 1.0
-
-                        if do_transparency:
-                            context_material.use_transparency = True
-                            context_material.transparency_method = 'RAYTRACE' if do_raytrace else 'Z_TRANSPARENCY'
-                            if "alpha" not in context_material_vars:
-                                context_material.alpha = 0.0
-
-                        if do_glass:
-                            if "ior" not in context_material_vars:
-                                context_material.raytrace_transparency.ior = 1.5
-
-                        if do_fresnel:
-                            context_material.raytrace_mirror.fresnel = 1.0  # could be any value for 'ON'
-
-                        """
-                        if do_raytrace:
-                            context_material.use_raytrace = True
-                        else:
-                            context_material.use_raytrace = False
-                        """
-                        # XXX, this is not following the OBJ spec, but this was
-                        # written when raytracing wasnt default, annoying to disable for blender users.
-                        context_material.use_raytrace = True
-
                     elif line_id == b'map_ka':
                         img_filepath = line_value(line.split())
                         if img_filepath:



More information about the Bf-extensions-cvs mailing list