[Bf-extensions-cvs] [ecdc7c0] master: OBJ IO: add (limited) support for options of textures in MTL files.

Bastien Montagne noreply at git.blender.org
Tue Oct 13 12:44:07 CEST 2015


Commit: ecdc7c03ec14e6ed70dea808b61049017f46ad97
Author: Bastien Montagne
Date:   Tue Oct 13 12:41:25 2015 +0200
Branches: master
https://developer.blender.org/rBAecdc7c03ec14e6ed70dea808b61049017f46ad97

OBJ IO: add (limited) support for options of textures in MTL files.

This commit adds IO support for:
* -o (offset texture)
* -s (scale texture)
* -bm (bump multiplier, used a normal factor)

Note that it also fixes T46459.

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

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 f8c179e..3b681fd 100644
--- a/io_scene_obj/__init__.py
+++ b/io_scene_obj/__init__.py
@@ -21,14 +21,12 @@
 bl_info = {
     "name": "Wavefront OBJ format",
     "author": "Campbell Barton, Bastien Montagne",
-    "version": (2, 2, 1),
-    "blender": (2, 74, 0),
+    "version": (2, 3, 0),
+    "blender": (2, 76, 0),
     "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",
     "warning": "",
-    "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
-                "Scripts/Import-Export/Wavefront_OBJ",
+    "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Import-Export/Wavefront_OBJ",
     "support": 'OFFICIAL',
     "category": "Import-Export"}
 
diff --git a/io_scene_obj/export_obj.py b/io_scene_obj/export_obj.py
index f0418a5..9c8039d 100644
--- a/io_scene_obj/export_obj.py
+++ b/io_scene_obj/export_obj.py
@@ -152,35 +152,43 @@ def write_mtl(scene, filepath, path_mode, copy_set, mtl_dict):
                             # texface overrides others
                             if (mtex.use_map_color_diffuse and (face_img is None) and
                                 (mtex.use_map_warp is False) and (mtex.texture_coords != 'REFLECTION')):
-                                image_map["map_Kd"] = image
+                                image_map["map_Kd"] = (mtex, image)
                             if mtex.use_map_ambient:
-                                image_map["map_Ka"] = image
+                                image_map["map_Ka"] = (mtex, image)
                             # this is the Spec intensity channel but Ks stands for specular Color
                             '''
                             if mtex.use_map_specular:
-                                image_map["map_Ks"] = image
+                                image_map["map_Ks"] = (mtex, image)
                             '''
                             if mtex.use_map_color_spec:  # specular color
-                                image_map["map_Ks"] = image
+                                image_map["map_Ks"] = (mtex, image)
                             if mtex.use_map_hardness:  # specular hardness/glossiness
-                                image_map["map_Ns"] = image
+                                image_map["map_Ns"] = (mtex, image)
                             if mtex.use_map_alpha:
-                                image_map["map_d"] = image
+                                image_map["map_d"] = (mtex, image)
                             if mtex.use_map_translucency:
-                                image_map["map_Tr"] = image
+                                image_map["map_Tr"] = (mtex, image)
                             if mtex.use_map_normal:
-                                image_map["map_Bump"] = image
+                                image_map["map_Bump"] = (mtex, image)
                             if mtex.use_map_displacement:
-                                image_map["disp"] = image
+                                image_map["disp"] = (mtex, image)
                             if mtex.use_map_color_diffuse and (mtex.texture_coords == 'REFLECTION'):
-                                image_map["refl"] = image
+                                image_map["refl"] = (mtex, image)
                             if mtex.use_map_emit:
-                                image_map["map_Ke"] = image
+                                image_map["map_Ke"] = (mtex, image)
 
-                for key, image in sorted(image_map.items()):
+                for key, (mtex, image) in sorted(image_map.items()):
                     filepath = bpy_extras.io_utils.path_reference(image.filepath, source_dir, dest_dir,
                                                                   path_mode, "", copy_set, image.library)
-                    fw('%s %s\n' % (key, repr(filepath)[1:-1]))
+                    options = [""]
+                    if key == "map_Bump":
+                        if mtex.normal_factor != 1.0:
+                            options += ['-bm', mtex.normal_factor]
+                    if mtex.offset != Vector((0.0, 0.0, 0.0)):
+                        options += ['-o', mtex.offset.x, mtex.offset.y, mtex.offset.z]
+                    if mtex.scale != Vector((1.0, 1.0, 1.0)):
+                        options += ['-s', mtex.scale.x, mtex.scale.y, mtex.scale.z]
+                    fw('%s%s %s\n' % (key, " ".join(options), repr(filepath)[1:-1]))
 
 
 def test_nurbs_compat(ob):
diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py
index a0db075..9f5200e 100644
--- a/io_scene_obj/import_obj.py
+++ b/io_scene_obj/import_obj.py
@@ -81,10 +81,21 @@ def create_materials(filepath, relpath,
     DIR = os.path.dirname(filepath)
     context_material_vars = set()
 
-    def load_material_image(blender_material, context_material_name, imagepath, type):
+    def load_material_image(blender_material, context_material_name, img_data, type):
         """
         Set textures defined in .mtl file.
         """
+        imagepath = img_data[-1]
+        map_options = {}
+
+        curr_token = []
+        for token in img_data[:-1]:
+            if token.startswith(b'-'):
+                if curr_token:
+                    map_options[curr_token[0]] = curr_token[1:]
+                curr_token[:] = []
+            curr_token.append(token)
+
         texture = bpy.data.textures.new(name=type, type='IMAGE')
 
         # Absolute path - c:\.. etc would work here
@@ -136,6 +147,10 @@ def create_materials(filepath, relpath,
             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':
             mtex = blender_material.texture_slots.add()
             mtex.use_map_color_diffuse = False
@@ -164,9 +179,30 @@ def create_materials(filepath, relpath,
             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:
+                mtex.offset.y = float(map_offset[1])
+            if len(map_offset) >= 3:
+                mtex.offset.z = float(map_offset[2])
+        if map_scale:
+            mtex.scale.x = float(map_scale[0])
+            if len(map_scale) >= 2:
+                mtex.scale.y = float(map_scale[1])
+            if len(map_scale) >= 3:
+                mtex.scale.z = float(map_scale[2])
+
     # Add an MTL with the same name as the obj if no MTLs are spesified.
     temp_mtl = os.path.splitext((os.path.basename(filepath)))[0] + b'.mtl'
 
@@ -360,39 +396,39 @@ def create_materials(filepath, relpath,
                             pass
 
                     elif line_id == b'map_ka':
-                        img_filepath = line_value(line.split())
-                        if img_filepath:
-                            load_material_image(context_material, context_material_name, img_filepath, 'Ka')
+                        img_data = line.split()[1:]
+                        if img_data:
+                            load_material_image(context_material, context_material_name, img_data, 'Ka')
                     elif line_id == b'map_ks':
-                        img_filepath = line_value(line.split())
-                        if img_filepath:
-                            load_material_image(context_material, context_material_name, img_filepath, 'Ks')
+                        img_data = line.split()[1:]
+                        if img_data:
+                            load_material_image(context_material, context_material_name, img_data, 'Ks')
                     elif line_id == b'map_kd':
-                        img_filepath = line_value(line.split())
-                        if img_filepath:
-                            load_material_image(context_material, context_material_name, img_filepath, 'Kd')
+                        img_data = line.split()[1:]
+                        if img_data:
+                            load_material_image(context_material, context_material_name, img_data, 'Kd')
                     elif line_id == b'map_ke':
-                        img_filepath = line_value(line.split())
-                        if img_filepath:
-                            load_material_image(context_material, context_material_name, img_filepath, 'Ke')
+                        img_data = line.split()[1:]
+                        if img_data:
+                            load_material_image(context_material, context_material_name, img_data, 'Ke')
                     elif line_id in {b'map_bump', b'bump'}:  # 'bump' is incorrect but some files use it.
-                        img_filepath = line_value(line.split())
-                        if img_filepath:
-                            load_material_image(context_material, context_material_name, img_filepath, 'Bump')
+                        img_data = line.split()[1:]
+                        if img_data:
+                            load_material_image(context_material, context_material_name, img_data, 'Bump')
                     elif line_id in {b'map_d', b'map_tr'}:  # Alpha map - Dissolve
-                        img_filepath = line_value(line.split())
-                        if img_filepath:
-                            load_material_image(context_material, context_material_name, img_filepath, 'D')
+                        img_dat

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list