www.xbdev.net
xbdev - software development
Thursday April 30, 2026
Home | Contact | Support | Blender (.py) Scripts... Automating Blender ..
     
 

Blender (.py) Scripts...

Automating Blender ..

 

Falling Random Rigid Body Cubes


Throw a few random cubes into the scene - and make them rigid bodies - so they can use forces (gravity and collisions). So they don't just fall and disapear - we'll also add in a ground plane.

<?php
import bpy
import random

# Clear existing objects
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

# Create the ground plane
bpy.ops.mesh.primitive_plane_add(size=20, location=(0, 0, 0))
ground = bpy.context.object
bpy.ops.rigidbody.object_add()
ground.rigid_body.type = 'PASSIVE'

# Create cubes with random positions
num_cubes = 20
for i in range(num_cubes):
    x = random.uniform(-8, 8)
    y = random.uniform(-8, 8)
    z = random.uniform(5, 15)

    bpy.ops.mesh.primitive_cube_add(size=1, location=(x, y, z))
    cube = bpy.context.object

    bpy.ops.rigidbody.object_add()
    cube.rigid_body.mass = random.uniform(0.5, 2.0)

# Add a camera
bpy.ops.object.camera_add(location=(0, -20, 10), rotation=(1.2, 0, 0))
camera = bpy.context.object
bpy.context.scene.camera = camera

# Add a light
bpy.ops.object.light_add(type='SUN', location=(0, 0, 20))
light = bpy.context.object
light.data.energy = 5


Things to Try


A few things to try so you can master the rigid body concept:

• Add a few more shapes (instead of 20) - try ramping up the numbers
• Try other shapes (not just cubes but spheres and donuts)
• Mix in some colors and materials (so they're not just all the same color - but different colors/shiny/glass)
• Go beyond just 'random' placement - build a wall and fire a block at it



















 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2026 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.