I have a grid 8x8 of cubes and I would that each cube fall down one by one and I imagine that each cube had to assign on a specific keyframe but I don't know how to do in python bpy. Below there is the code that I work with, maybe someone has an idea.
Thank you in advance
import bpy
import numpy as np
mass = 1000
f = 0.8
d = 0.999
start_frame = 0
delta = 14400
Dn = 1
nx = 8
ax = 1.2
ny = 8
by = 1.2
x0 = -4.2
y0 = -4.2
z0 = 3.
cube_mesh = bpy.ops.mesh.primitive_cube_add()
bpy.context.active_object.dimensions = (1,1,1)
bpy.ops.object.select_all(action='DESELECT')
cube = bpy.data.objects['Cubo']
cube.select_set(True)
bpy.context.view_layer.objects.active = cube
bpy.ops.rigidbody.object_add()
rigid_body = cube.rigid_body
rigid_body.type = 'ACTIVE'
rigid_body.mass = mass
rigid_body.friction = f
rigid_body.linear_damping = d
xx = np.arange(x0, x0+ax*Dn*nx, ax*Dn)
yy = np.arange(y0, y0+by*Dn*ny, by*Dn)
data = []
for x in xx:
for y in yy:
data.append([x,y,z0,np.random.randint(0,120)])
print('data',data)
lines = len(data)
for line in range(lines):
pos = data[line][:3]
delay = data[line][3]
print('pos', pos)
bpy.ops.object.duplicate()
obj = bpy.context.active_object
obj.location = pos
obj.rigid_body.enabled = True
obj.hide_viewport = False
obj.rigid_body.linear_damping = d
obj.keyframe_insert(data_path='hide_viewport', frame=start_frame)
obj.keyframe_insert(data_path='rigid_body.enabled', frame=start_frame)
obj.keyframe_insert(data_path='rigid_body.linear_damping', frame=start_frame)
obj.rigid_body.enabled = False
obj.hide_viewport = False
obj.keyframe_insert(data_path='hide_viewport', frame=start_frame + delay)
obj.keyframe_insert(data_path="rigid_body.enabled",frame=start_frame + delay)
obj.rigid_body.enabled = True
obj.keyframe_insert(data_path="hide_viewport",frame=start_frame + delay + 1)
obj.keyframe_insert(data_path="rigid_body.enabled",frame=start_frame + delay + 1)
obj.select_set(False)
cube.select_set(True)
bpy.ops.object.delete()