# Insert keyframe for rigid body properties for object python script blender

I have a bunch of objects falling onto a plane, and want to have them fall at different frames (i.e, Object1 falls at frame 1, Object2 falls at frame 100, etc)

Could anyone help me with trying to set a keyframe for the activation using the python script console?

I can do this by manually setting keyframes for the "Dynamic" checkbox in the Rigid Body dropdown, however I would like to automate this.

here is my code so far:

bpy.ops.import_mesh.stl(filepath="C://Users/deden/Desktop/x0y0z0h1.stl") bpy.ops.rigidbody.object_add(type="ACTIVE")

I would imagine the keyframe command would look something like this:

bpy.context.active_object.keyframe_insert(data_path="rigidbody",frame=100)


But I cannot get it to work and haven't found any specific examples.

Alternatively, if anyone could show me how to add these different objects at different frames that would achieve the same result. For this approach I've tried code like:

bpy.context.scene.frame_set(frame=100)
bpy.ops.import_mesh.stl(filepath="C://Users/deden/Desktop/x0y0z0h1.stl")


but when I play the animation all the objects are in place at the beginning of the animation as opposed to the frame I set it to before importing?

Please help!! End goal = objects falling at different times, whether its setting the rigid body properties with keyframes or importing the objects at different frames with rigid body activated

Thanks!

You have to use the correct datapath.

bpy.context.active_object.keyframe_insert(data_path="rigid_body.kinematic",frame=100)

1. Select the object, make it active.
2. Enable the animated (rigidbody.kinematic) property and keyframe it.
3. Disable the animated property on the frame you want to have the object start simulating and keyframe it.

This script assumes there is an object called Cube in the active scene.

import bpy

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

start_frame = 100

# make the object the active object
bpy.context.scene.objects.active = obj
obj.select = True

obj.rigid_body.kinematic = True
obj.keyframe_insert(data_path="rigid_body.kinematic",frame=start_frame)
obj.rigid_body.kinematic = False
obj.keyframe_insert(data_path="rigid_body.kinematic",frame=start_frame+1)


Look at the python tooltips.

• this gives me>>> bpy.context.active_object.keyframe_insert(data_path="rigid_body.kinematic",frame=100) Traceback (most recent call last): File "<blender_console>", line 1, in <module> TypeError: bpy_struct.keyframe_insert() property "rigid_body.kinematic" not found Jul 29 '19 at 20:32
• tried bpy.context.active_object.keyframe_insert(data_path="rigid_body.enabled",frame=100) no luck Jul 29 '19 at 20:40
• if I select the object then do bpy.ops.rigidbody.object_add()...then both of the above commands work, but the object begins falling right at frame0 as opposed to the keyframed frame... Jul 29 '19 at 20:41
• @DerekEden I added some details for you. Jul 29 '19 at 22:25
• you're a legend..thanks so much Jul 29 '19 at 23:42