Blender python code for generating many icospheres at random places + parenting forces.
import bpy
import random
MAX_LOC = 120
FACT_MIN = 0.5
FACT_MAX = 2
bpy.ops.object.select_all(action='SELECT')
# selecting all
bpy.ops.object.delete()
# deleting all
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.camera_add(enter_editmode=False, align='VIEW', location=(0, -200, 0), rotation=(1.6, 0.0, 0), scale=(1, 1, 1))
#bpy.ops.object.camera_add( location=(0, -5, 0), rotation=(1.6, 0.0, 0), scale=(1, 1, 1))
#adding camera
bpy.context.object.data.lens = 35
#setting camera lens focal length
bpy.ops.object.light_add(type='SUN', align='WORLD', location=(0, 0, 20), scale=(1, 1, 1))
#adding some light
bpy.context.object.data.energy = 20
bpy.context.scene.frame_current = 1
#always starting from the first frame
bpy.context.scene.frame_end = 1000
#bpy.context.space_data.context = 'SCENE'
#bpy.data.scenes["Scene"].(null) = 1000
#not working - must manually change from 250 to 1000 in scene cache
def spawn(ob):
factor = random.uniform(FACT_MIN, FACT_MAX)
#bpy.ops.object.select_all(action='DESELECT')
lx = random.uniform(-MAX_LOC, MAX_LOC)
ly = random.uniform(-MAX_LOC, MAX_LOC)
lz = random.uniform(-MAX_LOC, MAX_LOC)
sx = factor
sy = factor
sz = factor
bpy.ops.mesh.primitive_ico_sphere_add(location=(lx, ly, lz), scale=(sx, sy, sz))
#(adding icosphere)
bpy.ops.rigidbody.object_add()
bpy.context.object.rigid_body.mass *= factor
ico_sphere = bpy.context.object #novo!
bpy.ops.object.effector_add(type='FORCE', location=(lx, ly, lz), scale=(sx, sy, sz))
#(adding force)
bpy.context.object.field.strength = -50
bpy.context.object.field.use_gravity_falloff = False
effector = bpy.context.object
#parenting finished
if __name__ == '__main__':
selected = bpy.context.selected_objects[0] #was [-1]
for i in range (150):
spawn(selected)