2
$\begingroup$

I have set up a simple car like model in blender 2.8

enter image description here

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

  1. Main Block - Active Rigid Body
  2. Wheel - Active Rigid Body
  3. Empty - Rigid Body Constrained

enter image description here

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?

$\endgroup$
2
  • $\begingroup$ Rotation in blender uses radians. Adding 15 to euler rotation y is actually adding degrees(15) or ~860 degrees to your rotation. . Consider adding rigid body "animated" property keyframes of wheels as True at say frames 1 and 49, then off at 50. as in blender.stackexchange.com/questions/5100/… $\endgroup$
    – batFINGER
    Commented Mar 14, 2019 at 15:43
  • $\begingroup$ No i dont want to animate all... I just want to control the velocity of the wheels via python - the rest should be done by the physics system $\endgroup$
    – hypnomaki
    Commented Mar 14, 2019 at 16:49

1 Answer 1

2
$\begingroup$

I just got it working myself.

enter image description here

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

enter image description here

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
$\endgroup$

You must log in to answer this question.

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