Can't import OBJs that were exported with Apple's Model I/O API (Patch included) #71618

Closed
opened 2019-11-15 23:56:50 +01:00 by Peter Wunder · 20 comments

System Information
Operating system: Windows 10 1903 (18362.418)
Graphics card: Nvidia GTX 1070

Blender Version
Broken: 2.80

Short description of error
OBJ files that were generated using Apple's Model I/O API trip up Blender's MTL parser, making it impossible to import them.

Exact steps for others to reproduce the error
Download the following files.
They were generated by downloading the publicly accessible .usdz model of the AirPods Pro from Apple's product website and converting it to .obj using this tool I wrote: https://github.com/SamusAranX/USDConverter/blob/master/USDConverter/main.swift

airpods_pro_ios13.obj
airpods_pro_ios13.mtl

Try to import the .obj into Blender as you would with any other .obj file.
Get an error message telling you that the list index is out of range.

I wrote a silly little patch for myself, but re-applying it after every update is getting to be tedious, so I finally created an account here.
My patched import_obj.py file is here: https://gist.github.com/SamusAranX/de1663abe2346fc0dc49791ec9d5205a
I don't have a compehensive test suite, but for me, at least, it has reliably worked without breaking anything else so far.
I'm new to contributing to Blender, so please forgive me if this isn't the proper channel for this.

**System Information** Operating system: Windows 10 1903 (18362.418) Graphics card: Nvidia GTX 1070 **Blender Version** Broken: 2.80 **Short description of error** OBJ files that were generated using Apple's Model I/O API trip up Blender's MTL parser, making it impossible to import them. **Exact steps for others to reproduce the error** Download the following files. They were generated by downloading the publicly accessible .usdz model of the AirPods Pro from Apple's product website and converting it to .obj using this tool I wrote: https://github.com/SamusAranX/USDConverter/blob/master/USDConverter/main.swift [airpods_pro_ios13.obj](https://archive.blender.org/developer/F8096791/airpods_pro_ios13.obj) [airpods_pro_ios13.mtl](https://archive.blender.org/developer/F8096789/airpods_pro_ios13.mtl) Try to import the .obj into Blender as you would with any other .obj file. Get an error message telling you that the list index is out of range. I wrote a silly little patch for myself, but re-applying it after every update is getting to be tedious, so I finally created an account here. My patched import_obj.py file is here: https://gist.github.com/SamusAranX/de1663abe2346fc0dc49791ec9d5205a I don't have a compehensive test suite, but for me, at least, it has reliably worked without breaking anything else so far. I'm new to contributing to Blender, so please forgive me if this isn't the proper channel for this.
Author

Added subscriber: @GordenFreemant

Added subscriber: @GordenFreemant

blender/blender-addons#64264 was marked as duplicate of this issue

blender/blender-addons#64264 was marked as duplicate of this issue

Added subscriber: @StephenSwaney

Added subscriber: @StephenSwaney

What exactly is the problem?

Also, a diff would be nice.

What exactly is the problem? Also, a diff would be nice.
Author

bug.png

This is the problem.
The .mtl files generated by Model I/O don't use Ks x y z, they do Ks scalar with just one number.
I don't know if there's actually any official .mtl standard that this violates, but it'd be great to support this in Blender anyway.

Regarding the diff, is this okay?

diff --git a/import_obj.py b/import_obj.py
index c565bd0..76cb351 100644
--- a/import_obj.py
+++ b/import_obj.py
@@ -324,6 +324,11 @@ def create_materials(filepath, relpath,
                         col = (float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3]))
                         context_mat_wrap.base_color = col
                     elif line_id == b'ks':
+                        # Adds support for MTL files exported by Model I/O
+                        # Those files often just have one number instead of three following the "Ks"
+                        if len(line_split[1:]) < 3:
+                            line_split[1:4] = [line_split[1], line_split[1], line_split[1]]
+
                         spec_colors[:] = [
                             float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3])]
                         context_material_vars.add("specular")

![bug.png](https://archive.blender.org/developer/F8106160/bug.png) This is the problem. The .mtl files generated by Model I/O don't use `Ks x y z`, they do `Ks scalar` with just one number. I don't know if there's actually any official .mtl standard that this violates, but it'd be great to support this in Blender anyway. Regarding the diff, is this okay? ``` diff --git a/import_obj.py b/import_obj.py index c565bd0..76cb351 100644 --- a/import_obj.py +++ b/import_obj.py @@ -324,6 +324,11 @@ def create_materials(filepath, relpath, col = (float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3])) context_mat_wrap.base_color = col elif line_id == b'ks': + # Adds support for MTL files exported by Model I/O + # Those files often just have one number instead of three following the "Ks" + if len(line_split[1:]) < 3: + line_split[1:4] = [line_split[1], line_split[1], line_split[1]] + spec_colors[:] = [ float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3])] context_material_vars.add("specular") ```
Member

Added subscriber: @Harley

Added subscriber: @Harley
Member

The report seems a little confusing. It reads like "Apple has files in a new format and when I convert them myself using a particular library the resulting output is not valid"

But it appears to be a much simpler bug.

As far as I can see, MTL material files have material color statements (Ka, Kd, Ks, Tf) that are generally r g b values (0-1) separated by spaces. But the spec does say that the "g and b arguments are optional. If only r is specified, then g, and b are assumed to be equal to r."

Your supplied "mtl" file contains many lines that are a single value like "Ks 0". In this case our import should treat this as if it were "Ks 0 0 0".

If our import is indeed barfing on single values there then that is the bug to be reported, along with this kind of detail and sample files. But if you also want to submit a fix for this problem you'd really need to handle ALL of these color statements (Ka, Kd, Ks, Tf), not just Ks, since all can take single values.

The report seems a little confusing. It reads like "Apple has files in a new format and when I convert them myself using a particular library the resulting output is not valid" But it appears to be a much simpler bug. As far as I can see, [MTL material files ](http://paulbourke.net/dataformats/mtl/) have material color statements (Ka, Kd, Ks, Tf) that are generally r g b values (0-1) separated by spaces. But the spec does say that the "g and b arguments are optional. If only r is specified, then g, and b are assumed to be equal to r." Your supplied "mtl" file contains many lines that are a single value like "Ks 0". In this case our import should treat this as if it were "Ks 0 0 0". If our import is indeed barfing on single values there then **that** is the bug to be reported, along with this kind of detail and sample files. But if you also want to submit a fix for this problem you'd really need to handle ALL of these color statements (Ka, Kd, Ks, Tf), not just Ks, since all can take single values.

Thanks, Peter. That is exactly I was looking for.
The fix looks good. It even has comments!

Handling the other K* statements would be nice for completeness, but I have no idea if they are common in MTL files.

  • Ka - material ambient is multiplied by the texture value
  • Kd - material diffuse is multiplied by the texture value
  • Ks - material specular is multiplied by the texture value
Thanks, Peter. That is exactly I was looking for. The fix looks good. It even has comments! Handling the other K* statements would be nice for completeness, but I have no idea if they are common in MTL files. - Ka - material ambient is multiplied by the texture value - Kd - material diffuse is multiplied by the texture value - Ks - material specular is multiplied by the texture value
Bastien Montagne self-assigned this 2019-11-18 10:29:14 +01:00
Author

I updated the diff for all three of the K* statements.
I took a look at Tf as well, but it's apparently not supported by Blender at all, so there were no changes necessary.

diff --git a/import_obj.py b/import_obj.py
index 7be5b75..7ec1168 100644
--- a/import_obj.py
+++ b/import_obj.py
@@ -317,13 +317,25 @@ def create_materials(filepath, relpath,
                 elif context_material:
                     # we need to make a material to assign properties to it.
                     if line_id == b'ka':
+                        # expand single value to three values
+                        if len(line_split[1:]) < 3:
+                            line_split[1:4] = [line_split[1], line_split[1], line_split[1]]
+
                         refl = (float_func(line_split[1]) + float_func(line_split[2]) + float_func(line_split[3])) / 3.0
                         context_mat_wrap.metallic = refl
                         context_material_vars.add("metallic")
                     elif line_id == b'kd':
+                        # expand single value to three values
+                        if len(line_split[1:]) < 3:
+                            line_split[1:4] = [line_split[1], line_split[1], line_split[1]]
+
                         col = (float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3]))
                         context_mat_wrap.base_color = col
                     elif line_id == b'ks':
+                        # expand single value to three values
+                        if len(line_split[1:]) < 3:
+                            line_split[1:4] = [line_split[1], line_split[1], line_split[1]]
+
                         spec_colors[:] = [
                             float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3])]
                         context_material_vars.add("specular")

I updated the diff for all three of the K* statements. I took a look at Tf as well, but it's apparently not supported by Blender at all, so there were no changes necessary. ``` diff --git a/import_obj.py b/import_obj.py index 7be5b75..7ec1168 100644 --- a/import_obj.py +++ b/import_obj.py @@ -317,13 +317,25 @@ def create_materials(filepath, relpath, elif context_material: # we need to make a material to assign properties to it. if line_id == b'ka': + # expand single value to three values + if len(line_split[1:]) < 3: + line_split[1:4] = [line_split[1], line_split[1], line_split[1]] + refl = (float_func(line_split[1]) + float_func(line_split[2]) + float_func(line_split[3])) / 3.0 context_mat_wrap.metallic = refl context_material_vars.add("metallic") elif line_id == b'kd': + # expand single value to three values + if len(line_split[1:]) < 3: + line_split[1:4] = [line_split[1], line_split[1], line_split[1]] + col = (float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3])) context_mat_wrap.base_color = col elif line_id == b'ks': + # expand single value to three values + if len(line_split[1:]) < 3: + line_split[1:4] = [line_split[1], line_split[1], line_split[1]] + spec_colors[:] = [ float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3])] context_material_vars.add("specular") ```

This issue was referenced by blender/blender-addons@4fd8ee12db

This issue was referenced by blender/blender-addons@4fd8ee12dbaba9fb21dad8f3c2a8718fb69f27a0

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'
Author

Ooh, that's even better. Thanks for the quick fix!

Ooh, that's even better. Thanks for the quick fix!
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk
Member

That was the same as blender/blender-addons#64264 (OBJ import gives error for Ke / emmisive (single value instead of color)), right?

That was the same as blender/blender-addons#64264 (OBJ import gives error for Ke / emmisive (single value instead of color)), right?
Added subscribers: @filibis, @pipelineTDBadBoy, @HasKha, @Nocturnial, @BrendonMurphy, @DJJudd, @mont29

Indeed, thanks for the headup :)

Indeed, thanks for the headup :)

In #71618#812440, @lichtwerk wrote:
That was the same as blender/blender-addons#64264 (OBJ import gives error for Ke / emmisive (single value instead of color)), right?

It was the same, but the merged fix did not include the Ke (emmisive) parameter. Is it possible to add that in? Even though Ke is not in the original OBJ spec, it is defined in the PBR Extension, and is supported by Blender if defined in the three-tuple format: Ke 0.0 0.0 0.0.

Additional info:
http://exocortex.com/blog/extending_wavefront_mtl_to_support_pbr

This is my first time commenting here, so please let me know if I'm breaking any etiquette rules. :)

> In #71618#812440, @lichtwerk wrote: > That was the same as blender/blender-addons#64264 (OBJ import gives error for Ke / emmisive (single value instead of color)), right? It was the same, but the merged fix did not include the Ke (emmisive) parameter. Is it possible to add that in? Even though Ke is not in the original OBJ spec, it is defined in the PBR Extension, and is supported by Blender if defined in the three-tuple format: Ke 0.0 0.0 0.0. Additional info: http://exocortex.com/blog/extending_wavefront_mtl_to_support_pbr This is my first time commenting here, so please let me know if I'm breaking any etiquette rules. :)

Removed subscriber: @Nocturnial

Removed subscriber: @Nocturnial

@pipelineTDBadBoy I’d suggest you actually check the commit first, you'll see that it did include fix for Ke as well…

@pipelineTDBadBoy I’d suggest you actually check the commit first, you'll see that it did include fix for Ke as well…

My apologies, I assumed the pasted code in the thread was the commit. I'm still learning how things work here. Thanks again, this fix is very helpful!!

My apologies, I assumed the pasted code in the thread was the commit. I'm still learning how things work here. Thanks again, this fix is very helpful!!
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
8 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#71618
No description provided.