OBJ import - load_material_image - map_options not parsed correctly #52945

Closed
opened 2017-09-29 01:40:38 +02:00 by Tim Knip · 10 comments

System Information
Operating system and graphics card
Win 8.1 - GeForce GTX 765M

Blender Version
Broken: 2.79 - #5bd8ac9
Worked: (optional)

Short description of error

This part seems to be broken. Doesn't yield a valid map

        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)

Possible fix:

       map_options = {}
        curr_token = []
        for token in img_data[:-1]:
            if token.startswith(b'-'):	
                curr_token.append(token)
            elif len(curr_token) == 1:
                map_options[curr_token[0]] = token
                curr_token[:] = []

        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:
                    # make sure we parse a float
                    bm = float(bump_mult.decode('utf8'))
                    mat_wrap.normal_factor_set(bm)

            mtex = blender_material.texture_slots.add()
            mtex.use_map_color_diffuse = False
            mtex.texture = texture
            mtex.texture_coords = 'UV'
            mtex.use_map_normal = True
            if bump_mult:
                # make sure we parse a float
                bm = float(bump_mult.decode('utf8'))
                mtex.normal_factor = bm

Furthermore:
map_options (float) values are prefixed with 'bytes' : eg b'0.2'.
When a float value needs to be set, the value needs to be decoded from utf8.
eg: something like float(b'0.2'.decode('utf8'))

Exact steps for others to reproduce the error
Based on a (as simple as possible) attached .blend file with minimum amount of steps
zzz.mtl

Import attached OBJ file with bump maps and observe that the "-bm" map_option isn't set correctly on "blender_material.texture_slots- [x].normal_factor".

F_MK_Ukr2462.jpg

W_AV_UKR_2307_knot_NRM.jpg
zzz.obj

F_MK_Ukr2462.jpg

W_AV_UKR_2306_knot_NRM.jpg

W_AV_UKR_2309_nots_NRM2.jpg

Pillow_Normal.jpg

Met_DB_LO_0583.jpg

**System Information** Operating system and graphics card Win 8.1 - GeForce GTX 765M **Blender Version** Broken: 2.79 - #5bd8ac9 Worked: (optional) **Short description of error** This part seems to be broken. Doesn't yield a valid map ``` 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) ``` Possible fix: ``` map_options = {} curr_token = [] for token in img_data[:-1]: if token.startswith(b'-'): curr_token.append(token) elif len(curr_token) == 1: map_options[curr_token[0]] = token curr_token[:] = [] 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: # make sure we parse a float bm = float(bump_mult.decode('utf8')) mat_wrap.normal_factor_set(bm) mtex = blender_material.texture_slots.add() mtex.use_map_color_diffuse = False mtex.texture = texture mtex.texture_coords = 'UV' mtex.use_map_normal = True if bump_mult: # make sure we parse a float bm = float(bump_mult.decode('utf8')) mtex.normal_factor = bm ``` Furthermore: map_options (float) values are prefixed with 'bytes' : eg b'0.2'. When a float value needs to be set, the value needs to be decoded from utf8. eg: something like float(b'0.2'.decode('utf8')) **Exact steps for others to reproduce the error** Based on a (as simple as possible) attached .blend file with minimum amount of steps [zzz.mtl](https://archive.blender.org/developer/F907869/zzz.mtl) Import attached OBJ file with bump maps and observe that the "-bm" map_option isn't set correctly on "blender_material.texture_slots- [x].normal_factor". ![F_MK_Ukr2462.jpg](https://archive.blender.org/developer/F907900/F_MK_Ukr2462.jpg) ![W_AV_UKR_2307_knot_NRM.jpg](https://archive.blender.org/developer/F907901/W_AV_UKR_2307_knot_NRM.jpg) [zzz.obj](https://archive.blender.org/developer/F907870/zzz.obj) ![F_MK_Ukr2462.jpg](https://archive.blender.org/developer/F907903/F_MK_Ukr2462.jpg) ![W_AV_UKR_2306_knot_NRM.jpg](https://archive.blender.org/developer/F907905/W_AV_UKR_2306_knot_NRM.jpg) ![W_AV_UKR_2309_nots_NRM2.jpg](https://archive.blender.org/developer/F907907/W_AV_UKR_2309_nots_NRM2.jpg) ![Pillow_Normal.jpg](https://archive.blender.org/developer/F907909/Pillow_Normal.jpg) ![Met_DB_LO_0583.jpg](https://archive.blender.org/developer/F907916/Met_DB_LO_0583.jpg)
Author

Changed status to: 'Open'

Changed status to: 'Open'
Author

Added subscriber: @timknip

Added subscriber: @timknip
Author

proposed.patch

Here's a somewhat ugly solution.

diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py
index c3390d4d..13e26b0d 100644
--- a/io_scene_obj/import_obj.py
+++ b/io_scene_obj/import_obj.py
@@ -110,10 +110,15 @@ def create_materials(filepath, relpath,
         curr_token = []
         for token in img_data[:-1]:
             if token.startswith(b'-'):
-                if curr_token:
+                if len(curr_token) > 1:
                     map_options[curr_token[0]] = curr_token[1:]
-                curr_token[:] = []
-            curr_token.append(token)
+                    curr_token[:] = []
+                curr_token = [token]
+            else:
+                curr_token.append(token)
+        if len(curr_token) > 1:
+            map_options[curr_token[0]] = curr_token[1:]
+            curr_token[:] = []

         # Absolute path - c:\.. etc would work here
         image = obj_image_load(context_imagepath_map, line, DIR, use_image_search, relpath)
@@ -180,7 +185,7 @@ def create_materials(filepath, relpath,
                 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])
+                    mat_wrap.normal_factor_set(float_func(bump_mult[0]))

             mtex = blender_material.texture_slots.add()
             mtex.use_map_color_diffuse = False
@@ -188,7 +193,7 @@ def create_materials(filepath, relpath,
             mtex.texture_coords = 'UV'
             mtex.use_map_normal = True
             if bump_mult:
-                mtex.normal_factor = bump_mult[0]
+                mtex.normal_factor = float_func(bump_mult[0])

         elif type == 'D':
             if use_cycles:
[proposed.patch](https://archive.blender.org/developer/F912196/proposed.patch) Here's a somewhat ugly solution. ``` diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py index c3390d4d..13e26b0d 100644 --- a/io_scene_obj/import_obj.py +++ b/io_scene_obj/import_obj.py @@ -110,10 +110,15 @@ def create_materials(filepath, relpath, curr_token = [] for token in img_data[:-1]: if token.startswith(b'-'): - if curr_token: + if len(curr_token) > 1: map_options[curr_token[0]] = curr_token[1:] - curr_token[:] = [] - curr_token.append(token) + curr_token[:] = [] + curr_token = [token] + else: + curr_token.append(token) + if len(curr_token) > 1: + map_options[curr_token[0]] = curr_token[1:] + curr_token[:] = [] # Absolute path - c:\.. etc would work here image = obj_image_load(context_imagepath_map, line, DIR, use_image_search, relpath) @@ -180,7 +185,7 @@ def create_materials(filepath, relpath, 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]) + mat_wrap.normal_factor_set(float_func(bump_mult[0])) mtex = blender_material.texture_slots.add() mtex.use_map_color_diffuse = False @@ -188,7 +193,7 @@ def create_materials(filepath, relpath, mtex.texture_coords = 'UV' mtex.use_map_normal = True if bump_mult: - mtex.normal_factor = bump_mult[0] + mtex.normal_factor = float_func(bump_mult[0]) elif type == 'D': if use_cycles: ```
Author

Simpler: so the problems were:

  • if curr_token still had items after the loop, these items were not added to map_options.
  • bump_map- [x] was not cast to float.
diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py
index c3390d4d..bfa2d90b 100644
--- a/io_scene_obj/import_obj.py
+++ b/io_scene_obj/import_obj.py
@@ -114,6 +114,8 @@ def create_materials(filepath, relpath,
                     map_options[curr_token[0]] = curr_token[1:]
                 curr_token[:] = []
             curr_token.append(token)
+        if curr_token:
+            map_options[curr_token[0]] = curr_token[1:]

         # Absolute path - c:\.. etc would work here
         image = obj_image_load(context_imagepath_map, line, DIR, use_image_search, relpath)
@@ -180,7 +182,7 @@ def create_materials(filepath, relpath,
                 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])
+                    mat_wrap.normal_factor_set(float(bump_mult[0]))

             mtex = blender_material.texture_slots.add()
             mtex.use_map_color_diffuse = False
@@ -188,7 +190,7 @@ def create_materials(filepath, relpath,
             mtex.texture_coords = 'UV'
             mtex.use_map_normal = True
             if bump_mult:
-                mtex.normal_factor = bump_mult[0]
+                mtex.normal_factor = float(bump_mult[0])

         elif type == 'D':
             if use_cycles:

better.patch

Simpler: so the problems were: - if `curr_token` still had items after the loop, these items were not added to `map_options`. - `bump_map- [x]` was not cast to float. ``` diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py index c3390d4d..bfa2d90b 100644 --- a/io_scene_obj/import_obj.py +++ b/io_scene_obj/import_obj.py @@ -114,6 +114,8 @@ def create_materials(filepath, relpath, map_options[curr_token[0]] = curr_token[1:] curr_token[:] = [] curr_token.append(token) + if curr_token: + map_options[curr_token[0]] = curr_token[1:] # Absolute path - c:\.. etc would work here image = obj_image_load(context_imagepath_map, line, DIR, use_image_search, relpath) @@ -180,7 +182,7 @@ def create_materials(filepath, relpath, 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]) + mat_wrap.normal_factor_set(float(bump_mult[0])) mtex = blender_material.texture_slots.add() mtex.use_map_color_diffuse = False @@ -188,7 +190,7 @@ def create_materials(filepath, relpath, mtex.texture_coords = 'UV' mtex.use_map_normal = True if bump_mult: - mtex.normal_factor = bump_mult[0] + mtex.normal_factor = float(bump_mult[0]) elif type == 'D': if use_cycles: ``` [better.patch](https://archive.blender.org/developer/F912220/better.patch)

Added subscribers: @mont29, @ideasman42, @Sergey

Added subscribers: @mont29, @ideasman42, @Sergey
Campbell Barton was assigned by Sergey Sharybin 2017-10-10 11:48:17 +02:00

@ideasman42 or @mont29, mind having a look here? Thanks!

@ideasman42 or @mont29, mind having a look here? Thanks!

@timknip thanks a lot for report and investigation, last patch makes total sense indeed, will commit.

@timknip thanks a lot for report and investigation, last patch makes total sense indeed, will commit.
Campbell Barton was unassigned by Bastien Montagne 2017-10-10 12:31:21 +02:00
Bastien Montagne self-assigned this 2017-10-10 12:31:21 +02:00

This issue was referenced by 1f868b2e5d

This issue was referenced by 1f868b2e5d05d31bd0186d14b04fd5a86e6f20f8

This issue was referenced by e8f09a8e0b

This issue was referenced by e8f09a8e0b28eb7eeb6bfc37d9957c3b680e6da5

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'
Sign in to join this conversation.
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender-addons#52945
No description provided.