2
$\begingroup$

I am new to Blender as well as python scripting. I went through various documentation and was able to put together the below code that creates a plane as a passive rigid body, followed by creating one cuboid and 2 cylinders at random locations in the space on top of plane. I want to record the change of positions before and after the simulation. Below is my code to do this.

import bpy
import random
import copy


def createPlaneMesh(name, origin, scale, weight):
# Create mesh and object
    bpy.ops.mesh.primitive_cube_add()
    bpy.ops.transform.resize(value=scale)
    bpy.ops.rigidbody.objects_add()

    ob = bpy.context.object
    ob.name = name
    ob.location = origin
    ob.show_name = True
    ob.rigid_body.mass = weight
    ob.rigid_body.type = "PASSIVE"
    return ob

def createCubeMesh(name, origin, scale, weight):
# Create mesh and object
    bpy.ops.mesh.primitive_cube_add()
    bpy.ops.transform.resize(value=scale)
    bpy.ops.rigidbody.objects_add()

    ob = bpy.context.object
    ob.name = name
    ob.location = origin
    ob.show_name = True
    ob.rigid_body.mass = weight
    return ob

def createCylMesh(name, origin, r, h, weight):
# Create mesh and object
    bpy.ops.mesh.primitive_cylinder_add()
    bpy.ops.transform.resize(value=(r, r, h))
    bpy.ops.rigidbody.objects_add()

    ob = bpy.context.object
    ob.name = name
    ob.location = origin
    ob.show_name = True
    ob.rigid_body.mass = weight
    return ob

def clear():
    print("inside clear")
    candidate_list = [item.name for item in bpy.data.objects if item.type == "MESH"]
    for object_name in candidate_list:
        bpy.data.objects[object_name].select = True
        # remove all selected.
    bpy.ops.object.delete()
    return

def run(origin):

    l = []
    surface = createPlaneMesh('surface', (0, 0, -10), (100, 100, 3), 10)
    cube1 = createCubeMesh('cuboid1', (random.uniform(-15, 15),   random.uniform(-15, 15), random.uniform(5, 10)), (7.75, 5.625, 1.5), 0.296)
    l.append(cube1)
    cyl1 = createCylMesh('cylinder1', (random.uniform(-15, 15), random.uniform(-15, 15), random.uniform(3, 10)), 3, 9.75, 0.169)
    l.append(cyl1)
    cyl2 = createCylMesh('cylinder2', (random.uniform(-15, 15), random.uniform(-15, 15), random.uniform(3, 10)), 3, 9.75, 0.169)
    l.append(cyl2)
    return l


def get_location(list_ob):
    list_location = []
    for i in list_ob:
        list_location.append(i.location.copy())
    return list_location

def run_sim(list_ob):
    print('pre-simulation')
    prepose = get_location(list_ob)
    for i in prepose:
        print(i)
    scene = bpy.context.screen.scene
    bpy.ops.rigidbody.objects_add(type='ACTIVE')
    bpy.ops.ptcache.bake_all(bake=True)
    context = bpy.context
    context.scene.frame_set(scene.frame_end)
    scene.frame_current = scene.frame_end
    bpy.ops.object.visual_transform_apply()
    print('post-simulation')
    for i in range(len(prepose)):
        print(prepose[i], '=>', list_ob[i].location, list_ob[i].name)

    return

if __name__ == "__main__":
    clear()
    run_sim(run((0,0,0)))

It works well if I use

./blender -P ~/Desktop/physics_simulations/python_scripts/question_simulation.py

I can see the objects after simulation. But when I use -b I see only change in the last object when I output the positions of the objects. As below

post-simulation
<Vector (11.4047, -10.3778, 8.7058)> => <Vector (11.4047, -10.3778, 8.7058)> cuboid1
<Vector (-13.9499, 7.4854, 6.9536)> => <Vector (-13.9499, 7.4854, 6.9536)> cylinder1
<Vector (6.8909, -7.7191, 8.7173)> => <Vector (-45.5964, 49.2257, -4.0609)> cylinder2

Please let me know if I am missing anything here. Thanks in advance

$\endgroup$

1 Answer 1

1
$\begingroup$

Found the method that selects all the objects. I added the below line before visual_transform_apply() and it works as expected.

bpy.ops.object.select_all(action='SELECT')  
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .