Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBastien Montagne <montagne29@wanadoo.fr>2019-01-04 16:37:10 +0300
committerBastien Montagne <montagne29@wanadoo.fr>2019-01-04 16:38:40 +0300
commitedc34891367c03450f2bc36eac90b7e1b60d917c (patch)
tree04d7c617322b98e026cb51a8237bfef513bd3133 /io_scene_obj/import_obj.py
parent49437df00db24499f4bd9d9a814b55661e9da276 (diff)
OBJ Import: Fix finalize material code not being performed on last material.
Issue noticed by Philipp Oeser (@lichtwerk), thanks!
Diffstat (limited to 'io_scene_obj/import_obj.py')
-rw-r--r--io_scene_obj/import_obj.py112
1 files changed, 61 insertions, 51 deletions
diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py
index 3185e5c2..a8e08872 100644
--- a/io_scene_obj/import_obj.py
+++ b/io_scene_obj/import_obj.py
@@ -177,6 +177,61 @@ def create_materials(filepath, relpath,
else:
raise Exception("invalid type %r" % type)
+ def finalize_material(context_material, context_material_vars, spec_colors, emit_colors,
+ do_highlight, do_reflection, do_transparency, do_glass):
+ # Finalize previous mat, if any.
+ if context_material:
+ if "specular" in context_material_vars:
+ # XXX This is highly approximated, not sure whether we can do better...
+ # TODO: Find a way to guesstimate best value from diffuse color...
+ # IDEA: Use standard deviation of both spec and diff colors (i.e. how far away they are
+ # from some grey), and apply the the proportion between those two as tint factor?
+ spec = sum(spec_colors) / 3.0
+ # ~ spec_var = math.sqrt(sum((c - spec) ** 2 for c in spec_color) / 3.0)
+ # ~ diff = sum(context_mat_wrap.base_color) / 3.0
+ # ~ diff_var = math.sqrt(sum((c - diff) ** 2 for c in context_mat_wrap.base_color) / 3.0)
+ # ~ tint = min(1.0, spec_var / diff_var)
+ context_mat_wrap.specular = spec
+ context_mat_wrap.specular_tint = 0.0
+ if "roughness" not in context_material_vars:
+ context_mat_wrap.roughness = 0.0
+
+
+ emit_value = sum(emit_colors) / 3.0
+ if emit_value > 1e-6:
+ print("WARNING, emit value unsupported by Principled BSDF shader, skipped.")
+ # We have to adapt it to diffuse color too...
+ emit_value /= sum(context_material.diffuse_color) / 3.0
+ # ~ context_material.emit = emit_value
+
+ # FIXME, how else to use this?
+ if do_highlight:
+ if "specular" not in context_material_vars:
+ context_mat_wrap.specular = 1.0
+ if "roughness" not in context_material_vars:
+ context_mat_wrap.roughness = 0.0
+ else:
+ if "specular" not in context_material_vars:
+ context_mat_wrap.specular = 0.0
+ if "roughness" not in context_material_vars:
+ context_mat_wrap.roughness = 1.0
+
+ if do_reflection:
+ if "metallic" not in context_material_vars:
+ context_mat_wrap.metallic = 1.0
+
+ if do_transparency:
+ if "ior" not in context_material_vars:
+ context_mat_wrap.ior = 1.0
+ if "transmission" not in context_material_vars:
+ context_mat_wrap.transmission = 1.0
+ # EEVEE only
+ context_material.blend_method = 'BLEND'
+
+ if do_glass:
+ if "ior" not in context_material_vars:
+ context_mat_wrap.ior = 1.5
+
# Try to find a MTL with the same name as the OBJ if no MTLs are specified.
temp_mtl = os.path.splitext((os.path.basename(filepath)))[0] + ".mtl"
if os.path.exists(os.path.join(DIR, temp_mtl)):
@@ -220,57 +275,8 @@ def create_materials(filepath, relpath,
if line_id == b'newmtl':
# Finalize previous mat, if any.
- if context_material:
- if "specular" in context_material_vars:
- # XXX This is highly approximated, not sure whether we can do better...
- # TODO: Find a way to guesstimate best value from diffuse color...
- # IDEA: Use standard deviation of both spec and diff colors (i.e. how far away they are
- # from some grey), and apply the the proportion between those two as tint factor?
- spec = sum(spec_colors) / 3.0
- # ~ spec_var = math.sqrt(sum((c - spec) ** 2 for c in spec_color) / 3.0)
- # ~ diff = sum(context_mat_wrap.base_color) / 3.0
- # ~ diff_var = math.sqrt(sum((c - diff) ** 2 for c in context_mat_wrap.base_color) / 3.0)
- # ~ tint = min(1.0, spec_var / diff_var)
- context_mat_wrap.specular = spec
- context_mat_wrap.specular_tint = 0.0
- if "roughness" not in context_material_vars:
- context_mat_wrap.roughness = 0.0
-
-
- emit_value = sum(emit_colors) / 3.0
- if emit_value > 1e-6:
- print("WARNING, emit value unsupported by Principled BSDF shader, skipped.")
- # We have to adapt it to diffuse color too...
- emit_value /= sum(context_material.diffuse_color) / 3.0
- # ~ context_material.emit = emit_value
-
- # FIXME, how else to use this?
- if do_highlight:
- if "specular" not in context_material_vars:
- context_mat_wrap.specular = 1.0
- if "roughness" not in context_material_vars:
- context_mat_wrap.roughness = 0.0
- else:
- if "specular" not in context_material_vars:
- context_mat_wrap.specular = 0.0
- if "roughness" not in context_material_vars:
- context_mat_wrap.roughness = 1.0
-
- if do_reflection:
- if "metallic" not in context_material_vars:
- context_mat_wrap.metallic = 1.0
-
- if do_transparency:
- if "ior" not in context_material_vars:
- context_mat_wrap.ior = 1.0
- if "transmission" not in context_material_vars:
- context_mat_wrap.transmission = 1.0
- # EEVEE only
- context_material.blend_method = 'BLEND'
-
- if do_glass:
- if "ior" not in context_material_vars:
- context_mat_wrap.ior = 1.5
+ finalize_material(context_material, context_material_vars, spec_colors, emit_colors,
+ do_highlight, do_reflection, do_transparency, do_glass)
context_material_name = line_value(line_split)
context_material = unique_materials.get(context_material_name)
@@ -415,6 +421,10 @@ def create_materials(filepath, relpath,
context_material_name, img_data, line, 'refl')
else:
print("WARNING: %r:%r (ignored)" % (filepath, line))
+
+ # Finalize last mat, if any.
+ finalize_material(context_material, context_material_vars, spec_colors, emit_colors,
+ do_highlight, do_reflection, do_transparency, do_glass)
mtl.close()