Accessing the same color attribute multiple times return unpredictable results #117488

Open
opened 2024-01-24 21:40:02 +01:00 by Todd J. York · 5 comments

System Information
Operating system: Windows 11
Graphics card: RTX 3060

Blender Version
Broken: 4.0.2, branch: blender-v4.0-release, commit date: 2023-12-05 07:41, hash: 9be62e85b7, type: release

Short description of error
I think bpy.types.Mesh.color_attribute is somehow bugged?

I was working on an operator for converting color attributes to vertex groups (generated by another operator from vertex groups), and had to use hacks for this to work.

Exact steps for others to reproduce the error
Run the script provided in the attached "test.blend" multiple times (undoing after each run) to see how the result is unredictable.
Changing line 5 to False will yield consistent behavior.

Other than the effects demonstrated by this file, I was experiencing the following when running the actual script (see links above) on my actual model:

  • crash
  • internal error (sorry, forgot the description, followed by a crash upon another try so now I can't investigate the console history)
  • random deletion of unrelated color attributes upon calling bpy.types.Mesh.color_attributes.remove()
  • len(attribute.data) returns the correct length, but accessing attribute.data[0] on the next line throws an "index out of range" exception
**System Information** Operating system: Windows 11 Graphics card: RTX 3060 **Blender Version** Broken: 4.0.2, branch: blender-v4.0-release, commit date: 2023-12-05 07:41, hash: 9be62e85b727, type: release **Short description of error** I think `bpy.types.Mesh.color_attribute` is somehow bugged? I was working on [an operator](https://github.com/TJYSunset/WTFB/blob/88ca5fe9e31697db287391e46a67f44ece996c99/color_attributes_to_vertex_groups.py) for converting color attributes to vertex groups (generated by [another operator](https://github.com/TJYSunset/WTFB/blob/88ca5fe9e31697db287391e46a67f44ece996c99/vertex_groups_to_color_attributes.py) from vertex groups), and [had to use hacks](https://github.com/TJYSunset/WTFB/commit/88ca5fe9e31697db287391e46a67f44ece996c99) for this to work. **Exact steps for others to reproduce the error** Run the script provided in the attached "test.blend" multiple times (undoing after each run) to see how the result is unredictable. Changing line 5 to `False` will yield consistent behavior. Other than the effects demonstrated by this file, I was experiencing the following when running the actual script (see links above) on my actual model: + crash + internal error (sorry, forgot the description, followed by a crash upon another try so now I can't investigate the console history) + random deletion of unrelated color attributes upon calling `bpy.types.Mesh.color_attributes.remove()` + `len(attribute.data)` returns the correct length, but accessing `attribute.data[0]` on the next line throws an "index out of range" exception
949 KiB
Todd J. York added the
Status
Needs Triage
Priority
Normal
Type
Report
labels 2024-01-24 21:40:03 +01:00

Stack trace is attached.

Stack trace is attached.
Iliya Katushenock added
Module
Modeling
Status
Confirmed
Interest
Python API
and removed
Status
Needs Triage
labels 2024-01-24 21:59:30 +01:00
Contributor

IIUC, when you add or remove a color attribute, generally any existing references to color attributes may become junk (pointer is invalidated). Your workaround avoids this problem by holding onto names instead of direct references to color attributes.

See https://devtalk.blender.org/t/python-api-access-of-customdatalayer-cant-handle-inserted-layers/29310 and #107500 for a recent discussion.

IIUC, when you add or remove a color attribute, generally any existing references to color attributes may become junk (pointer is invalidated). Your workaround avoids this problem by holding onto names instead of direct references to color attributes. See https://devtalk.blender.org/t/python-api-access-of-customdatalayer-cant-handle-inserted-layers/29310 and #107500 for a recent discussion.
Author

If a fix is nowhere near, I think this should warned in bold text in the API docs. The issue tracker and devtalk won't pop up anywhere in Google searches already bloated by YouTube garbage and unrelated beginner questions, let alone a convoluted bug like this.

If a fix is nowhere near, I think this should warned in bold text in the API docs. The issue tracker and devtalk won't pop up anywhere in Google searches already bloated by YouTube garbage and unrelated beginner questions, let alone a convoluted bug like this.
Contributor

There's a section about it on the Gotchas page

Help! My script crashes Blender

TL;DR Do not keep direct references to Blender data (of any kind) when modifying the container of that data, and/or when some undo/redo may happen (e.g. during modal operators execution…). Instead, use indices (or other data always stored by value in Python, like string keys…), that allow you to get access to the desired data.

Mesh attributes aren't mentioned explicitly but the case is similar to the example in Array Re-Allocation.

There's [a section about it on the Gotchas page](https://docs.blender.org/api/current/info_gotcha.html#help-my-script-crashes-blender) > Help! My script crashes Blender > > TL;DR Do not keep direct references to Blender data (of any kind) when modifying the container of that data, and/or when some undo/redo may happen (e.g. during modal operators execution…). Instead, use indices (or other data always stored by value in Python, like string keys…), that allow you to get access to the desired data. Mesh attributes aren't mentioned explicitly but the case is similar to the example in [Array Re-Allocation](https://docs.blender.org/api/current/info_gotcha.html#array-re-allocation).
Author

Pretty sure the possibilities of undefined behavior isn't only limited to crashing (as seen here), and having this section titled "Help! My script crashes Blender" means any one without the ability to skim English (take me for example) will brush it off as unrelated in a heartbeat. Also, not listing the other undefined behaviors means it won't show up in search engines at all.

The discoverability of the Gotchas page is also quite bad - I only knew the docs had an index.html because I searched for an offline version. Shouldn't the links to these pages live in the sidebar?

Pretty sure the possibilities of undefined behavior isn't only limited to crashing (as seen here), and having this section titled "Help! My script crashes Blender" means any one without the ability to skim English (take me for example) will brush it off as unrelated in a heartbeat. Also, not listing the other undefined behaviors means it won't show up in search engines at all. The discoverability of the Gotchas page is also quite bad - I only knew the docs had an index.html because I searched for an offline version. Shouldn't the links to these pages live in the sidebar?
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
3 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#117488
No description provided.