From 7194d2205ecb9920449cc5fac5b0f9418be5c173 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Sat, 11 Jul 2015 00:25:55 +0200 Subject: 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...). --- io_scene_obj/__init__.py | 2 +- io_scene_obj/export_obj.py | 2 +- io_scene_obj/import_obj.py | 105 +++++++++++++++++++++++++-------------------- 3 files changed, 60 insertions(+), 49 deletions(-) (limited to 'io_scene_obj') diff --git a/io_scene_obj/__init__.py b/io_scene_obj/__init__.py index e6452eb9..b356da75 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 00733dd1..d47555c1 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 b3f81541..77c2cd4a 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: -- cgit v1.2.3