Using rigidbody physics to create/bake/place geometry in a scene

Hi all,

in short, I would like to be able to, semi-automatically, build a wall by ‘physically’ dropping the (generated) bricks into a box representing the outline of the wall. It’s almost working, but I can’t seem to get to ‘end’ the simulation (and keep the final transform).

Below is the relevant code; step [3] is the problem.

        # [0] activate the target object
        bpy.ops.object.select_all(action='DESELECT')
        obj.select = True
        context.scene.objects.active = obj
        
        # [1] add a (default) rigidbody to it and bake its transform
        bpy.ops.rigidbody.object_add()
        bpy.ops.nla.bake(frame_start = context.scene.frame_start, frame_end = context.scene.frame_end, bake_types = {'OBJECT'})
        
        # [2] go to last frame and apply the simulated transform (object has supposedly come to rest)
        context.scene.frame_set(context.scene.frame_end)
        bpy.ops.object.visual_transform_apply()
        
        
        # [3] unlink/remove the action created (!!) on the objects transform in step [1: bake]
        # !! this is the problem: neither of the below lines work -- wrong context
        # bpy.ops.nla.action_unlink(force_delete = True)
        # bpy.ops.action.unlink()
        succes3 = False
        
        
        # [4] turn off 'dynamic' (keep it where it is?) but keep collisions in place for future simulations
        # -- this works, but should probably be commented out while debugging, eg until step [3] works 
        if succes3: obj.rigid_body.enabled = False

So the question really seems to be: how to unlink the (active) action/s from the active object/s, from within an Operator.execute()-context?

Here’s the current source/addon: dropRigidbodyTool.zip (3.83 KB)
Here’s a test blend (2.76) file (press generate button, near buttom of panel): dropRigiBody-010.blend (530 KB)
It looks like this:


Any help very much appreciated!

Ok, problem solved. I seem to have misinterpreted the ‘wrong context’ error message. In my case, this usually means ‘you can’t do this because you’re in an operator execute/poll method’. In this case, however, it meant my code was being called from the wrong context.area.type – which is ‘VIEW_3D’ according to the addon/panel, but needs to be ‘DOPESHEET_EDITOR’ in order to be able to unlink the active action.

Fortunately, this can be changed, even when in an operator-executing-context. So this code [step 3] works:

        # [3] unlink/remove the action created on the objects transform in step [1: bake]
        context.area.type = 'DOPESHEET_EDITOR'
        context.space_data.mode = 'ACTION'
        bpy.ops.action.unlink()
        context.area.type = 'VIEW_3D'

Woohoo! :slight_smile: