0
$\begingroup$

I have a technical question.

Let's say I want to create an environment with an event for example:

  • a man kicking a ball

Can I control the physics of the scene?

I would like to define the ball's velocity as 10m/sec.

$\endgroup$
2
$\begingroup$

Yes, you can access almost all properties with python. Activate the python tooltips, which make it easy to find the name of a property.

I'd use the technique from this answer.

  • Make the object an active rigid body.
  • Set a keyframe on frame 1 of the object location.
  • Go to frame 3, move the object (so that it moves with the desired speed from frame 1 to 3) and set another location keyframe.
  • Activate the rigidbody's Kinematic (Animated) property on frame 2 and keyframe it. Deactivate it on frame 3 and keyframe it.

With that, the object will start beeing a rigidbody on frame three. And the rigid body system will use its velocity from frame 2, which we define with our location keyframes. A script to achieve this is at the end of this answer.

(Reference for keyframing rigid body properties of multiple objects.)

The passive rigid body collision objects are setup manually, but can be coded just as well.

result

The finished script:

import bpy
import mathutils

ball = bpy.data.objects['Ball']

# select the ball and make it a rigid body tutorial
ball.select = True
bpy.context.scene.objects.active = ball
bpy.ops.rigidbody.object_add()

# Set the rigid bodies properties
ball.rigid_body.collision_shape = 'SPHERE'
ball.rigid_body.friction = 0.6
ball.rigid_body.restitution = 0.4
ball.rigid_body.use_margin = True

# Get the first frame of the scene, or whenever
# the ball is supposed to start moving, and the fps.
# Also set the interpolation to linear.
start_frame = bpy.context.scene.frame_start
fps = bpy.context.scene.render.fps
pref_edit = bpy.context.user_preferences.edit
keyInterp = pref_edit.keyframe_new_interpolation_type
pref_edit.keyframe_new_interpolation_type ='LINEAR'

# setup the start position and the velocity
# the velocity is in units/second/fps*2
start_position = ball.location
velocity = mathutils.Vector((1, 0, .5)).normalized() * 10 / fps * 2

# keyframe the start_position on the first frame
# and the moved ball on the next frame
ball.location = start_position
ball.keyframe_insert(data_path='location', frame=start_frame)
ball.location = start_position + velocity
ball.keyframe_insert(data_path='location', frame=start_frame+2)

ball.rigid_body.kinematic = True
ball.keyframe_insert(data_path='rigid_body.kinematic', frame=start_frame + 1)
ball.rigid_body.kinematic = False
ball.keyframe_insert(data_path='rigid_body.kinematic', frame=start_frame + 2)

# set interpolation type back
pref_edit.keyframe_new_interpolation_type = keyInterp

$\endgroup$
  • $\begingroup$ Thank you for your reply. Where can I learn how to control and create a full environment? ball, person etc. Is it possible to create all the parts of the environment in GUI and afterward generate python code from it? if so, how? I'm completely new to Blender and I need some guidance. $\endgroup$ – AvivSham Jul 29 at 17:37
  • $\begingroup$ Stackexchange is not a normal forum. Ask one question per Q&A post. Question also have to be specific. Your asking about very broad topics and it will probably beneficial to watch some basic tutorials first. $\endgroup$ – Leander Aug 7 at 8:11
  • $\begingroup$ Also, if this answer solved your question consider marking it as accepted by clicking on the checkmark on the top left of the answer. $\endgroup$ – Leander Aug 7 at 8:11

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.