0
$\begingroup$

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()
$\endgroup$
9
  • 1
    $\begingroup$ Why isn’t this code working? What happens? $\endgroup$
    – TheLabCat
    Commented Apr 3, 2023 at 15:32
  • $\begingroup$ Sorry, I have set italian language on blender so to make the code working you have to switch 'Cubo' with 'Cube' $\endgroup$ Commented Apr 3, 2023 at 15:40
  • 1
    $\begingroup$ What I mean is that you didn’t have any output of the code. What’s going wrong? I don’t want to just try to run it myself, because I might not have things set up quite the same way as you, and just synchronizing our set ups will probably take as long if not longer than solving the problem. $\endgroup$
    – TheLabCat
    Commented Apr 3, 2023 at 17:37
  • $\begingroup$ If you run it from blender the output is a grid of cubes 8x8 while if you run it from another debugging program you will not get any graphical output. Thak you! $\endgroup$ Commented Apr 4, 2023 at 6:52
  • 1
    $\begingroup$ Yes, it does. Now the cubes fall down almost at the same time and the only two parameters that define theyr fall are the "start_keyframe" and the "delay", I think so. The outcome that I expect at the end is a blender simulation where the cubes fall down one by one. $\endgroup$ Commented Apr 4, 2023 at 15:44

0

You must log in to answer this question.

Browse other questions tagged .