[Bf-extensions-cvs] [0ed2792d] master: OBJ import: Add basic support for Cycles shaders.
Bastien Montagne
noreply at git.blender.org
Sat May 27 13:08:40 CEST 2017
Commit: 0ed2792d30d726cca252fac298741ee6247f3f03
Author: Bastien Montagne
Date: Sat May 27 13:05:59 2017 +0200
Branches: master
https://developer.blender.org/rBA0ed2792d30d726cca252fac298741ee6247f3f03
OBJ import: Add basic support for Cycles shaders.
This code uses same wrapper as FBX importer to generate a basic nodal
shader for Cycles material.
Note that not all MTL features are supported, some might be possible to
add later though.
===================================================================
M io_scene_obj/__init__.py
M io_scene_obj/import_obj.py
M modules/cycles_shader_compat.py
===================================================================
diff --git a/io_scene_obj/__init__.py b/io_scene_obj/__init__.py
index 71587533..6d1312ec 100644
--- a/io_scene_obj/__init__.py
+++ b/io_scene_obj/__init__.py
@@ -139,6 +139,7 @@ class ImportOBJ(bpy.types.Operator, ImportHelper, IOOBJOrientationHelper):
from_up=self.axis_up,
).to_4x4()
keywords["global_matrix"] = global_matrix
+ keywords["use_cycles"] = (context.scene.render.engine == 'CYCLES')
if bpy.data.is_saved and context.user_preferences.filepaths.use_relative_paths:
import os
diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py
index 24a3b04f..36b0106d 100644
--- a/io_scene_obj/import_obj.py
+++ b/io_scene_obj/import_obj.py
@@ -88,7 +88,7 @@ def obj_image_load(context_imagepath_map, line, DIR, recursive, relpath):
def create_materials(filepath, relpath,
material_libs, unique_materials, unique_material_images,
- use_image_search, float_func):
+ use_image_search, use_cycles, float_func):
"""
Create all the used materials in this obj,
assign colors and images to the materials from all referenced material libs
@@ -99,7 +99,9 @@ def create_materials(filepath, relpath,
# Don't load the same image multiple times
context_imagepath_map = {}
- def load_material_image(blender_material, context_material_name, img_data, line, type):
+ cycles_material_wrap_map = {}
+
+ def load_material_image(blender_material, mat_wrap, use_cycles, context_material_name, img_data, line, type):
"""
Set textures defined in .mtl file.
"""
@@ -113,16 +115,22 @@ def create_materials(filepath, relpath,
curr_token[:] = []
curr_token.append(token)
- texture = bpy.data.textures.new(name=type, type='IMAGE')
-
# Absolute path - c:\.. etc would work here
image = obj_image_load(context_imagepath_map, line, DIR, use_image_search, relpath)
+ texture = bpy.data.textures.new(name=type, type='IMAGE')
if image is not None:
texture.image = image
+ map_offset = map_options.get(b'-o')
+ map_scale = map_options.get(b'-s')
+
# Adds textures for materials (rendering)
if type == 'Kd':
+ if use_cycles:
+ mat_wrap.diffuse_image_set(image)
+ mat_wrap.diffuse_mapping_set(coords='UV', translation=map_offset, scale=map_scale)
+
mtex = blender_material.texture_slots.add()
mtex.texture = texture
mtex.texture_coords = 'UV'
@@ -133,45 +141,62 @@ def create_materials(filepath, relpath,
unique_material_images[context_material_name] = image # set the texface image
elif type == 'Ka':
+ if use_cycles:
+ # XXX Not supported?
+ print("WARNING, currently unsupported ambient texture, skipped.")
+
mtex = blender_material.texture_slots.add()
mtex.use_map_color_diffuse = False
-
mtex.texture = texture
mtex.texture_coords = 'UV'
mtex.use_map_ambient = True
elif type == 'Ks':
+ if use_cycles:
+ mat_wrap.specular_image_set(image)
+ mat_wrap.specular_mapping_set(coords='UV', translation=map_offset, scale=map_scale)
+
mtex = blender_material.texture_slots.add()
mtex.use_map_color_diffuse = False
-
mtex.texture = texture
mtex.texture_coords = 'UV'
mtex.use_map_color_spec = True
elif type == 'Ke':
+ if use_cycles:
+ # XXX Not supported?
+ print("WARNING, currently unsupported emit texture, skipped.")
+
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':
+ bump_mult = map_options.get(b'-bm')
+
+ if use_cycles:
+ mat_wrap.normal_image_set(image)
+ mat_wrap.normal_mapping_set(coords='UV', translation=map_offset, scale=map_scale)
+ if bump_mult:
+ mat_wrap.normal_factor_set(bump_mult[0])
+
mtex = blender_material.texture_slots.add()
mtex.use_map_color_diffuse = False
-
mtex.texture = texture
mtex.texture_coords = 'UV'
mtex.use_map_normal = True
-
- bump_mult = map_options.get(b'-bm')
if bump_mult:
mtex.normal_factor = bump_mult[0]
elif type == 'D':
+ if use_cycles:
+ mat_wrap.alpha_image_set(image)
+ mat_wrap.alpha_mapping_set(coords='UV', translation=map_offset, scale=map_scale)
+
mtex = blender_material.texture_slots.add()
mtex.use_map_color_diffuse = False
-
mtex.texture = texture
mtex.texture_coords = 'UV'
mtex.use_map_alpha = True
@@ -179,34 +204,38 @@ def create_materials(filepath, relpath,
blender_material.transparency_method = 'Z_TRANSPARENCY'
if "alpha" not in context_material_vars:
blender_material.alpha = 0.0
- # Todo, unset deffuse material alpha if it has an alpha channel
+ # Todo, unset diffuse material alpha if it has an alpha channel
elif type == 'disp':
+ if use_cycles:
+ mat_wrap.bump_image_set(image)
+ mat_wrap.bump_mapping_set(coords='UV', translation=map_offset, scale=map_scale)
+
mtex = blender_material.texture_slots.add()
mtex.use_map_color_diffuse = False
-
mtex.texture = texture
mtex.texture_coords = 'UV'
mtex.use_map_displacement = True
elif type == 'refl':
+ map_type = map_options.get(b'-type')
+ if map_type and map_type != [b'sphere']:
+ print("WARNING, unsupported reflection type '%s', defaulting to 'sphere'"
+ "" % ' '.join(i.decode() for i in map_type))
+
+ if use_cycles:
+ mat_wrap.diffuse_image_set(image, projection='SPHERE')
+ mat_wrap.diffuse_mapping_set(coords='Reflection', translation=map_offset, scale=map_scale)
+
mtex = blender_material.texture_slots.add()
mtex.use_map_color_diffuse = False
-
mtex.texture = texture
mtex.texture_coords = 'REFLECTION'
mtex.use_map_color_diffuse = True
-
- map_type = map_options.get(b'-type')
- if map_type and map_type != [b'sphere']:
- print("WARNING, unsupported reflection type '%s', defaulting to 'sphere'"
- "" % ' '.join(i.decode() for i in map_type))
mtex.mapping = 'SPHERE'
else:
raise Exception("invalid type %r" % type)
- map_offset = map_options.get(b'-o')
- map_scale = map_options.get(b'-s')
if map_offset:
mtex.offset.x = float(map_offset[0])
if len(map_offset) >= 2:
@@ -230,8 +259,13 @@ def create_materials(filepath, relpath,
# Create new materials
for name in unique_materials: # .keys()
if name is not None:
- 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"))
unique_material_images[name] = None # assign None to all material images to start with, add to later.
+ if use_cycles:
+ from modules import cycles_shader_compat
+ ma_wrap = cycles_shader_compat.CyclesShaderWrapper(ma)
+ cycles_material_wrap_map[ma] = ma_wrap
+
# XXX Why was this needed? Cannot find any good reason, and adds stupid empty matslot in case we do not separate
# mesh (see T44947).
@@ -255,6 +289,7 @@ def create_materials(filepath, relpath,
# print('\t\tloading mtl: %e' % mtlpath)
context_material = None
+ context_mat_wrap = None
mtl = open(mtlpath, 'rb')
for line in mtl: # .readlines():
line = line.strip()
@@ -269,6 +304,8 @@ def create_materials(filepath, relpath,
if context_material:
emit_value = sum(emit_colors) / 3.0
if emit_value > 1e-6:
+ if use_cycles:
+ print("WARNING, currently unsupported emit value, skipped.")
# We have to adapt it to diffuse color too...
emit_value /= sum(context_material.diffuse_color) / 3.0
context_material.emit = emit_value
@@ -277,10 +314,17 @@ def create_materials(filepath, relpath,
context_material.ambient = 0.0
if do_highlight:
+ if use_cycles:
+ context_mat_wrap.hardness_value_set = 1.0
# FIXME, how else to use this?
context_material.specular_intensity = 1.0
+ else:
+ if use_cycles:
+ context_mat_wrap.hardness_value_set = 0.0
if do_reflection:
+ if use_cycles:
+ context_mat_wrap.reflect_factor_set = 1.0
context_material.raytrace_mirror.use = True
context_material.raytrace_mirror.reflect_factor = 1.0
@@ -288,13 +332,19 @@ def create_materials(filepath, relpath,
context_material.use_transparency = True
context_material.transparency_method = 'RAYTRACE' if do_raytrace else 'Z_TRANSPARENCY'
if "alph
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-extensions-cvs
mailing list