Keyframe returned by keyframe_points.insert() gets ruined by any other call to keyframe_points.insert() #83044

Closed
opened 2020-11-26 11:17:32 +01:00 by burger@wiredworks.com · 15 comments

System Information
Operating system: Windows-7-6.1.7601-SP1 64 Bits
Graphics card: Quadro K2000/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 385.69

Blender Version
Broken: version: 2.83.4, branch: master, commit date: 2020-08-05 06:00, hash: c113af8288
Worked: (newest version of Blender that worked as expected)

Short description of error
Keyframe_points handles not set

Exact steps for others to reproduce the error

Default startup with Cube

A simple script not setting the keyframe_points handles to the specified points

import bpy

Dataobject = bpy.data.objects['Cube']

Dataobject['Pos'] = 0
fcurve = Dataobject.driver_add('["Pos"]')
- THX to Philipp Oeser (lichtwerk)
- to edit the driver fcurve with keyframes, you'll have to remove the fmodifier
try:
    fcurve.modifiers.remove(fcurve.modifiers[0])
except IndexError:
     pass
PointA = fcurve.keyframe_points.insert(0,0)
PointB = fcurve.keyframe_points.insert(1,1)
PointC = fcurve.keyframe_points.insert(2,1)
PointD = fcurve.keyframe_points.insert(3,0)

PointA.handle_left  = ( 0 , 0)
PointA.handle_right = ( 0 , 0)
PointB.handle_left  = ( 1 , 1)
PointB.handle_right = ( 1 , 1)
PointC.handle_left  = ( 2 , 1)
PointC.handle_right = ( 2 , 1)
PointD.handle_left  = ( 3 , 0)
PointD.handle_right = ( 3 , 0)

Keyframe_points.png

Note :The handles of the last point are correct

A simple script setting them to their correct points

import bpy

Dataobject = bpy.data.objects['Cube']

Dataobject['Pos'] = 0
fcurve = Dataobject.driver_add('["Pos"]')
- THX to Philipp Oeser (lichtwerk)
- to edit the driver fcurve with keyframes, you'll have to remove the fmodifier
try:
    fcurve.modifiers.remove(fcurve.modifiers[0])
except IndexError:
     pass
PointA = fcurve.keyframe_points.insert(0,0)
PointB = fcurve.keyframe_points.insert(1,1)
PointC = fcurve.keyframe_points.insert(2,1)
PointD = fcurve.keyframe_points.insert(3,0)

fcurve.keyframe_points[0].handle_left  = ( 0 , 0)
fcurve.keyframe_points[0].handle_right = ( 0 , 0)
fcurve.keyframe_points[1].handle_left  = ( 1 , 1)
fcurve.keyframe_points[1].handle_right = ( 1 , 1)
fcurve.keyframe_points[2].handle_left  = ( 2 , 1)
fcurve.keyframe_points[2].handle_right = ( 2 , 1)
fcurve.keyframe_points[3].handle_left  = ( 3 , 0)
fcurve.keyframe_points[3].handle_right = ( 3 , 0)

keyframe1.png

To check, just compare the memory addresses:

print(PointA, fcurve.keyframe_points[0])
print(PointB, fcurve.keyframe_points[1])
print(PointC, fcurve.keyframe_points[2])
print(PointD, fcurve.keyframe_points[3])
**System Information** Operating system: Windows-7-6.1.7601-SP1 64 Bits Graphics card: Quadro K2000/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 385.69 **Blender Version** Broken: version: 2.83.4, branch: master, commit date: 2020-08-05 06:00, hash: `c113af8288` Worked: (newest version of Blender that worked as expected) **Short description of error** Keyframe_points handles not set **Exact steps for others to reproduce the error** Default startup with Cube A simple script not setting the keyframe_points handles to the specified points ``` import bpy Dataobject = bpy.data.objects['Cube'] Dataobject['Pos'] = 0 fcurve = Dataobject.driver_add('["Pos"]') - THX to Philipp Oeser (lichtwerk) - to edit the driver fcurve with keyframes, you'll have to remove the fmodifier try: fcurve.modifiers.remove(fcurve.modifiers[0]) except IndexError: pass PointA = fcurve.keyframe_points.insert(0,0) PointB = fcurve.keyframe_points.insert(1,1) PointC = fcurve.keyframe_points.insert(2,1) PointD = fcurve.keyframe_points.insert(3,0) PointA.handle_left = ( 0 , 0) PointA.handle_right = ( 0 , 0) PointB.handle_left = ( 1 , 1) PointB.handle_right = ( 1 , 1) PointC.handle_left = ( 2 , 1) PointC.handle_right = ( 2 , 1) PointD.handle_left = ( 3 , 0) PointD.handle_right = ( 3 , 0) ``` ![Keyframe_points.png](https://archive.blender.org/developer/F9379650/Keyframe_points.png) Note :The handles of the last point are correct A simple script setting them to their correct points ``` import bpy Dataobject = bpy.data.objects['Cube'] Dataobject['Pos'] = 0 fcurve = Dataobject.driver_add('["Pos"]') - THX to Philipp Oeser (lichtwerk) - to edit the driver fcurve with keyframes, you'll have to remove the fmodifier try: fcurve.modifiers.remove(fcurve.modifiers[0]) except IndexError: pass PointA = fcurve.keyframe_points.insert(0,0) PointB = fcurve.keyframe_points.insert(1,1) PointC = fcurve.keyframe_points.insert(2,1) PointD = fcurve.keyframe_points.insert(3,0) fcurve.keyframe_points[0].handle_left = ( 0 , 0) fcurve.keyframe_points[0].handle_right = ( 0 , 0) fcurve.keyframe_points[1].handle_left = ( 1 , 1) fcurve.keyframe_points[1].handle_right = ( 1 , 1) fcurve.keyframe_points[2].handle_left = ( 2 , 1) fcurve.keyframe_points[2].handle_right = ( 2 , 1) fcurve.keyframe_points[3].handle_left = ( 3 , 0) fcurve.keyframe_points[3].handle_right = ( 3 , 0) ``` ![keyframe1.png](https://archive.blender.org/developer/F9379695/keyframe1.png) - the problem is that each time you insert a new keyframe, you are actually ruining all previous return values of `keyframe_points.insert` - the reason for this is that `insert_bezt_fcurve` will actually realloc the whole `fcu->bezt` [which holds your "keyframes"], see [here ]] and [[ https:*developer.blender.org/diffusion/B/browse/master/source/blender/editors/animation/keyframing.c$453 | here ](https:*developer.blender.org/diffusion/B/browse/master/source/blender/editors/animation/keyframing.c$436) To check, just compare the memory addresses: ``` print(PointA, fcurve.keyframe_points[0]) print(PointB, fcurve.keyframe_points[1]) print(PointC, fcurve.keyframe_points[2]) print(PointD, fcurve.keyframe_points[3]) ```

Added subscriber: @martburg

Added subscriber: @martburg

it get' even better when more points are added the already correct handles get overwritten

import bpy

Dataobject = bpy.data.objects['Cube']

Dataobject['Pos'] = 0
fcurve = Dataobject.driver_add('["Pos"]')
- THX to Philipp Oeser (lichtwerk)
- to edit the driver fcurve with keyframes, you'll have to remove the fmodifier
try:
    fcurve.modifiers.remove(fcurve.modifiers[0])
except IndexError:
     pass
PointA = fcurve.keyframe_points.insert(0,0)
PointB = fcurve.keyframe_points.insert(1,1)
PointC = fcurve.keyframe_points.insert(2,1)
PointD = fcurve.keyframe_points.insert(3,0)

fcurve.keyframe_points[0].handle_left  = ( 0 , 0)
fcurve.keyframe_points[0].handle_right = ( 0 , 0)
fcurve.keyframe_points[1].handle_left  = ( 1 , 1)
fcurve.keyframe_points[1].handle_right = ( 1 , 1)
fcurve.keyframe_points[2].handle_left  = ( 2 , 1)
fcurve.keyframe_points[2].handle_right = ( 2 , 1)
fcurve.keyframe_points[3].handle_left  = ( 3 , 0)
fcurve.keyframe_points[3].handle_right = ( 3 , 0)
 
PointE = fcurve.keyframe_points.insert(0.5,0)
PointF = fcurve.keyframe_points.insert(1.5,1)
PointG = fcurve.keyframe_points.insert(2.5,1)
PointH = fcurve.keyframe_points.insert(3.5,0)

keyframe2.png

it get' even better when more points are added the already correct handles get overwritten ``` import bpy Dataobject = bpy.data.objects['Cube'] Dataobject['Pos'] = 0 fcurve = Dataobject.driver_add('["Pos"]') - THX to Philipp Oeser (lichtwerk) - to edit the driver fcurve with keyframes, you'll have to remove the fmodifier try: fcurve.modifiers.remove(fcurve.modifiers[0]) except IndexError: pass PointA = fcurve.keyframe_points.insert(0,0) PointB = fcurve.keyframe_points.insert(1,1) PointC = fcurve.keyframe_points.insert(2,1) PointD = fcurve.keyframe_points.insert(3,0) fcurve.keyframe_points[0].handle_left = ( 0 , 0) fcurve.keyframe_points[0].handle_right = ( 0 , 0) fcurve.keyframe_points[1].handle_left = ( 1 , 1) fcurve.keyframe_points[1].handle_right = ( 1 , 1) fcurve.keyframe_points[2].handle_left = ( 2 , 1) fcurve.keyframe_points[2].handle_right = ( 2 , 1) fcurve.keyframe_points[3].handle_left = ( 3 , 0) fcurve.keyframe_points[3].handle_right = ( 3 , 0) PointE = fcurve.keyframe_points.insert(0.5,0) PointF = fcurve.keyframe_points.insert(1.5,1) PointG = fcurve.keyframe_points.insert(2.5,1) PointH = fcurve.keyframe_points.insert(3.5,0) ``` ![keyframe2.png](https://archive.blender.org/developer/F9379763/keyframe2.png)
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk
Member

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Member

Will check

Will check
Member

Not sure how this is supposed to work correctly, but here are my findings:

To check, just compare the memory addresses:

print(PointA, fcurve.keyframe_points[0])
print(PointB, fcurve.keyframe_points[1])
print(PointC, fcurve.keyframe_points[2])
print(PointD, fcurve.keyframe_points[3])

Now, one might come to the conclusion that the return value is still usable for the time no other keyframe has been inserted, but this fails as well (I havent tracked down this further):

import bpy

Dataobject = bpy.data.objects['Cube']

Dataobject['Pos'] = 0
fcurve = Dataobject.driver_add('["Pos"]')
- THX to Philipp Oeser (lichtwerk)
- to edit the driver fcurve with keyframes, you'll have to remove the fmodifier
try:
    fcurve.modifiers.remove(fcurve.modifiers[0])
except IndexError:
     pass
PointA = fcurve.keyframe_points.insert(0,0)
PointA.handle_left  = ( 0 , 0)
PointA.handle_right = ( 0 , 0)
fcurve.update()

PointB = fcurve.keyframe_points.insert(1,1)
PointB.handle_left  = ( 1 , 1)
PointB.handle_right = ( 1 , 1)
fcurve.update()

PointC = fcurve.keyframe_points.insert(2,1)
PointC.handle_left  = ( 2 , 1)
PointC.handle_right = ( 2 , 1)
fcurve.update()

PointD = fcurve.keyframe_points.insert(3,0)
PointD.handle_left  = ( 3 , 0) #these work
PointD.handle_right = ( 3 , 0) #these work
fcurve.update()

So to me it looks like the returned Keyframe of keyframe_points.insert gets broken by every other call to keyframe_points.insert.
And the way inserting keyframes happens internally, there is also no easy way out.

Not sure how this is supposed to work correctly, but here are my findings: - you might be lucky, it might work with the last Keyframe you are manipulating - the problem is that each time you insert a new keyframe, you are actually ruining all previous return values of `keyframe_points.insert` - the reason for this is that `insert_bezt_fcurve` will actually realloc the whole `fcu->bezt` [which holds your "keyframes"], see [here ]] and [[ https:*developer.blender.org/diffusion/B/browse/master/source/blender/editors/animation/keyframing.c$453 | here ](https:*developer.blender.org/diffusion/B/browse/master/source/blender/editors/animation/keyframing.c$436) To check, just compare the memory addresses: ``` print(PointA, fcurve.keyframe_points[0]) print(PointB, fcurve.keyframe_points[1]) print(PointC, fcurve.keyframe_points[2]) print(PointD, fcurve.keyframe_points[3]) ``` Now, one might come to the conclusion that the return value is still usable for the time no other keyframe has been inserted, but this fails as well (I havent tracked down this further): ``` import bpy Dataobject = bpy.data.objects['Cube'] Dataobject['Pos'] = 0 fcurve = Dataobject.driver_add('["Pos"]') - THX to Philipp Oeser (lichtwerk) - to edit the driver fcurve with keyframes, you'll have to remove the fmodifier try: fcurve.modifiers.remove(fcurve.modifiers[0]) except IndexError: pass PointA = fcurve.keyframe_points.insert(0,0) PointA.handle_left = ( 0 , 0) PointA.handle_right = ( 0 , 0) fcurve.update() PointB = fcurve.keyframe_points.insert(1,1) PointB.handle_left = ( 1 , 1) PointB.handle_right = ( 1 , 1) fcurve.update() PointC = fcurve.keyframe_points.insert(2,1) PointC.handle_left = ( 2 , 1) PointC.handle_right = ( 2 , 1) fcurve.update() PointD = fcurve.keyframe_points.insert(3,0) PointD.handle_left = ( 3 , 0) #these work PointD.handle_right = ( 3 , 0) #these work fcurve.update() ``` So to me it looks like the returned Keyframe of `keyframe_points.insert` gets broken by every other call to `keyframe_points.insert`. And the way inserting keyframes happens internally, there is also no easy way out.
Philipp Oeser changed title from Keyframe_points handles not set to Keyframe returned by keyframe_points.insert() gets ruined by any other call to keyframe_points.insert() 2020-11-26 13:59:11 +01:00
Member

I think this also answers the point raised in #82227 (Inconsistent Behavior T81299)

In #82227#1051121, @martburg wrote:
...
and the inconsistency is not that behavior but the loss of the reference to the points

    self.InK.co = (0,0)
    self.Mid.co = (100,10)
    self.Out.co = (200,0)

declared in def execute but in def modal those points have to be addressed as

        self.cue.keyframe_points- [x].co = (0,0)
        self.cue.keyframe_points- [x].co = (100,10)
        self.cue.keyframe_points- [x].co = (200,0)
I think this also answers the point raised in #82227 (Inconsistent Behavior T81299) > In #82227#1051121, @martburg wrote: > ... > and the inconsistency is not **that** behavior but the loss of the reference to the points > > self.InK.co = (0,0) > self.Mid.co = (100,10) > self.Out.co = (200,0) > > declared in `def execute` but in `def modal` those points have to be addressed as > > self.cue.keyframe_points- [x].co = (0,0) > self.cue.keyframe_points- [x].co = (100,10) > self.cue.keyframe_points- [x].co = (200,0)

Yes, it does ---- you were faster

THX
Martin

Yes, it does ---- you were faster THX Martin

Added subscriber: @dr.sybren

Added subscriber: @dr.sybren

fcurve.keyframe_points.insert() sorts the keyframes to ensure they're stored in order of timestamp. This means that Python objects created before the call are not necessarily valid after that call. Pass options={'FAST'} to prevent this, and call fcurve.update() after the work on the curve is done.

`fcurve.keyframe_points.insert()` sorts the keyframes to ensure they're stored in order of timestamp. This means that Python objects created before the call are not necessarily valid after that call. Pass `options={'FAST'}` to prevent this, and call `fcurve.update()` after the work on the curve is done.
Member

@dr.sybren: thx looking into this.

Unfortunately, this doesnt seem to work...
#83044.blend

Am I doing something wrong?

@dr.sybren: thx looking into this. Unfortunately, this doesnt seem to work... [#83044.blend](https://archive.blender.org/developer/F9381023/T83044.blend) Am I doing something wrong?
Member

In #83044#1061973, @lichtwerk wrote:

To check, just compare the memory addresses:

print(PointA, fcurve.keyframe_points[0])
print(PointB, fcurve.keyframe_points[1])
print(PointC, fcurve.keyframe_points[2])
print(PointD, fcurve.keyframe_points[3])

Looks like all of this is still true -- even for options={'FAST'}

<bpy_struct, Keyframe at 0x7fbaee09e9f8> <bpy_struct, Keyframe at 0x7fbafafa9208>
<bpy_struct, Keyframe at 0x7fbafa78bbf0> <bpy_struct, Keyframe at 0x7fbafafa9250>
<bpy_struct, Keyframe at 0x7fbafb2aad38> <bpy_struct, Keyframe at 0x7fbafafa9298>
<bpy_struct, Keyframe at 0x7fbafafa92e0> <bpy_struct, Keyframe at 0x7fbafafa92e0>

For the last call, address is the same, previous ones are ruined.

> In #83044#1061973, @lichtwerk wrote: > - the problem is that each time you insert a new keyframe, you are actually ruining all previous return values of `keyframe_points.insert` > -- the reason for this is that `insert_bezt_fcurve` will actually realloc the whole `fcu->bezt` [which holds your "keyframes"], see [here ]] and [[ https:*developer.blender.org/diffusion/B/browse/master/source/blender/editors/animation/keyframing.c$453 | here ](https:*developer.blender.org/diffusion/B/browse/master/source/blender/editors/animation/keyframing.c$436) > > To check, just compare the memory addresses: > > ``` > print(PointA, fcurve.keyframe_points[0]) > print(PointB, fcurve.keyframe_points[1]) > print(PointC, fcurve.keyframe_points[2]) > print(PointD, fcurve.keyframe_points[3]) > ``` Looks like all of this is still true -- even for `options={'FAST'}` ``` <bpy_struct, Keyframe at 0x7fbaee09e9f8> <bpy_struct, Keyframe at 0x7fbafafa9208> <bpy_struct, Keyframe at 0x7fbafa78bbf0> <bpy_struct, Keyframe at 0x7fbafafa9250> <bpy_struct, Keyframe at 0x7fbafb2aad38> <bpy_struct, Keyframe at 0x7fbafafa9298> <bpy_struct, Keyframe at 0x7fbafafa92e0> <bpy_struct, Keyframe at 0x7fbafafa92e0> ``` For the last call, address is the same, previous ones are ruined.

Changed status from 'Confirmed' to: 'Archived'

Changed status from 'Confirmed' to: 'Archived'

Ah yes, I confused two things. options={'FAST'} skips the calculation of automatic handles. Reordering of the keyframes always happens.

I'm closing this as invalid, as this is not a bug but simply how Blender works. This is similar to the array re-allocation described in the "Gochas" section of the Python API documentation.

Ah yes, I confused two things. `options={'FAST'}` skips the calculation of automatic handles. Reordering of the keyframes always happens. I'm closing this as invalid, as this is not a bug but simply how Blender works. This is similar to the [array re-allocation described in the "Gochas" section of the Python API documentation](https://docs.blender.org/api/master/info_gotcha.html#array-re-allocation).
Member

In #83044#1063064, @dr.sybren wrote:
This is similar to the array re-allocation described in the "Gochas" section of the Python API documentation.

This is good to know!

> In #83044#1063064, @dr.sybren wrote: > This is similar to the [array re-allocation described in the "Gochas" section of the Python API documentation](https://docs.blender.org/api/master/info_gotcha.html#array-re-allocation). This is good to know!
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#83044
No description provided.