Invalid translation on keys in python API [new object material node names (Principled BSDF, Material Output) are now translated upon creation] #104145

Closed
opened 2023-01-26 02:44:21 +01:00 by Satoshi Yamasaki · 27 comments

System Information
Operating system: Windows 10 pro x64
Graphics card: RTX 2060 12GB

Blender Version
Broken: 3.4
Worked: 3.3 (and perhaps before)

behavior changed in 9d732445b9

Short description of error
Addon compatibiliry is broken with Invalid translation on keys in python API

In 3.4 with translated UI, some addons (E.g. Dream Texture) gives errors. for example,

"material.node_tree.links.new(image_texture_node.outputs- [x], material.node_tree.nodes['Principled BSDF'].inputs[0])"
"KeyError: 'bpy_prop_collection[key]: key "Principled BSDF" not found'"

It doesn't be occured without UI translation (using us_en) and 3.3 and before. It's similar issue that has been occured with translated new data name (Add Camera Rig for example), but this issue is occured due to invalid translating key names with the UI translation funciton dynamically.

Exact steps for others to reproduce the error

  1. Open Preferences, go to Interface tab, and change Translation > Language to (let's say) Japanese.
  2. Open Python Console and enter
>>> mat = D.materials.new("Mat")
>>> mat.use_nodes = True
>>> mat.node_tree.nodes["

  1. Press Tab to autocomplete.
  2. Observe node names. Here's a comparison between 3.5 (left) and 3.3.2 (right).

names.png

**System Information** Operating system: Windows 10 pro x64 Graphics card: RTX 2060 12GB **Blender Version** Broken: 3.4 Worked: 3.3 (and perhaps before) behavior changed in 9d732445b9 **Short description of error** Addon compatibiliry is broken with Invalid translation on keys in python API In 3.4 with translated UI, some addons (E.g. Dream Texture) gives errors. for example, "material.node_tree.links.new(image_texture_node.outputs- [x], material.node_tree.nodes['Principled BSDF'].inputs[0])" "KeyError: 'bpy_prop_collection[key]: key "Principled BSDF" not found'" It doesn't be occured without UI translation (using us_en) and 3.3 and before. It's similar issue that has been occured with translated new data name (Add Camera Rig for example), but this issue is occured due to invalid translating key names with the UI translation funciton dynamically. **Exact steps for others to reproduce the error** 1. Open Preferences, go to Interface tab, and change Translation > Language to (let's say) Japanese. 2. Open Python Console and enter ``` >>> mat = D.materials.new("Mat") >>> mat.use_nodes = True >>> mat.node_tree.nodes[" ``` 3. Press Tab to autocomplete. 4. Observe node names. Here's a comparison between 3.5 (left) and 3.3.2 (right). ![names.png](https://archive.blender.org/developer/F14199489/names.png)
Author
Member

Added subscriber: @SatoshiYamasaki

Added subscriber: @SatoshiYamasaki
Bastien Montagne was assigned by Satoshi Yamasaki 2023-01-26 02:48:01 +01:00
Contributor

Added subscriber: @scurest

Added subscriber: @scurest
Member

Added subscriber: @PratikPB2123

Added subscriber: @PratikPB2123
Member

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

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

Hi, thanks for the report. Not sure how to verify this issue locally. Can you share exact steps to repro the issue?

Hi, thanks for the report. Not sure how to verify this issue locally. Can you share exact steps to repro the issue?
Contributor
  1. Open Preferences, go to Interface tab, and change Translation > Language to (let's say) Japanese.
  2. Open Python Console and enter
>>> mat = D.materials.new("Mat")
>>> mat.use_nodes = True
>>> mat.node_tree.nodes["

  1. Press Tab to autocomplete.
  2. Observe node names. Here's a comparison between 3.5 (left) and 3.3.2 (right).

names.png

1. Open Preferences, go to Interface tab, and change Translation > Language to (let's say) Japanese. 2. Open Python Console and enter ``` >>> mat = D.materials.new("Mat") >>> mat.use_nodes = True >>> mat.node_tree.nodes[" ``` 3. Press Tab to autocomplete. 4. Observe node names. Here's a comparison between 3.5 (left) and 3.3.2 (right). ![names.png](https://archive.blender.org/developer/F14199489/names.png)
Member

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

Changed status from 'Needs User Info' to: 'Confirmed'
Member

Thanks @scurest
Bisecting.
getting build errors on intermediate commits so unable to bisect.

Thanks @scurest ~~Bisecting.~~ getting build errors on intermediate commits so unable to bisect.
Member

Added subscribers: @pioverfour, @lichtwerk

Added subscribers: @pioverfour, @lichtwerk
Member

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

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

Hm, so behavior changed in 9d732445b9

It might be expected behavior though, not sure if Addons can rely on such a "hardcoded" name in a translated environment (I would assume though that many do).

That being said, the following works as expected (this could be forwarded to the Addon devs as a fix/workaround @SatoshiYamasaki ):

mat = D.materials.new("Mat")
mat.use_nodes = True
mat.node_tree.nodes[bpy.app.translations.pgettext_iface("Principled BSDF")]

In any case, I am raising priority here to get an answer from @mont29 and @pioverfour .
I am not seeing this mentioned in the release notes https://wiki.blender.org/wiki/Reference/Release_Notes/3.4/Python_API (this should be done as a minimal step I think)

Hm, so behavior changed in 9d732445b9 It might be expected behavior though, not sure if Addons can rely on such a "hardcoded" name in a translated environment (I would assume though that many do). That being said, the following works as expected (this could be forwarded to the Addon devs as a fix/workaround @SatoshiYamasaki ): ``` mat = D.materials.new("Mat") mat.use_nodes = True mat.node_tree.nodes[bpy.app.translations.pgettext_iface("Principled BSDF")] ``` In any case, I am raising priority here to get an answer from @mont29 and @pioverfour . I am not seeing this mentioned in the release notes https://wiki.blender.org/wiki/Reference/Release_Notes/3.4/Python_API (this should be done as a minimal step I think)
Philipp Oeser changed title from Invalid translation on keys in python API to Invalid translation on keys in python API [new object material node names (Principled BSDF, Material Output) are now translated upon creation] 2023-01-26 12:58:11 +01:00
Bastien Montagne was unassigned by Philipp Oeser 2023-01-26 12:58:11 +01:00
Member

Added subscriber: @mont29

Added subscriber: @mont29
Author
Member

Thank you guys and I'm sorry for missing informations.

@lichtwerk: Thank you the code. But I think this new behaivor causes problems than benefits because I don't believe that most of addon devs think about translated environment.
So, It should be reverted I think.

@pioverfour, @mont29: Do you think about it?

Thank you guys and I'm sorry for missing informations. @lichtwerk: Thank you the code. But I think this new behaivor causes problems than benefits because I don't believe that most of addon devs think about translated environment. So, It should be reverted I think. @pioverfour, @mont29: Do you think about it?
Author
Member

Oops...this issue is same as other existing issues, just turn off "New Data" option of translation to solve it.
I apologize about this report...

Oops...this issue is same as other existing issues, just turn off "New Data" option of translation to solve it. I apologize about this report...
Author
Member

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

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

What issue is this a dupe of?

What issue is this a dupe of?

Added subscriber: @mod_moder

Added subscriber: @mod_moder

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

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

However, this problem can still be found by someone. It's best to wait for the developer's response.

However, this problem can still be found by someone. It's best to wait for the developer's response.
Member

Hi, I agree with @lichtwerk ’s conclusions, the break should have been documented and I apologize for not doing it. Can the release notes still be edited after the fact?

To me, translating the node names is the correct behaviour, and as noted by @SatoshiYamasaki it can be disabled in the preferences—although the option is not so obvious.
The behaviour is still causing bugs for add-ons using hardcoded names though, and it’s a problem because few devs use a translated UI so they’re not going to catch the bug. I’d say that the best course of action would be to report it directly to the developers of Dream Textures and any other add-on affected, and suggest using the fix provided by Philipp (bpy.app.translations.pgettext_iface("Principled BSDF")). I’ll try to determine if any built-in add-ons are having issues.

@SatoshiYamasaki You mentioned several add-ons were affected besides Dream Textures, do you know which ones?

Hi, I agree with @lichtwerk ’s conclusions, the break should have been documented and I apologize for not doing it. Can the release notes still be edited after the fact? To me, translating the node names is the correct behaviour, and as noted by @SatoshiYamasaki it can be disabled in the preferences—although the option is not so obvious. The behaviour is still causing bugs for add-ons using hardcoded names though, and it’s a problem because few devs use a translated UI so they’re not going to catch the bug. I’d say that the best course of action would be to report it directly to the developers of Dream Textures and any other add-on affected, and suggest using the fix provided by Philipp (`bpy.app.translations.pgettext_iface("Principled BSDF")`). I’ll try to determine if any built-in add-ons are having issues. @SatoshiYamasaki You mentioned several add-ons were affected besides Dream Textures, do you know which ones?
Contributor

You can can find some by grepping for Material Output and Principled BSDF in the addons folder. io_mesh_atomic for instance has a bunch (example).

You can can find some by grepping for `Material Output` and `Principled BSDF` in the addons folder. io_mesh_atomic for instance has a bunch ([example](https://github.com/blender/blender-addons/blob/9958ddb879934718cc2b379b556f0bc3b861bee5/io_mesh_atomic/pdb_import.py#L756-L759)).
Author
Member

@pioverfour: Thank you and sorry, I don't know any addon affected this change except Dream Texture. it's just my fault.

I agree that this is correct behavior too. I remember Add Camera Rig and some addons (sorry, I forget them) had similar problem for bone constraints or modifiers which added by scripts, but it works fine now. so I believe Dream Texture (and other addons I don't know) will be fixed too.

@scurest : Thank you for the supplement comment again. :)

@pioverfour: Thank you and sorry, I don't know any addon affected this change except Dream Texture. it's just my fault. I agree that this is correct behavior too. I remember Add Camera Rig and some addons (sorry, I forget them) had similar problem for bone constraints or modifiers which added by scripts, but it works fine now. so I believe Dream Texture (and other addons I don't know) will be fixed too. @scurest : Thank you for the supplement comment again. :)
Member

Thanks @scurest, I’m in the process of fixing the issue.

On second thought I’ll not be using the suggested fix though, as I’d rather get the nodes from their types than their translated names. I’m going with:

principled_node = next(n for n in material.node_tree.nodes if n.type == "BSDF_PRINCIPLED")

For the record, another possibility is:

principled_node, output_node = material.node_tree.nodes

or:

principled_node = material.node_tree.nodes[0]
output_node = material.node_tree.nodes[1]

but I’m not sure the order is expected to be stable so I’ll avoid it as well!

Thanks @scurest, I’m in the process of fixing the issue. On second thought I’ll not be using the suggested fix though, as I’d rather get the nodes from their types than their translated names. I’m going with: ``` principled_node = next(n for n in material.node_tree.nodes if n.type == "BSDF_PRINCIPLED") ``` For the record, another possibility is: ``` principled_node, output_node = material.node_tree.nodes ``` or: ``` principled_node = material.node_tree.nodes[0] output_node = material.node_tree.nodes[1] ``` but I’m not sure the order is expected to be stable so I’ll avoid it as well!

The bug is indeed in the add-ons, not in 9d732445b9. Code should never, ever rely on a user-editable name to access data, unless it is explicitly given by the user, or set by that same code and checked for the actual final name as defined by Blender (since there can also be numeric suffixes etc.).

Accessing nodes by type or indices is also weak, but at least relatively consistent.

The bug is indeed in the add-ons, not in 9d732445b9. Code should never, ever rely on a user-editable name to access data, unless it is explicitly given by the user, or set by that same code and checked for the actual final name as defined by Blender (since there can also be numeric suffixes etc.). Accessing nodes by type or indices is also weak, but at least relatively consistent.
Bastien Montagne added this to the User Interface project 2023-02-09 15:34:10 +01:00
Member

Closing this issue after add-ons were fixed in #104402 and #104403.

Closing this issue after add-ons were fixed in #104402 and #104403.
Blender Bot added
Status
Archived
and removed
Status
Needs Info from Developers
labels 2023-02-11 23:47:38 +01:00
Contributor

Were the release notes updated like @lichtwerk mentioned? I didn't see it.

Were the release notes updated like @lichtwerk mentioned? I didn't see it.
Member

They are now, thanks for reminding me.

They are now, thanks for reminding me.
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
7 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#104145
No description provided.