Obj Material transparencies not automatically connected correctly when using jpeg #83749

Closed
opened 2020-12-14 09:24:46 +01:00 by Vaik Nay · 25 comments

System Information
Operating system: Windows-10-10.0.19041-SP0 64 Bits
Graphics card: GeForce GTX 1080/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 457.09

Blender Version
Broken: version: 2.92.0 Alpha, branch: master, commit date: 2020-12-12 18:07, hash: blender/blender@4797c13e8f
Worked: 2.90.1 Stable 23-Sep-20-06:43 3e85bb34d0d7

Short description of error
I noticed that the latest blender versions automatically connect alpha node of an image texture to the alpha node of the principled shader when importing obj. This would be fine if the image texture were a png that has transparencies. But with Jpegs this won't work as intended. It used to connect it correctly in this build: Ybrpk66MT_!YksF . Now I have to go in and manually correct every shader of all my imported shaders that came from an obj to the correct node. Which can take a long time, and stops me from using the "newer" Blender builds

Exact steps for others to reproduce the error
Just import an Obj mesh that uses transparency using jpeg image texture
[Based on the default startup or an attached .blend file (as simple as possible)]

**System Information** Operating system: Windows-10-10.0.19041-SP0 64 Bits Graphics card: GeForce GTX 1080/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 457.09 **Blender Version** Broken: version: 2.92.0 Alpha, branch: master, commit date: 2020-12-12 18:07, hash: `blender/blender@4797c13e8f` Worked: 2.90.1 Stable 23-Sep-20-06:43 3e85bb34d0d7 **Short description of error** I noticed that the latest blender versions automatically connect alpha node of an image texture to the alpha node of the principled shader when importing obj. This would be fine if the image texture were a png that has transparencies. But with Jpegs this won't work as intended. It used to connect it correctly in this build: Ybrpk66MT_!YksF . Now I have to go in and manually correct every shader of all my imported shaders that came from an obj to the correct node. Which can take a long time, and stops me from using the "newer" Blender builds **Exact steps for others to reproduce the error** Just import an Obj mesh that uses transparency using jpeg image texture [Based on the default startup or an attached .blend file (as simple as possible)]
Author

Added subscriber: @vaik.nay

Added subscriber: @vaik.nay

Added subscriber: @rjg

Added subscriber: @rjg

Changed status from 'Needs Triage' to: 'Needs User Info'

Changed status from 'Needs Triage' to: 'Needs User Info'

I've just tested this with blender/blender@c6075118d5 and blender/blender@4797c13e8f and it doesn't seem to connect the alpha output with the models I had around. Also when connecting the alpha channel on purpose the result still looks correct, since the .jpg doesn't have an alpha channel and this returns a value of 1.0 across the image for me.

Could you provide a minimal example .obj, .mtl and texture that shows this problem? For instance a cube with the texture applied.

I've just tested this with `blender/blender@c6075118d5` and `blender/blender@4797c13e8f` and it doesn't seem to connect the alpha output with the models I had around. Also when connecting the alpha channel on purpose the result still looks correct, since the .jpg doesn't have an alpha channel and this returns a value of 1.0 across the image for me. Could you provide a **minimal** example .obj, .mtl and texture that shows this problem? For instance a cube with the texture applied.
Author

Sure, here a plane with eyelashes

File here

Sure, here a plane with eyelashes [File here](https://drive.google.com/drive/folders/1tjV_6rWh_EXwJLXfUdIQx2cyRtiq-SFS?usp=sharing)

@vaik.nay Please upload the file here and make sure it's a minimal example.

@vaik.nay Please upload the file here and make sure it's a *minimal* example.
Author
[test_eyelashes.obj](https://archive.blender.org/developer/F9510551/test_eyelashes.obj) [test_eyelashes.mtl](https://archive.blender.org/developer/F9510550/test_eyelashes.mtl)

Changed status from 'Needs User Info' to: 'Archived'

Changed status from 'Needs User Info' to: 'Archived'

Thank you for the files. The problem is that the *.mtl contains a map_d instead of a map_Kd. The former is used for "dissolve", as the mtl specification calls it. This is essentially an alpha texture and how Blender interprets it. The latter is what you want for a diffuse texture. If you simply rename map_d to map_Kd it will import as expected.

This is not a bug in Blender, but either a configuration issue or bug in the software that created the *.mtl.

Thank you for the files. The problem is that the *.mtl contains a `map_d` instead of a `map_Kd`. The former is used for "dissolve", as the mtl specification calls it. This is essentially an alpha texture and how Blender interprets it. The latter is what you want for a diffuse texture. If you simply rename `map_d` to `map_Kd` it will import as expected. This is not a bug in Blender, but either a configuration issue or bug in the software that created the *.mtl.
Author

So why is it that previous blender versions open them just fine?

So why is it that previous blender versions open them just fine?

I think I might have misunderstood you. Your issue is not that the alpha socket gets an input, but rather that the alpha output of the image is used for jpegs instead of the color output, since jpegs don't have any alpha. Is that correct?

While I haven't verified this yet, the change could've been introduced by blender/blender@0696eaa3e8.

I think I might have misunderstood you. Your issue is not that the alpha socket gets an input, but rather that the alpha output of the image is used for jpegs instead of the color output, since jpegs don't have any alpha. Is that correct? While I haven't verified this yet, the change could've been introduced by blender/blender@0696eaa3e8.
Author

Correct. It's a huge problem in workflows that depends a lot on importing obj files using jpegs for transparencies.

Correct. It's a huge problem in workflows that depends a lot on importing obj files using jpegs for transparencies.

Changed status from 'Archived' to: 'Needs Triage'

Changed status from 'Archived' to: 'Needs Triage'

I'm reopening the ticket. This needs further investigation.

I'm reopening the ticket. This needs further investigation.

Added subscriber: @iss

Added subscriber: @iss

This issue is caused by blender/blender@0696eaa3e8.

Not sure if checking extension of image would be acceptable. Crude, but it would work I guess?

diff --git a/release/scripts/modules/bpy_extras/node_shader_utils.py b/release/scripts/modules/bpy_extras/node_shader_utils.py
index 5ddb42e49fc..3e08d9e73a5 100644
--- a/release/scripts/modules/bpy_extras/node_shader_utils.py
+++ b/release/scripts/modules/bpy_extras/node_shader_utils.py
@@ -464,13 +464,13 @@ class PrincipledBSDFWrapper(ShaderWrapper):
     alpha = property(alpha_get, alpha_set)

     # Will only be used as gray-scale one...
-    def alpha_texture_get(self):
+    def alpha_texture_get(self, use_alpha=True):
         if not self.use_nodes or self.node_principled_bsdf is None:
             return None
         return ShaderImageTextureWrapper(
             self, self.node_principled_bsdf,
             self.node_principled_bsdf.inputs["Alpha"],
-            use_alpha=True,
+            use_alpha=use_alpha,
             grid_row_diff=-1,
             colorspace_name='Non-Color',
         )
diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py
index 470e85ec..222b2d18 100644
--- a/io_scene_obj/import_obj.py
+++ b/io_scene_obj/import_obj.py
@@ -205,7 +205,15 @@ def create_materials(filepath, relpath,
             _generic_tex_set(mat_wrap.normalmap_texture, image, 'UV', map_offset, map_scale)

         elif type == 'D':
-            _generic_tex_set(mat_wrap.alpha_texture, image, 'UV', map_offset, map_scale)
+            ext = os.path.splitext(image.filepath)[1]
+            # Use color output of image texture with images that doesn't have alpha channel.
+            if ext == '.jpg':
+                nodetex = mat_wrap.alpha_texture_get(use_alpha=False)
+                nodetex.image = image
+                #...
+                #...
+            else:
+                _generic_tex_set(mat_wrap.alpha_texture, image, 'UV', map_offset, map_scale)

         elif type == 'disp':
             # XXX Not supported?
This issue is caused by blender/blender@0696eaa3e8. Not sure if checking extension of image would be acceptable. Crude, but it would work I guess? ``` diff --git a/release/scripts/modules/bpy_extras/node_shader_utils.py b/release/scripts/modules/bpy_extras/node_shader_utils.py index 5ddb42e49fc..3e08d9e73a5 100644 --- a/release/scripts/modules/bpy_extras/node_shader_utils.py +++ b/release/scripts/modules/bpy_extras/node_shader_utils.py @@ -464,13 +464,13 @@ class PrincipledBSDFWrapper(ShaderWrapper): alpha = property(alpha_get, alpha_set) # Will only be used as gray-scale one... - def alpha_texture_get(self): + def alpha_texture_get(self, use_alpha=True): if not self.use_nodes or self.node_principled_bsdf is None: return None return ShaderImageTextureWrapper( self, self.node_principled_bsdf, self.node_principled_bsdf.inputs["Alpha"], - use_alpha=True, + use_alpha=use_alpha, grid_row_diff=-1, colorspace_name='Non-Color', ) ``` ``` diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py index 470e85ec..222b2d18 100644 --- a/io_scene_obj/import_obj.py +++ b/io_scene_obj/import_obj.py @@ -205,7 +205,15 @@ def create_materials(filepath, relpath, _generic_tex_set(mat_wrap.normalmap_texture, image, 'UV', map_offset, map_scale) elif type == 'D': - _generic_tex_set(mat_wrap.alpha_texture, image, 'UV', map_offset, map_scale) + ext = os.path.splitext(image.filepath)[1] + # Use color output of image texture with images that doesn't have alpha channel. + if ext == '.jpg': + nodetex = mat_wrap.alpha_texture_get(use_alpha=False) + nodetex.image = image + #... + #... + else: + _generic_tex_set(mat_wrap.alpha_texture, image, 'UV', map_offset, map_scale) elif type == 'disp': # XXX Not supported? ```
Author

You mean using png instead of jpeg?

You mean using png instead of jpeg?

@iss that's maybe not the cleanest solution as we would have to repeat this for all image formats without an alpha channel, but I currently don't see a cleaner way to do this with the Python API (e.g. we cannot use alpha_mode to check this, as this is still set to STRAIGHT when using JPEG, although it doesn't have an alpha channel).

@iss that's maybe not the cleanest solution as we would have to repeat this for all image formats without an alpha channel, but I currently don't see a cleaner way to do this with the Python API (e.g. we cannot use `alpha_mode` to check this, as this is still set to `STRAIGHT` when using JPEG, although it doesn't have an alpha channel).
Author

Wouldn't it be possible to have the program check if there is an alpha channel before connecting it so? Or at least have it be as it was before where jpegs would always be connected with from the color node?

Wouldn't it be possible to have the program check if there is an alpha channel before connecting it so? Or at least have it be as it was before where jpegs would always be connected with from the color node?

@iss There might be a more elegant way though the Python API. We could check if .depth is 24 (3 * 8) and thus the image has no alpha channel.

@vaik.nay That's what we are trying to do. No, we cannot revert the change introduced in blender/blender@0696eaa3e8, as the modification of the defaults affects other add-ons too.

@iss There might be a more elegant way though the Python API. We could check if `.depth` is 24 (3 * 8) and thus the image has no alpha channel. @vaik.nay That's what we are trying to do. No, we cannot revert the change introduced in `blender/blender@0696eaa3e8`, as the modification of the defaults affects other add-ons too.
Member

Added subscribers: @mont29, @lichtwerk

Added subscribers: @mont29, @lichtwerk
Member

Changed status from 'Needs Triage' to: 'Needs Developer To Reproduce'

Changed status from 'Needs Triage' to: 'Needs Developer To Reproduce'
Member

@mont29 , to not get stuck with this issue: could you make a decision here?

Quoting the commit message again:

This will break people using a dedicated B&W texture for this alpha
pass, but we cannot really handle all cases properly in this wrapper, we
only try to support the most common ones to some extent.

OTOH, checking depth sounds like a useful improvement?

@mont29 , to not get stuck with this issue: could you make a decision here? Quoting the commit message again: > This will break people using a dedicated B&W texture for this alpha > pass, but we cannot really handle all cases properly in this wrapper, we > only try to support the most common ones to some extent. OTOH, checking depth sounds like a useful improvement?

This issue was referenced by blender/blender@ae82410329

This issue was referenced by blender/blender@ae8241032959c7bc225dfaef3b75f44b1f65577b

Changed status from 'Needs Developer To Reproduce' to: 'Resolved'

Changed status from 'Needs Developer To Reproduce' to: 'Resolved'
Bastien Montagne self-assigned this 2020-12-29 16:43:15 +01:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
6 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#83749
No description provided.