Shape keys missing from bpy.data.meshes.new_from_object() even when the mesh Object is not evaluated #109313

Open
opened 2023-06-23 23:24:28 +02:00 by Thomas Barlow · 3 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 531.41

Blender Version
Broken: version: 4.0.0 Alpha, branch: main, commit date: 2023-06-19 17:26, hash: cedec09d0594
Worked: 2.79 (sort of, the API was a bit different due to the depsgraph not existing)

Short description of error
Creating a new Mesh from a mesh Object with bpy.data.meshes.new_from_object removes the shape keys (or simply does not copy the them in the first place), even when the Object is not evaluated.

The API in 2.79 is a little different to 2.80+, since there is no depsgraph; you choose with the function arguments whether or not to apply modifiers:
When using apply_modifiers=True, the new mesh has no shape keys as expected. Providing an evaluated Object to the function in 2.80+ is equivalent to this.
When using apply_modifiers=False, the new mesh retains its shape keys as expected. In my opinion, providing a non-evaluated Object to the function in 2.80+ should be equivalent to this.

2.79 API docs: https://docs.blender.org/api/2.79/bpy.types.BlendDataMeshes.html#bpy.types.BlendDataMeshes.new_from_object
Current API docs: https://docs.blender.org/api/current/bpy.types.BlendDataMeshes.html#bpy.types.BlendDataMeshes.new_from_object

Since the API changed between 2.79 and 2.80, it's not clear whether this is a bug or intended behaviour.
Object.to_mesh() and BlendDataMeshes.new_from_object() appear to have identical behaviour in 2.79, though this is again a bit different in 2.80+ because Object.to_mesh() now creates a temporary mesh owned by the Object that is not added to bpy.data.meshes.

Exact steps for others to reproduce the error

  1. Create a mesh Object
  2. Add a shape key (other than the 'Basis')
  3. Enter Edit Mode
  4. Modify the shape key so that it noticeably moves the mesh
  5. Exit Edit Mode
  6. Change the value of the shape key to 1.0
  7. Run in the Python Console:
    1. new_mesh = D.meshes.new_from_object(C.object, preserve_all_data_layers=True, depsgraph=C.evaluated_depsgraph_get())
    2. C.object.data = new_mesh
  8. Observe that the new mesh has no shape keys (and is not evaluated as expected)

Attached are some .blend files created in different Blender versions that have a script and three Objects that go over different cases (has modifiers/no modifiers/has modifiers but they're disabled) of creating meshes with both BlendDataMeshes.new_from_object and Object.to_mesh as well as making a copy of an evaluated mesh with Object.copy in 2.80+.

The 2.79 version is a bit different due to the different API, but the 2.80+ version in 4.0.0a results in this:
(the .to_mesh().copy() behaviour of evaluated Objects has been reported separately as #109327)
image

**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 531.41 **Blender Version** Broken: version: 4.0.0 Alpha, branch: main, commit date: 2023-06-19 17:26, hash: `cedec09d0594` Worked: 2.79 (sort of, the API was a bit different due to the depsgraph not existing) **Short description of error** Creating a new Mesh from a mesh Object with `bpy.data.meshes.new_from_object` removes the shape keys (or simply does not copy the them in the first place), even when the Object is not evaluated. The API in 2.79 is a little different to 2.80+, since there is no depsgraph; you choose with the function arguments whether or not to apply modifiers: When using `apply_modifiers=True`, the new mesh has no shape keys as expected. Providing an evaluated Object to the function in 2.80+ is equivalent to this. When using `apply_modifiers=False`, the new mesh retains its shape keys as expected. In my opinion, providing a non-evaluated Object to the function in 2.80+ should be equivalent to this. 2.79 API docs: https://docs.blender.org/api/2.79/bpy.types.BlendDataMeshes.html#bpy.types.BlendDataMeshes.new_from_object Current API docs: https://docs.blender.org/api/current/bpy.types.BlendDataMeshes.html#bpy.types.BlendDataMeshes.new_from_object Since the API changed between 2.79 and 2.80, it's not clear whether this is a bug or intended behaviour. `Object.to_mesh()` and `BlendDataMeshes.new_from_object()` appear to have identical behaviour in 2.79, though this is again a bit different in 2.80+ because `Object.to_mesh()` now creates a temporary mesh owned by the Object that is not added to `bpy.data.meshes`. **Exact steps for others to reproduce the error** 1. Create a mesh Object 1. Add a shape key (other than the 'Basis') 1. Enter Edit Mode 1. Modify the shape key so that it noticeably moves the mesh 1. Exit Edit Mode 1. Change the value of the shape key to 1.0 1. Run in the Python Console: 1. `new_mesh = D.meshes.new_from_object(C.object, preserve_all_data_layers=True, depsgraph=C.evaluated_depsgraph_get())` 1. `C.object.data = new_mesh` 1. Observe that the new mesh has no shape keys (and is not evaluated as expected) Attached are some .blend files created in different Blender versions that have a script and three Objects that go over different cases (has modifiers/no modifiers/has modifiers but they're disabled) of creating meshes with both `BlendDataMeshes.new_from_object` and `Object.to_mesh` as well as making a copy of an evaluated mesh with `Object.copy` in 2.80+. The 2.79 version is a bit different due to the different API, but the 2.80+ version in 4.0.0a results in this: (the .to_mesh().copy() behaviour of evaluated Objects has been reported separately as https://projects.blender.org/blender/blender/issues/109327) ![image](/attachments/ed5d740a-37b9-48b9-9881-87c64774f8ac)
Thomas Barlow added the
Type
Report
Status
Needs Triage
Priority
Normal
labels 2023-06-23 23:24:29 +02:00
Member

Can confirm the behaviour.

From what I can see, new_from_object and to_mesh().copy() both respect the evaluation status if they do things this way:

  • new_from_object gives "original mesh without anything" when called on unevaluated object, and gives "completely applied result" when called on evaluated objects.
  • to_mesh() applies modifier if there is any, but when applying modifier it will have to apply shape keys (? because it's not applying modifiers to every single shape keys since that would lead to topology changes?)

So I think the problem is that if we want to make things consistent, we need to make sure that:

  • new_from_object to give mesh with shape keys
  • to_mesh() to always apply shape keys (?)
Can confirm the behaviour. From what I can see, `new_from_object` and `to_mesh().copy()` both respect the evaluation status if they do things this way: - `new_from_object` gives "original mesh without anything" when called on unevaluated object, and gives "completely applied result" when called on evaluated objects. - `to_mesh()` applies modifier if there is any, but when applying modifier it will have to apply shape keys (? because it's not applying modifiers to every single shape keys since that would lead to topology changes?) So I think the problem is that if we want to make things consistent, we need to make sure that: - `new_from_object` to give mesh _with_ shape keys - `to_mesh()` to always apply shape keys (?)
YimingWu added
Module
Python API
Status
Confirmed
Interest
Modeling
and removed
Status
Needs Triage
labels 2023-06-24 04:46:42 +02:00
  • to_mesh() applies modifier if there is any, but when applying modifier it will have to apply shape keys (? because it's not applying modifiers to every single shape keys since that would lead to topology changes?)

Applying modifiers also implies applying the current mix of shapekeys, which is internally a virtual modifier placed at the very top of the stack. So getting evaluated object/obdata (aka 'applying modifiers') will also always removes/applies shapekeys. So think to_mesh() behaves as expected.

I think new_from_object should indeed keep the shapekey info when called on original data. But that may have unexpected consequences in other parts of the code...

> - `to_mesh()` applies modifier if there is any, but when applying modifier it will have to apply shape keys (? because it's not applying modifiers to every single shape keys since that would lead to topology changes?) Applying modifiers also implies applying the current mix of shapekeys, which is internally a virtual modifier placed at the very top of the stack. So getting evaluated object/obdata (aka 'applying modifiers') will also always removes/applies shapekeys. So think `to_mesh()` behaves as expected. I think `new_from_object` should indeed keep the shapekey info when called on original data. But that may have unexpected consequences in other parts of the code...
Member

So think to_mesh() behaves as expected.

For mesh without modifiers and with shape keys, to_mesh() will keep shape keys. I believe this is where the confusion arises, since applying shape keys is needed for applying modifiers. I suggest to maybe:

  1. make it consistently apply shape keys or
  2. to have an additional argument for the function to keep/apply shape keys when there's no modifiers on the mesh.
> So think `to_mesh()` behaves as expected. For mesh without modifiers and with shape keys, `to_mesh()` will keep shape keys. I believe this is where the confusion arises, since applying shape keys is needed for applying modifiers. I suggest to maybe: 1. make it consistently apply shape keys or 2. to have an additional argument for the function to keep/apply shape keys when there's no modifiers on the mesh.
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#109313
No description provided.