10
$\begingroup$

Currently I have a default cube and 400 frames.

Here's what I have working without keyframing

import bpy

current_scene = bpy.context.scene

default_cube = current_scene.objects['default_cube']

def update_scene():
    current_frame_value = current_scene.frame_current

    if current_frame_value < 100:
        default_cube.scale.x = .05
    elif current_frame_value < 200:
         default_cube.scale.x = .08
    elif current_frame_value > 300:
        default_cube.scale.x = .3
     else:
        default_cube.scale = 1

bpy.app.handlers.frame_change_pre.append(update_scene)

print('I work!')

The problem I'm having with the non-keyframe method is there isn't any interpolation between the scaling and I intend do this for thousands of frames. What is a good method to use keyframes programmatically to achieve interpolation?

https://docs.blender.org/manual/en/latest/editors/python_console.html Blender 2.8 doesn't reference anywhere.

$\endgroup$

1 Answer 1

32
$\begingroup$

Figured it out!

Here's how to use the Python API to set it:

# X, Y, and Z location to set
default_cube.location = (0.0, 0.0, 0.0)
# Set the keyframe with that location, and which frame.
default_cube.keyframe_insert(data_path="location", frame=1)

# do it again!
default_cube.location = (3.0, 2.0, 1.0)
# setting it for frame 10
default_cube.keyframe_insert(data_path="location", frame=10)

Details on .keyframe_insert() can be found in Blender's manual.

$\endgroup$
1
  • $\begingroup$ Based God Julian, thank you $\endgroup$ Commented Apr 13, 2022 at 22:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .