0
$\begingroup$

What is the right way to create two moving bodies collision by script? Since both of the objects been animated they collapse into each other when colliding. How can I prevent this from happening?

enter image description here

Part of my code:

bpy.data.objects['Cube'].rigid_body.kinematic = True
bpy.data.objects['Cube'].location = [-5,-5,1]
bpy.data.objects['Cube'].keyframe_insert(data_path='location', frame=1)
bpy.data.objects['Cube'].location = [0,0,1]
bpy.data.objects['Cube'].keyframe_insert(data_path='location', frame=20)
bpy.data.objects['Cube'].rigid_body.kinematic = False

bpy.data.objects['Sphere'].rigid_body.kinematic = True
bpy.data.objects['Sphere'].location = [5,-5,1]
bpy.data.objects['Sphere'].keyframe_insert(data_path='location', frame=1)
bpy.data.objects['Sphere'].location = [0,0,1]
bpy.data.objects['Sphere'].keyframe_insert(data_path='location', frame=20)
bpy.data.objects['Sphere'].rigid_body.kinematic = False
$\endgroup$
0
$\begingroup$

In this setup, you will need to add keyframes for the kinematic property. The kinematic property should be enabled during the beginning so that the object movement is controlled by the animation. Right before the two objects collide, the kinematic property should be disabled so that the rigid body simulator takes control over the object movement.

Here is an example for how to modify your script so that the kinematic property is disabled at frame 12:

bpy.data.objects['Cube'].rigid_body.kinematic = True
bpy.data.objects['Cube'].keyframe_insert(data_path='rigid_body.kinematic', frame=1)
bpy.data.objects['Cube'].location = [-5,-5,1]
bpy.data.objects['Cube'].keyframe_insert(data_path='location', frame=1)
bpy.data.objects['Cube'].location = [0,0,1]
bpy.data.objects['Cube'].keyframe_insert(data_path='location', frame=20)
bpy.data.objects['Cube'].rigid_body.kinematic = False
bpy.data.objects['Cube'].keyframe_insert(data_path='rigid_body.kinematic', frame=12)

bpy.data.objects['Sphere'].rigid_body.kinematic = True
bpy.data.objects['Sphere'].keyframe_insert(data_path='rigid_body.kinematic', frame=1)
bpy.data.objects['Sphere'].location = [5,-5,1]
bpy.data.objects['Sphere'].keyframe_insert(data_path='location', frame=1)
bpy.data.objects['Sphere'].location = [0,0,1]
bpy.data.objects['Sphere'].keyframe_insert(data_path='location', frame=20)
bpy.data.objects['Sphere'].rigid_body.kinematic = False
bpy.data.objects['Sphere'].keyframe_insert(data_path='rigid_body.kinematic', frame=12)
```
$\endgroup$
  • $\begingroup$ It works but this way isn't convenient since my plane has a friction parameter which may result that the cube (which has a more shared surface with the plane) won't get to its target location because of that. The Ideal thing would have been to change the kinematic properties one frame before the collision, but since I'm using scripting without GUI it's much harder to do that. Is there a more convenient way to do what I'm trying? $\endgroup$ – AvivSham Sep 28 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.