www.xbdev.net
xbdev - software development
Thursday June 26, 2025
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.

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=20location=(000))
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):
    
random.uniform(-88)
    
random.uniform(-88)
    
random.uniform(515)

    
bpy.ops.mesh.primitive_cube_add(size=1location=(xyz))
    
cube bpy.context.object

    bpy
.ops.rigidbody.object_add()
    
cube.rigid_body.mass random.uniform(0.52.0)

# Add a camera
bpy.ops.object.camera_add(location=(0, -2010), rotation=(1.200))
camera bpy.context.object
bpy
.context.scene.camera camera

# Add a light
bpy.ops.object.light_add(type='SUN'location=(0020))
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-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.