How to apply a force via python to a rigid body wheel I have set up a simple car like model in blender 2.8



I have now set up a rigid body so I can simulate simple physics


Main Block - Active Rigid Body
Wheel - Active Rigid Body
Empty - Rigid Body Constrained




This exactly works as expected. When I set the constraint to Hinge, also the wheels are turning when the vehicle rolls down a test plane)

But I need to control the vehicle via a python script (Apply a rotation/force to the 4 wheels)

Therefore I've set up the following script

import bpy, time

# get the 4 wheels
wheels = [bpy.data.objects['WheelFrontLeft'], 
    bpy.data.objects['WheelBackLeft'], 
    bpy.data.objects['WheelBackRight'], 
    bpy.data.objects['WheelFrontRight']]

for wheel in wheels: 
    wheel.animation_data_clear() # clear keyframes
    wheel.rotation_euler[1] = 0 # reset rotation

for i in range(1,50): # step through 50 frames
    bpy.context.scene.frame_set(i)
    for wheel in wheels: # iterate wheels
        wheel.rotation_euler[1] += 15 # rotate +15 
        wheel.keyframe_insert(data_path="rotation_euler", frame=i) # insert keyframe


Unfortunately this does not change anything...

After running the script I got a totally messed up scene (everything is wrong positioned/rotated) and if I restart the animation, the vehicle just keeps falling (Only the Rigid body simulation applies without the rotation)

So I don't see any rotation at all. Only when I'm checking the animation checkbox (for the active rigid body wheels), the wheels are moving. In that case the car is not falling anymore (physics won't be applied).
Obviously that is an either or switch for physics/animation

I also tried setting the empty constraint type to Motor instead of Fixed but then it just falls apart.... 

Also rotating the constraints does not help at all....

What am I doing wrong? How can I fix this?
I just got it working myself.



The solution is the motor constraint. Using the motor constraint alone, the vehicle will fall apart. Thus you have to add another constraint between each vehicle and the body - a hinge constraint to only allow rotation.

Thus the complete setup looks like this


Create the vehicle (body and 4 wheels as seperate objects)
Create a Hinge Constraint between each wheel and the body (I used an empty for it - take care of the right rotation)
Create a Motor Constraint between each wheel and the body (Angular movement - I used an empty for it - take care of the right rotation))


Now you can check out the context menu of the angular velocity. There you can find the option "Copy Data Path". This will copy the relative property path of the object to use it withing your scripts



Then you can use it for example like this

constraints = [bpy.data.objects['fl'], bpy.data.objects['bl'], 
    bpy.data.objects['br'], bpy.data.objects['fr']]

for c in constraints:
    c.rigid_body_constraint.motor_ang_target_velocity=5 
    c.rigid_body_constraint.motor_ang_max_impulse=100