Skip to content
Snippets Groups Projects
Commit c1784de1 authored by Bastien Montagne's avatar Bastien Montagne
Browse files

OBJ: Cleanup: remove 'use_cycles' option.

We are always using nodes now, whether we want it or not...
Also fixed various access to removed 'fixed shader' old 2.7x materials
in import, and some other minor bugs.
parent 0a588aef
No related branches found
No related tags found
No related merge requests found
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
bl_info = { bl_info = {
"name": "Wavefront OBJ format", "name": "Wavefront OBJ format",
"author": "Campbell Barton, Bastien Montagne", "author": "Campbell Barton, Bastien Montagne",
"version": (3, 3, 6), "version": (3, 3, 7),
"blender": (2, 80, 0), "blender": (2, 80, 0),
"location": "File > Import-Export", "location": "File > Import-Export",
"description": "Import-Export OBJ, Import OBJ mesh, UV's, materials and textures", "description": "Import-Export OBJ, Import OBJ mesh, UV's, materials and textures",
...@@ -137,7 +137,6 @@ class ImportOBJ(bpy.types.Operator, ImportHelper): ...@@ -137,7 +137,6 @@ class ImportOBJ(bpy.types.Operator, ImportHelper):
from_up=self.axis_up, from_up=self.axis_up,
).to_4x4() ).to_4x4()
keywords["global_matrix"] = global_matrix keywords["global_matrix"] = global_matrix
keywords["use_cycles"] = True # (context.scene.view_render.engine == 'CYCLES')
if bpy.data.is_saved and context.user_preferences.filepaths.use_relative_paths: if bpy.data.is_saved and context.user_preferences.filepaths.use_relative_paths:
import os import os
......
...@@ -88,7 +88,7 @@ def obj_image_load(context_imagepath_map, line, DIR, recursive, relpath): ...@@ -88,7 +88,7 @@ def obj_image_load(context_imagepath_map, line, DIR, recursive, relpath):
def create_materials(filepath, relpath, def create_materials(filepath, relpath,
material_libs, unique_materials, material_libs, unique_materials,
use_image_search, use_cycles, float_func): use_image_search, float_func):
""" """
Create all the used materials in this obj, Create all the used materials in this obj,
assign colors and images to the materials from all referenced material libs assign colors and images to the materials from all referenced material libs
...@@ -99,9 +99,9 @@ def create_materials(filepath, relpath, ...@@ -99,9 +99,9 @@ def create_materials(filepath, relpath,
# Don't load the same image multiple times # Don't load the same image multiple times
context_imagepath_map = {} context_imagepath_map = {}
cycles_material_wrap_map = {} nodal_material_wrap_map = {}
def load_material_image(blender_material, mat_wrap, use_cycles, context_material_name, img_data, line, type): def load_material_image(blender_material, mat_wrap, context_material_name, img_data, line, type):
""" """
Set textures defined in .mtl file. Set textures defined in .mtl file.
""" """
...@@ -129,44 +129,37 @@ def create_materials(filepath, relpath, ...@@ -129,44 +129,37 @@ def create_materials(filepath, relpath,
# Adds textures for materials (rendering) # Adds textures for materials (rendering)
if type == 'Kd': if type == 'Kd':
if use_cycles: mat_wrap.diffuse_image_set(image)
mat_wrap.diffuse_image_set(image) mat_wrap.diffuse_mapping_set(coords='UV', translation=map_offset, scale=map_scale)
mat_wrap.diffuse_mapping_set(coords='UV', translation=map_offset, scale=map_scale)
elif type == 'Ka': elif type == 'Ka':
if use_cycles: # XXX Not supported?
# XXX Not supported? print("WARNING, currently unsupported ambient texture, skipped.")
print("WARNING, currently unsupported ambient texture, skipped.")
elif type == 'Ks': elif type == 'Ks':
if use_cycles: mat_wrap.specular_image_set(image)
mat_wrap.specular_image_set(image) mat_wrap.specular_mapping_set(coords='UV', translation=map_offset, scale=map_scale)
mat_wrap.specular_mapping_set(coords='UV', translation=map_offset, scale=map_scale)
elif type == 'Ke': elif type == 'Ke':
if use_cycles: # XXX Not supported?
# XXX Not supported? print("WARNING, currently unsupported emit texture, skipped.")
print("WARNING, currently unsupported emit texture, skipped.")
elif type == 'Bump': elif type == 'Bump':
bump_mult = map_options.get(b'-bm') bump_mult = map_options.get(b'-bm')
bump_mult = float(bump_mult[0]) if (bump_mult is not None and len(bump_mult) > 1) else 1.0 bump_mult = float(bump_mult[0]) if (bump_mult is not None and len(bump_mult) > 1) else 1.0
if use_cycles: mat_wrap.normal_image_set(image)
mat_wrap.normal_image_set(image) mat_wrap.normal_mapping_set(coords='UV', translation=map_offset, scale=map_scale)
mat_wrap.normal_mapping_set(coords='UV', translation=map_offset, scale=map_scale) if bump_mult:
if bump_mult: mat_wrap.normal_factor_set(bump_mult)
mat_wrap.normal_factor_set(bump_mult)
elif type == 'D': elif type == 'D':
if use_cycles: mat_wrap.alpha_image_set(image)
mat_wrap.alpha_image_set(image) mat_wrap.alpha_mapping_set(coords='UV', translation=map_offset, scale=map_scale)
mat_wrap.alpha_mapping_set(coords='UV', translation=map_offset, scale=map_scale)
elif type == 'disp': elif type == 'disp':
if use_cycles: mat_wrap.bump_image_set(image)
mat_wrap.bump_image_set(image) mat_wrap.bump_mapping_set(coords='UV', translation=map_offset, scale=map_scale)
mat_wrap.bump_mapping_set(coords='UV', translation=map_offset, scale=map_scale)
elif type == 'refl': elif type == 'refl':
map_type = map_options.get(b'-type') map_type = map_options.get(b'-type')
...@@ -174,9 +167,8 @@ def create_materials(filepath, relpath, ...@@ -174,9 +167,8 @@ def create_materials(filepath, relpath,
print("WARNING, unsupported reflection type '%s', defaulting to 'sphere'" print("WARNING, unsupported reflection type '%s', defaulting to 'sphere'"
"" % ' '.join(i.decode() for i in map_type)) "" % ' '.join(i.decode() for i in map_type))
if use_cycles: mat_wrap.diffuse_image_set(image, projection='SPHERE')
mat_wrap.diffuse_image_set(image, projection='SPHERE') mat_wrap.diffuse_mapping_set(coords='Reflection', translation=map_offset, scale=map_scale)
mat_wrap.diffuse_mapping_set(coords='Reflection', translation=map_offset, scale=map_scale)
else: else:
raise Exception("invalid type %r" % type) raise Exception("invalid type %r" % type)
...@@ -205,10 +197,9 @@ def create_materials(filepath, relpath, ...@@ -205,10 +197,9 @@ def create_materials(filepath, relpath,
for name in unique_materials: # .keys() for name in unique_materials: # .keys()
if name is not None: if name is not None:
ma = unique_materials[name] = bpy.data.materials.new(name.decode('utf-8', "replace")) ma = unique_materials[name] = bpy.data.materials.new(name.decode('utf-8', "replace"))
if use_cycles: from modules import cycles_shader_compat
from modules import cycles_shader_compat ma_wrap = cycles_shader_compat.CyclesShaderWrapper(ma)
ma_wrap = cycles_shader_compat.CyclesShaderWrapper(ma) nodal_material_wrap_map[ma] = ma_wrap
cycles_material_wrap_map[ma] = ma_wrap
for libname in sorted(material_libs): for libname in sorted(material_libs):
# print(libname) # print(libname)
...@@ -242,63 +233,44 @@ def create_materials(filepath, relpath, ...@@ -242,63 +233,44 @@ def create_materials(filepath, relpath,
if context_material: if context_material:
emit_value = sum(emit_colors) / 3.0 emit_value = sum(emit_colors) / 3.0
if emit_value > 1e-6: if emit_value > 1e-6:
if use_cycles: print("WARNING, currently unsupported emit value, skipped.")
print("WARNING, currently unsupported emit value, skipped.")
# We have to adapt it to diffuse color too... # We have to adapt it to diffuse color too...
emit_value /= sum(context_material.diffuse_color) / 3.0 emit_value /= sum(context_material.diffuse_color) / 3.0
context_material.emit = emit_value # ~ context_material.emit = emit_value
if not do_ambient: if not do_ambient:
context_material.ambient = 0.0 context_material.ambient = 0.0
if do_highlight: if do_highlight:
if use_cycles: context_mat_wrap.hardness_value_set(1.0)
context_mat_wrap.hardness_value_set(1.0)
# FIXME, how else to use this? # FIXME, how else to use this?
context_material.specular_intensity = 1.0 context_material.specular_intensity = 1.0
else: else:
if use_cycles: context_mat_wrap.hardness_value_set(0.0)
context_mat_wrap.hardness_value_set(0.0)
if do_reflection: if do_reflection:
if use_cycles: context_mat_wrap.reflect_factor_set(1.0)
context_mat_wrap.reflect_factor_set(1.0) context_material.metallic = 1.0
context_material.raytrace_mirror.use = True
context_material.raytrace_mirror.reflect_factor = 1.0
if do_transparency: 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: if "alpha" not in context_material_vars:
if use_cycles: context_mat_wrap.alpha_value_set(0.0)
context_mat_wrap.alpha_value_set(0.0) # EEVEE only
context_material.alpha = 0.0 context_material.blend_method = 'BLEND'
if do_glass: if do_glass:
if use_cycles: print("WARNING, currently unsupported glass material, skipped.")
print("WARNING, currently unsupported glass material, skipped.") # ~ if "ior" not in context_material_vars:
if "ior" not in context_material_vars: # ~ context_material.raytrace_transparency.ior = 1.5
context_material.raytrace_transparency.ior = 1.5
if do_fresnel: if do_fresnel:
if use_cycles: print("WARNING, currently unsupported fresnel option, skipped.")
print("WARNING, currently unsupported fresnel option, skipped.") # ~ context_material.raytrace_mirror.fresnel = 1.0 # could be any value for 'ON'
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_name = line_value(line_split)
context_material = unique_materials.get(context_material_name) context_material = unique_materials.get(context_material_name)
if use_cycles and context_material is not None: if context_material is not None:
context_mat_wrap = cycles_material_wrap_map[context_material] context_mat_wrap = nodal_material_wrap_map[context_material]
context_material_vars.clear() context_material_vars.clear()
emit_colors[:] = [0.0, 0.0, 0.0] emit_colors[:] = [0.0, 0.0, 0.0]
...@@ -315,33 +287,26 @@ def create_materials(filepath, relpath, ...@@ -315,33 +287,26 @@ def create_materials(filepath, relpath,
# we need to make a material to assign properties to it. # we need to make a material to assign properties to it.
if line_id == b'ka': if line_id == b'ka':
col = (float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3])) col = (float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3]))
if use_cycles: context_mat_wrap.reflect_color_set(col)
context_mat_wrap.reflect_color_set(col)
elif line_id == b'kd': elif line_id == b'kd':
col = (float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3])) col = (float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3]))
if use_cycles: context_mat_wrap.diffuse_color_set(col)
context_mat_wrap.diffuse_color_set(col)
elif line_id == b'ks': elif line_id == b'ks':
col = (float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3])) col = (float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3]))
if use_cycles: context_mat_wrap.specular_color_set(col)
context_mat_wrap.specular_color_set(col) context_mat_wrap.hardness_value_set(1.0)
context_mat_wrap.hardness_value_set(1.0)
elif line_id == b'ke': elif line_id == b'ke':
# We cannot set context_material.emit right now, we need final diffuse color as well for this. # We cannot set context_material.emit right now, we need final diffuse color as well for this.
emit_colors[:] = [ emit_colors[:] = [
float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3])] float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3])]
elif line_id == b'ns': elif line_id == b'ns':
if use_cycles: context_mat_wrap.hardness_value_set(((float_func(line_split[1]) + 3.0) / 50.0) - 0.65)
context_mat_wrap.hardness_value_set(((float_func(line_split[1]) + 3.0) / 50.0) - 0.65)
elif line_id == b'ni': # Refraction index (between 1 and 3). elif line_id == b'ni': # Refraction index (between 1 and 3).
if use_cycles: print("WARNING, currently unsupported glass material, skipped.")
print("WARNING, currently unsupported glass material, skipped.")
elif line_id == b'd': # dissolve (transparency) elif line_id == b'd': # dissolve (transparency)
if use_cycles: context_mat_wrap.alpha_value_set(float_func(line_split[1]))
context_mat_wrap.alpha_value_set(float_func(line_split[1]))
elif line_id == b'tr': # translucency elif line_id == b'tr': # translucency
if use_cycles: print("WARNING, currently unsupported translucency option, skipped.")
print("WARNING, currently unsupported translucency option, skipped.")
elif line_id == b'tf': elif line_id == b'tf':
# rgb, filter color, blender has no support for this. # rgb, filter color, blender has no support for this.
pass pass
...@@ -405,44 +370,44 @@ def create_materials(filepath, relpath, ...@@ -405,44 +370,44 @@ def create_materials(filepath, relpath,
elif line_id == b'map_ka': elif line_id == b'map_ka':
img_data = line.split()[1:] img_data = line.split()[1:]
if img_data: if img_data:
load_material_image(context_material, context_mat_wrap, use_cycles, load_material_image(context_material, context_mat_wrap,
context_material_name, img_data, line, 'Ka') context_material_name, img_data, line, 'Ka')
elif line_id == b'map_ks': elif line_id == b'map_ks':
img_data = line.split()[1:] img_data = line.split()[1:]
if img_data: if img_data:
load_material_image(context_material, context_mat_wrap, use_cycles, load_material_image(context_material, context_mat_wrap,
context_material_name, img_data, line, 'Ks') context_material_name, img_data, line, 'Ks')
elif line_id == b'map_kd': elif line_id == b'map_kd':
img_data = line.split()[1:] img_data = line.split()[1:]
if img_data: if img_data:
load_material_image(context_material, context_mat_wrap, use_cycles, load_material_image(context_material, context_mat_wrap,
context_material_name, img_data, line, 'Kd') context_material_name, img_data, line, 'Kd')
elif line_id == b'map_ke': elif line_id == b'map_ke':
img_data = line.split()[1:] img_data = line.split()[1:]
if img_data: if img_data:
load_material_image(context_material, context_mat_wrap, use_cycles, load_material_image(context_material, context_mat_wrap,
context_material_name, img_data, line, 'Ke') context_material_name, img_data, line, 'Ke')
elif line_id in {b'map_bump', b'bump'}: # 'bump' is incorrect but some files use it. elif line_id in {b'map_bump', b'bump'}: # 'bump' is incorrect but some files use it.
img_data = line.split()[1:] img_data = line.split()[1:]
if img_data: if img_data:
load_material_image(context_material, context_mat_wrap, use_cycles, load_material_image(context_material, context_mat_wrap,
context_material_name, img_data, line, 'Bump') context_material_name, img_data, line, 'Bump')
elif line_id in {b'map_d', b'map_tr'}: # Alpha map - Dissolve elif line_id in {b'map_d', b'map_tr'}: # Alpha map - Dissolve
img_data = line.split()[1:] img_data = line.split()[1:]
if img_data: if img_data:
load_material_image(context_material, context_mat_wrap, use_cycles, load_material_image(context_material, context_mat_wrap,
context_material_name, img_data, line, 'D') context_material_name, img_data, line, 'D')
elif line_id in {b'map_disp', b'disp'}: # displacementmap elif line_id in {b'map_disp', b'disp'}: # displacementmap
img_data = line.split()[1:] img_data = line.split()[1:]
if img_data: if img_data:
load_material_image(context_material, context_mat_wrap, use_cycles, load_material_image(context_material, context_mat_wrap,
context_material_name, img_data, line, 'disp') context_material_name, img_data, line, 'disp')
elif line_id in {b'map_refl', b'refl'}: # reflectionmap elif line_id in {b'map_refl', b'refl'}: # reflectionmap
img_data = line.split()[1:] img_data = line.split()[1:]
if img_data: if img_data:
load_material_image(context_material, context_mat_wrap, use_cycles, load_material_image(context_material, context_mat_wrap,
context_material_name, img_data, line, 'refl') context_material_name, img_data, line, 'refl')
else: else:
print("\t%r:%r (ignored)" % (filepath, line)) print("\t%r:%r (ignored)" % (filepath, line))
...@@ -754,7 +719,6 @@ def create_mesh(new_objects, ...@@ -754,7 +719,6 @@ def create_mesh(new_objects,
me.normals_split_custom_set(tuple(zip(*(iter(clnors),) * 3))) me.normals_split_custom_set(tuple(zip(*(iter(clnors),) * 3)))
me.use_auto_smooth = True me.use_auto_smooth = True
me.show_edge_sharp = True
ob = bpy.data.objects.new(me.name, me) ob = bpy.data.objects.new(me.name, me)
new_objects.append(ob) new_objects.append(ob)
...@@ -881,7 +845,6 @@ def load(context, ...@@ -881,7 +845,6 @@ def load(context,
use_split_groups=True, use_split_groups=True,
use_image_search=True, use_image_search=True,
use_groups_as_vgroups=False, use_groups_as_vgroups=False,
use_cycles=True,
relpath=None, relpath=None,
global_matrix=None global_matrix=None
): ):
...@@ -1171,7 +1134,7 @@ def load(context, ...@@ -1171,7 +1134,7 @@ def load(context,
progress.step("Done, loading materials and images...") progress.step("Done, loading materials and images...")
create_materials(filepath, relpath, material_libs, unique_materials, create_materials(filepath, relpath, material_libs, unique_materials,
use_image_search, use_cycles, float_func) use_image_search, float_func)
progress.step("Done, building geometries (verts:%i faces:%i materials: %i smoothgroups:%i) ..." % progress.step("Done, building geometries (verts:%i faces:%i materials: %i smoothgroups:%i) ..." %
(len(verts_loc), len(faces), len(unique_materials), len(unique_smooth_groups))) (len(verts_loc), len(faces), len(unique_materials), len(unique_smooth_groups)))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment