Error on assigning different amount of verts to MeshPolygon.vertices in Blender 3.5 #106384

Closed
opened 2023-03-31 14:33:46 +02:00 by Andrej · 4 comments
Contributor

Blender 3.5

Short description of error
Found that in 3.5 the code below that's trying to assign different number of verts to polygon than there was before results in error. Haven't found anything related in https://wiki.blender.org/wiki/Reference/Release_Notes/3.5/Python_API.

It's not that big of a deal, I can rework original code with bmesh. Just wanted to put it out there - maybe it's part of some different issue.

Exact steps for others to reproduce the error

Select cube and run the code below in default scene. You can test the same code in 3.4 - it doesn't result in error.

obj = bpy.context.object
obj.data.polygons.add(1)
obj.data.polygons[-1].vertices = (0,1,2)

# ValueError: bpy_struct: item.attr = val: MeshPolygon.vertices: array length cannot be changed to 3 (expected 0)
Blender 3.5 **Short description of error** Found that in 3.5 the code below that's trying to assign different number of verts to polygon than there was before results in error. Haven't found anything related in https://wiki.blender.org/wiki/Reference/Release_Notes/3.5/Python_API. It's not that big of a deal, I can rework original code with bmesh. Just wanted to put it out there - maybe it's part of some different issue. **Exact steps for others to reproduce the error** Select cube and run the code below in default scene. You can test the same code in 3.4 - it doesn't result in error. ```python obj = bpy.context.object obj.data.polygons.add(1) obj.data.polygons[-1].vertices = (0,1,2) # ValueError: bpy_struct: item.attr = val: MeshPolygon.vertices: array length cannot be changed to 3 (expected 0) ```
Andrej added the
Severity
Normal
Status
Needs Triage
Type
Report
labels 2023-03-31 14:33:47 +02:00
Andrej changed title from Cannot assign to Cannot assign different amount of verts to MeshPolygon.vertices in Blender 3.5 2023-03-31 14:49:04 +02:00
Andrej changed title from Cannot assign different amount of verts to MeshPolygon.vertices in Blender 3.5 to Error on assigning different amount of verts to MeshPolygon.vertices in Blender 3.5 2023-03-31 14:49:23 +02:00
Member

If you add

print(len(obj.data.polygons[-1].vertices))
print(obj.data.polygons[-1].vertices[:])

to the end of the above code in 3.4, you should be able to see that the assignment to vertices doesn't do anything; vertices remains zero length.

It looks like 51e5417bd3 in 3.5 also has the effect of raising an error when the array cannot be resized by assignment and the array is smaller than the sequence it's being set to.


To set vertices like this, the loop_total of the polygon can be set to the number of vertices beforehand so that vertices changes to the desired length.

This should work in both 3.4 and 3.5, though for practical use you may want to add new loops and set the loop_start of the polygon too so you're not overwriting the vertex_index of any existing loops or overwriting unallocated memory when the loops don't exist (there don't appear to be any safeguards).

obj = bpy.context.object
obj.data.polygons.add(1)
obj.data.polygons[-1].loop_total = 3
obj.data.polygons[-1].vertices = (1,2,3)
print(len(obj.data.polygons[-1].vertices))
print(obj.data.polygons[-1].vertices[:])
If you add ```py print(len(obj.data.polygons[-1].vertices)) print(obj.data.polygons[-1].vertices[:]) ``` to the end of the above code in 3.4, you should be able to see that the assignment to `vertices` doesn't do anything; `vertices` remains zero length. It looks like https://projects.blender.org/blender/blender/commit/51e5417bd37eecbf6db2f2f5e37e5155db039996 in 3.5 also has the effect of raising an error when the array cannot be resized by assignment and the array is smaller than the sequence it's being set to. ----- To set `vertices` like this, the `loop_total` of the polygon can be set to the number of vertices beforehand so that `vertices` changes to the desired length. This should work in both 3.4 and 3.5, though for practical use you may want to add new loops and set the `loop_start` of the polygon too so you're not overwriting the `vertex_index` of any existing loops or overwriting unallocated memory when the loops don't exist (there don't appear to be any safeguards). ```py obj = bpy.context.object obj.data.polygons.add(1) obj.data.polygons[-1].loop_total = 3 obj.data.polygons[-1].vertices = (1,2,3) print(len(obj.data.polygons[-1].vertices)) print(obj.data.polygons[-1].vertices[:]) ```
Author
Contributor

That's true. In the code I was using orginally assigning vertices with obj.data.polygons[-1].vertices = [0,1,2] prior to declaring obj.data.polygons[-1].loop_total basically had no effect and it was just a happy accident that it produced correct geometry.
Checked that it worked exactly the same way even with obj.data.polygons[-1].vertices = [0,1,2] commented out.

That's true. In the code I was using orginally assigning vertices with `obj.data.polygons[-1].vertices = [0,1,2]` prior to declaring `obj.data.polygons[-1].loop_total` basically had no effect and it was just a happy accident that it produced correct geometry. Checked that it worked exactly the same way even with `obj.data.polygons[-1].vertices = [0,1,2]` commented out.

I am not very familiar with workflow in this case, does this mean that there is no issue after all?

I am not very familiar with workflow in this case, does this mean that there is no issue after all?
Author
Contributor

Yeah, basically it wasn't working before and now there is error message for it.
I don't think there is an issue actually.

Yeah, basically it wasn't working before and now there is error message for it. I don't think there is an issue actually.
Blender Bot added
Status
Archived
and removed
Status
Needs Triage
labels 2023-04-12 07:22:16 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
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 & 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
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
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
Asset System
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
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#106384
No description provided.