Modifying Basis shape key .co via script in Object mode desyncs from mesh vertices causing new from_mix=False shape keys to revert script changes #98890

Open
opened 2022-06-14 18:26:16 +02:00 by Mysteryem · 9 comments

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

Blender Version
Broken: 2.79.0, 2.80.75, 2.93.7, 3.0.0, 3.2.0, 3.3.0 Alpha (branch: master, commit date: 2022-06-07 18:08, hash: 173a15bcda) (I have not tested any versions older than 2.79.0)
Worked:

Short description of error
Modifying the Basis shape key of a mesh via script in Object mode does not update the mesh vertices to match the changes to the Basis shape key.
When creating a new shape key with from_mix=False, the newly created shape key matches the positions of the mesh vertices instead of the Basis shape key, making the new shape key effectively undo the modifications via script.
This is confusing because there is no way to tell through the UI that the mesh vertices and Basis shape key have become desynchronised due to a script.
Fixing the desynchronised state is at least fairly simple, going into Edit mode and back out to Object mode will cause the mesh vertices to update to match the basis shape key, fixing the desync (of course, the script could also be modified to propagate the changes to the mesh vertices too).
I'm unsure if the issue here should be considered the desync between basis shape key and mesh vertices, the behaviour of creating new shape keys with from_mix=False or a fault with the script for not also updating the mesh vertices, causing the desync in the first place.

Exact steps for others to reproduce the error

  • Add a shape key to the default Cube so that the Cube now has a Basis shape key
  • Modify any of the .co of the Basis shape key's .data while remaining in Object mode by using a script or the Python console, e.g. bpy.context.object.data.shape_keys.reference_key.data- [x].co.x += 1
  • Add a new shape key to the default Cube with from_mix=False (either the UI, the operator used by the UI or the shape_key_add method of the Cube Object)
  • The newly created shape key will match the original cube shape instead of the basis shape key shape, so when activated (value -> 1.0) it will revert the mesh's shape back to the original cube shape
    Alternative steps to reproduce only the desync without creating a new shape key with from_mix=False:
  • Add a shape key to the default Cube so that the Cube now has a Basis shape key
  • Modify any of the .co of the Basis shape key's .data while remaining in Object mode by using a script or the Python console, e.g. bpy.context.object.data.shape_keys.reference_key.data- [x].co.x += 1
  • Remove the basis shape key from the Cube such that it no longer has any shape keys, the mesh will revert back to the original cube shape when doing so
    • If you were to instead go into Edit mode and back out to Object mode before removing the Basis shape key (thus fixing the desync), the Cube will remain in the shape of the Basis shape key when the Basis shape key is removed.

Modifying the Basis shape's .co via foreach_set('co', my_co_array) has the same issues, but note that it does not update the visuals of the cube in the 3D view immediately so it can appear as if it has done nothing until something else causes the visuals to update e.g.

me.shape_keys.reference_key.data.foreach_set('co', [0,] * 3 * len(me.vertices))```

In both of these sets of steps, you can instead modify the .co of the mesh vertices to achieve the same issue, but with a reversed effect.

----
If you select the default Cube as the active object, the following script will run through the first set of reproduction steps, creating shape keys with both `from_mix=True` and `from_mix=False` and printing whether they match the basis and if not, printing whether they instead match the mesh vertices:
```import bpy

def cos_equal(shape_data_or_vertices1, shape_data_or_vertices2):

for data1, data2 in zip(shape_data_or_vertices1, shape_data_or_vertices2):
if data1.co != data2.co:
return False
return True


cube = bpy.context.object
me = cube.data

# Remove all shape keys if the script has been run previously
if me.shape_keys:

bpy.ops.object.shape_key_remove(all=True)


print("\nTesting shape keys in {}:".format(bpy.app.version))

basis_shape = cube.shape_key_add(name='Basis')

assert me.shape_keys.reference_key == basis_shape

# Newly created Basis shape key should match the mesh vertices
for v, b in zip(me.vertices, basis_shape.data):

assert v.co == b.co


# Modify the basis shape key via script in some way
basis_shape.data- [x].co.x += 1
print("Modified basis shape key via script")

if cos_equal(me.vertices, basis_shape.data):

print("Basis shape key matches mesh vertices")

else:

print("Basis shape key does not match mesh vertices, there is desync!")


# Create new shape key, from mix, using the shape_key_add funtion
new_shape_from_mix = cube.shape_key_add(name='from_mix=True', from_mix=True)

assert new_shape_from_mix.relative_key == basis_shape

if cos_equal(basis_shape.data, new_shape_from_mix.data):

print("from_mix=True matches basis shape key")

else:

print("from_mix=True does not match basis shape key!")

    # Compare against the mesh vertices instead

if cos_equal(me.vertices, new_shape_from_mix.data):
print(" from_mix=True matches mesh vertices")
else:
print(" from_mix=True does not match mesh vertices")


# New shape key, not from mix, created using the shape_key_add funtion
new_shape = cube.shape_key_add(name='from_mix=False', from_mix=False)

assert new_shape.relative_key == basis_shape

if cos_equal(basis_shape.data, new_shape.data):

print("from_mix=False matches basis shape key")

else:

print("from_mix=False does not match basis shape key!")

    # Compare against the mesh vertices instead

if cos_equal(me.vertices, new_shape.data):
print(" from_mix=False matches mesh vertices")
else:
print(" from_mix=False does not match mesh vertices")

Example script output in the System console:

Modified basis shape key via script
Basis shape key does not match mesh vertices, there is desync!
from_mix=True matches basis shape key
from_mix=False does not match basis shape key!

from_mix=False matches mesh vertices```

**System Information** Operating system: Windows-10-10.0.19043-SP0 64 Bits Graphics card: NVIDIA GeForce GTX 1070 Ti/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 471.11 **Blender Version** Broken: 2.79.0, 2.80.75, 2.93.7, 3.0.0, 3.2.0, 3.3.0 Alpha (branch: master, commit date: 2022-06-07 18:08, hash: 173a15bcda) (I have not tested any versions older than 2.79.0) Worked: **Short description of error** Modifying the Basis shape key of a mesh via script in Object mode does not update the mesh vertices to match the changes to the Basis shape key. When creating a new shape key with `from_mix=False`, the newly created shape key matches the positions of the mesh vertices instead of the Basis shape key, making the new shape key effectively undo the modifications via script. This is confusing because there is no way to tell through the UI that the mesh vertices and Basis shape key have become desynchronised due to a script. Fixing the desynchronised state is at least fairly simple, going into Edit mode and back out to Object mode will cause the mesh vertices to update to match the basis shape key, fixing the desync (of course, the script could also be modified to propagate the changes to the mesh vertices too). I'm unsure if the issue here should be considered the desync between basis shape key and mesh vertices, the behaviour of creating new shape keys with `from_mix=False` or a fault with the script for not also updating the mesh vertices, causing the desync in the first place. **Exact steps for others to reproduce the error** - Add a shape key to the default Cube so that the Cube now has a Basis shape key - Modify any of the .co of the Basis shape key's .data while remaining in Object mode by using a script or the Python console, e.g. `bpy.context.object.data.shape_keys.reference_key.data- [x].co.x += 1` - Add a new shape key to the default Cube with `from_mix=False` (either the UI, the operator used by the UI or the shape_key_add method of the Cube Object) - The newly created shape key will match the original cube shape instead of the basis shape key shape, so when activated (value -> 1.0) it will revert the mesh's shape back to the original cube shape Alternative steps to reproduce only the desync without creating a new shape key with `from_mix=False`: - Add a shape key to the default Cube so that the Cube now has a Basis shape key - Modify any of the .co of the Basis shape key's .data while remaining in Object mode by using a script or the Python console, e.g. `bpy.context.object.data.shape_keys.reference_key.data- [x].co.x += 1` - Remove the basis shape key from the Cube such that it no longer has any shape keys, the mesh will revert back to the original cube shape when doing so - If you were to instead go into Edit mode and back out to Object mode before removing the Basis shape key (thus fixing the desync), the Cube will remain in the shape of the Basis shape key when the Basis shape key is removed. Modifying the Basis shape's .co via foreach_set('co', my_co_array) has the same issues, but note that it does not update the visuals of the cube in the 3D view immediately so it can appear as if it has done nothing until something else causes the visuals to update e.g. ```me = bpy.context.object.data me.shape_keys.reference_key.data.foreach_set('co', [0,] * 3 * len(me.vertices))``` In both of these sets of steps, you can instead modify the .co of the mesh vertices to achieve the same issue, but with a reversed effect. ---- If you select the default Cube as the active object, the following script will run through the first set of reproduction steps, creating shape keys with both `from_mix=True` and `from_mix=False` and printing whether they match the basis and if not, printing whether they instead match the mesh vertices: ```import bpy def cos_equal(shape_data_or_vertices1, shape_data_or_vertices2): ``` for data1, data2 in zip(shape_data_or_vertices1, shape_data_or_vertices2): if data1.co != data2.co: return False return True ``` cube = bpy.context.object me = cube.data # Remove all shape keys if the script has been run previously if me.shape_keys: ``` bpy.ops.object.shape_key_remove(all=True) ``` print("\nTesting shape keys in {}:".format(bpy.app.version)) basis_shape = cube.shape_key_add(name='Basis') assert me.shape_keys.reference_key == basis_shape # Newly created Basis shape key should match the mesh vertices for v, b in zip(me.vertices, basis_shape.data): ``` assert v.co == b.co ``` # Modify the basis shape key via script in some way basis_shape.data- [x].co.x += 1 print("Modified basis shape key via script") if cos_equal(me.vertices, basis_shape.data): ``` print("Basis shape key matches mesh vertices") ``` else: ``` print("Basis shape key does not match mesh vertices, there is desync!") ``` # Create new shape key, from mix, using the shape_key_add funtion new_shape_from_mix = cube.shape_key_add(name='from_mix=True', from_mix=True) assert new_shape_from_mix.relative_key == basis_shape if cos_equal(basis_shape.data, new_shape_from_mix.data): ``` print("from_mix=True matches basis shape key") ``` else: ``` print("from_mix=True does not match basis shape key!") ``` # Compare against the mesh vertices instead ``` if cos_equal(me.vertices, new_shape_from_mix.data): print(" from_mix=True matches mesh vertices") else: print(" from_mix=True does not match mesh vertices") ``` # New shape key, not from mix, created using the shape_key_add funtion new_shape = cube.shape_key_add(name='from_mix=False', from_mix=False) assert new_shape.relative_key == basis_shape if cos_equal(basis_shape.data, new_shape.data): ``` print("from_mix=False matches basis shape key") ``` else: ``` print("from_mix=False does not match basis shape key!") ``` # Compare against the mesh vertices instead ``` if cos_equal(me.vertices, new_shape.data): print(" from_mix=False matches mesh vertices") else: print(" from_mix=False does not match mesh vertices") ``` ``` Example script output in the System console: ```Testing shape keys in (3, 0, 0): Modified basis shape key via script Basis shape key does not match mesh vertices, there is desync! from_mix=True matches basis shape key from_mix=False does not match basis shape key! ``` from_mix=False matches mesh vertices```
Author

Added subscriber: @Mysteryem-2

Added subscriber: @Mysteryem-2

blender/blender-addons#102452 was marked as duplicate of this issue

blender/blender-addons#102452 was marked as duplicate of this issue
Author

Two clips that follow along the reproduction steps:
Reproduction with creating a new shape key with from_mix=False
ReproductionWithNewFromMixFalseShape.mp4
Reproduction with only the Basis shape key
ReproductionWithoutNewFromMixFalseShape.mp4

Two clips that follow along the reproduction steps: Reproduction with creating a new shape key with `from_mix=False` [ReproductionWithNewFromMixFalseShape.mp4](https://archive.blender.org/developer/F13163696/ReproductionWithNewFromMixFalseShape.mp4) Reproduction with only the Basis shape key [ReproductionWithoutNewFromMixFalseShape.mp4](https://archive.blender.org/developer/F13163698/ReproductionWithoutNewFromMixFalseShape.mp4)
Author

I've realised you can reproduce the desync issue without requiring any scripting:
Starting from a default cube:

  • Create a first shape key so that it becomes the 'basis' shape key
  • Create a second shape key
  • Go into edit mode and modify the vertices in the second shape key
  • Move the second shape key up in the list of shape key to make it into the new 'basis' shape key (optionally change its relative key to itself first to avoid https://developer.blender.org/T79892)
  • The 'basis' shape key (bpy.context.object.data.shape_keys.reference_key) and the mesh vertices are now desynced until you enter and exit Edit mode.
I've realised you can reproduce the desync issue without requiring any scripting: Starting from a default cube: - Create a first shape key so that it becomes the 'basis' shape key - Create a second shape key - Go into edit mode and modify the vertices in the second shape key - Move the second shape key up in the list of shape key to make it into the new 'basis' shape key (optionally change its relative key to itself first to avoid https://developer.blender.org/T79892) - The 'basis' shape key (`bpy.context.object.data.shape_keys.reference_key`) and the mesh vertices are now desynced until you enter and exit Edit mode.
Member

Added subscriber: @OmarEmaraDev

Added subscriber: @OmarEmaraDev
Member

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

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

It seems shape_key_add just creates the new shape key from the mesh itself, not from the reference key. But I am not sure how the reference key relates to the mesh itself, so tagging the module for more information.

It seems `shape_key_add` just creates the new shape key from the mesh itself, not from the reference key. But I am not sure how the reference key relates to the mesh itself, so tagging the module for more information.
Author

If it's deemed that keeping the mesh vertices and reference key in sync is completely down to the script author, then I think it may be best for the steps in my previous comment, to cause a desync without using any scripts, to go into their own separate bug report.
The FBX exporter provided with Blender, for example, exports the mesh's vertex positions. Users could be unaware that the mesh vertices and reference key are desynced due to Blender visibly displaying the reference key, export as FBX and then find their exported mesh does not match what was shown in Blender.

If it's deemed that keeping the mesh vertices and reference key in sync is completely down to the script author, then I think it may be best for the steps in my previous comment, to cause a desync without using any scripts, to go into their own separate bug report. The FBX exporter provided with Blender, for example, exports the mesh's vertex positions. Users could be unaware that the mesh vertices and reference key are desynced due to Blender visibly displaying the reference key, export as FBX and then find their exported mesh does not match what was shown in Blender.
Member

Added subscriber: @halfblue3

Added subscriber: @halfblue3
Philipp Oeser removed the
Interest
Modeling
label 2023-02-09 15:27:40 +01:00
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#98890
No description provided.