Accessing MeshVertex.normal values is slow after setting MeshVertex.co #105909

Closed
opened 2023-03-20 03:41:28 +01:00 by Thomas Barlow · 5 comments
Member

System Information
Operating system: Windows-10-10.0.19045-SP0 64 Bits
Graphics card: NVIDIA GeForce GTX 1070 Ti/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 516.94

Blender Version
Broken: version: 3.3.0, branch: master, commit date: 2022-09-06 15:39, hash: rB0759f671ce1f (also tested and broken in 3.3.4, 3.4, 3.5 and 3.6)
Worked: 3.2.2

Short description of error
Accessing MeshVertex.normal (specifically the values of the normal attribute must be accessed, there's no issue with assigning .normal to a variable) after setting MeshVertex.co is very slow.

It seems to get slower as the number of vertices in the mesh increases.

Note that this slowdown does not occur when accessing the normals through Mesh.vertex_normals or setting vertex position through the "position" attribute.

Exact steps for others to reproduce the error
Run the scripts and look in the system console to observe the drop in performance in 3.3.0 and after.

Attached is a .blend (authored in 3.2.2) that contains a cube subdivided to 24578 vertices along with the scripts plus a more in-depth script that also tests Mesh.vertex_normals and the "position" attribute in newer Blender versions.

Example use case script:

import bpy
offset = 0.01
for v in bpy.context.object.data.vertices:
    v.co += v.normal * offset

Timing test script:
(I get around 1.7μs in 3.2.2 and 300μs in 3.3.0 and newer)

import bpy
import timeit

obj = bpy.context.object
me = obj.data

print()
print()
print(f"{len(me.vertices)} vertices:")

vertex0 = me.vertices[0]

trials = 1000

# Expected to be around 1.7μs with 24578 vertices
t_slow = timeit.timeit("vertex0.co = vertex0.co;vertex0.normal[:]", globals=globals(), number=trials)/trials * 1e6
print(f"Set MeshVertex.co then get MeshVertex.normal values:\n\t{t_slow:f}μs")

# Expected to be around 7e-05μs per vertex with 24578 vertices
print(f"slow/number of vertices: {t_slow/len(me.vertices):.2e}μs")
**System Information** Operating system: Windows-10-10.0.19045-SP0 64 Bits Graphics card: NVIDIA GeForce GTX 1070 Ti/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 516.94 **Blender Version** Broken: version: 3.3.0, branch: master, commit date: 2022-09-06 15:39, hash: `rB0759f671ce1f` (also tested and broken in 3.3.4, 3.4, 3.5 and 3.6) Worked: 3.2.2 **Short description of error** Accessing `MeshVertex.normal` (specifically the values of the `normal` attribute must be accessed, there's no issue with assigning `.normal` to a variable) after setting `MeshVertex.co` is very slow. It seems to get slower as the number of vertices in the mesh increases. Note that this slowdown does not occur when accessing the normals through `Mesh.vertex_normals` or setting vertex position through the "position" attribute. **Exact steps for others to reproduce the error** Run the scripts and look in the system console to observe the drop in performance in 3.3.0 and after. Attached is a .blend (authored in 3.2.2) that contains a cube subdivided to 24578 vertices along with the scripts plus a more in-depth script that also tests `Mesh.vertex_normals` and the "position" attribute in newer Blender versions. Example use case script: ```py import bpy offset = 0.01 for v in bpy.context.object.data.vertices: v.co += v.normal * offset ``` Timing test script: (I get around 1.7μs in 3.2.2 and 300μs in 3.3.0 and newer) ```py import bpy import timeit obj = bpy.context.object me = obj.data print() print() print(f"{len(me.vertices)} vertices:") vertex0 = me.vertices[0] trials = 1000 # Expected to be around 1.7μs with 24578 vertices t_slow = timeit.timeit("vertex0.co = vertex0.co;vertex0.normal[:]", globals=globals(), number=trials)/trials * 1e6 print(f"Set MeshVertex.co then get MeshVertex.normal values:\n\t{t_slow:f}μs") # Expected to be around 7e-05μs per vertex with 24578 vertices print(f"slow/number of vertices: {t_slow/len(me.vertices):.2e}μs") ```
Thomas Barlow added the
Priority
Normal
Status
Needs Triage
Type
Report
labels 2023-03-20 03:41:28 +01:00

Note that the Normal of all vertices needs to be recalculated every time you change the position of the vertices.

If it were not recalculated, the values would be outdated and wrong.

That is why it is slow in this case.

If the intention is to reuse the non-recalculated normals, maybe you should copy the array of normals or change the position of the vertices in a way to skip the tag that indicates that the normals need to be recalculated.

So this slowdown is expected and not considered a bug.

Note that the `Normal` of all vertices needs to be recalculated every time you change the position of the vertices. If it were not recalculated, the values would be outdated and wrong. That is why it is slow in this case. If the intention is to reuse the non-recalculated normals, maybe you should copy the array of normals or change the position of the vertices in a way to skip the tag that indicates that the normals need to be recalculated. So this slowdown is expected and not considered a bug.
Blender Bot added
Status
Archived
and removed
Status
Needs Triage
labels 2023-03-20 15:31:40 +01:00
Author
Member

Note that the Normal of all vertices needs to be recalculated every time you change the position of the vertices.

If it were not recalculated, the values would be outdated and wrong.

That is why it is slow in this case.

Then shouldn't these cases also be slow? They all run without significant slowdown.

import bpy
offset = 0.01
me = bpy.context.object.data
for v, n in zip(me.vertices, me.vertex_normals):
    v.co += n.vector * offset
import bpy
offset = 0.01
me = bpy.context.object.data
for p, v in zip(me.attributes["position"].data, me.vertices):
    p.vector += v.normal * offset
import bpy
offset = 0.01
me = bpy.context.object.data
for p, n in zip(me.attributes["position"].data, me.vertex_normals):
    p.vector += n.vector * offset
> Note that the `Normal` of all vertices needs to be recalculated every time you change the position of the vertices. > > If it were not recalculated, the values would be outdated and wrong. > > That is why it is slow in this case. Then shouldn't these cases also be slow? They all run without significant slowdown. ``` import bpy offset = 0.01 me = bpy.context.object.data for v, n in zip(me.vertices, me.vertex_normals): v.co += n.vector * offset ``` ``` import bpy offset = 0.01 me = bpy.context.object.data for p, v in zip(me.attributes["position"].data, me.vertices): p.vector += v.normal * offset ``` ``` import bpy offset = 0.01 me = bpy.context.object.data for p, n in zip(me.attributes["position"].data, me.vertex_normals): p.vector += n.vector * offset ```

If you prefer, you can check when the functions BKE_mesh_tag_positions_changedand BKE_mesh_vert_normals_ensure are called. (I'm not sure off the top of my head if these cases can create some kind of copy)

If you prefer, you can check when the functions `BKE_mesh_tag_positions_changed`and `BKE_mesh_vert_normals_ensure` are called. (I'm not sure off the top of my head if these cases can create some kind of copy)
Author
Member

Ok, yes, only the example in the issue description is recalculating normals while iterating.

image
image

In 3.2, the normals were not recalculated while iterating with the example in the issue description and I couldn't see anything relating to this change in the 3.3 release notes.

image

Ok, yes, only the example in the issue description is recalculating normals while iterating. ![image](/attachments/5440c31a-8722-4579-a311-f6e6393679ae) ![image](/attachments/3757c59b-a71c-4c33-9942-c0e0cdd9fdd0) In 3.2, the normals were not recalculated while iterating with the example in the issue description and I couldn't see anything relating to this change in the 3.3 release notes. ![image](/attachments/ad1b9adf-f0a5-4ff6-bad7-8d9588056926)

The way vertex normals are stored changed a while ago. See d9c6ceb3b88b6db87490b08e0089f9a18e6c52d6

Perhaps some related things had to change as well.

Or this could have been a bug fix.

The way vertex normals are stored changed a while ago. See d9c6ceb3b88b6db87490b08e0089f9a18e6c52d6 Perhaps some related things had to change as well. Or this could have been a bug fix.
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
2 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#105909
No description provided.