1
$\begingroup$

I am new in Blender and I want to get the velocity of a moving object. I am not a programmer and I guess I have to create a Python code. Can you maybe help me with it, because I don't even know the libraries of python or how to use them? Can you show me a way to program a "get velocity" code or node? I will really appreciate a finger pointing in the right direction.

Thank you very much

example of gear moving along rack

$\endgroup$

1 Answer 1

5
$\begingroup$

There are 2 velocities possible - linear and angular.

Here is a possibility how you can get the linear velocity.

First - create a rigid body simulation. In my attached example I let a sphere fall down to a plane. Then select the sphere and go to Object -> Rigid Bodies -> bake keyframes.

This is needed and makes Blender to save the locations of the sphere.

Then run this script:

    # before use you have to do: Object -> Rigid Body -> Bake to keyframes

import bpy
from mathutils import Vector

def distance_line(point1: Vector, point2: Vector):
    return (point2 - point1).length

object = bpy.data.objects["Sphere"]

bpy.context.scene.frame_set(1)
prev_location = object.location.copy()

for frame in range(1, 50, 1):

    bpy.context.scene.frame_set(frame)
    new_location = object.location
    
    distance = distance_line(new_location, prev_location)
    distance_vec = new_location - prev_location

    time = frame / 50
    velocity = distance_vec / time
    speed = distance / time
    print("frame", frame, "speed = ", speed, "velocity = ", velocity)
    prev_location = new_location.copy()

frame 1 speed =  0.0 velocity =  <Vector (0.0000, 0.0000, 0.0000)>
frame 2 speed =  0.23405551788968906 velocity =  <Vector (0.0000, 0.0000, -0.2341)>
frame 3 speed =  0.43941338677345454 velocity =  <Vector (0.0000, 0.0000, -0.4394)>
frame 4 speed =  0.5417287333633571 velocity =  <Vector (0.0000, 0.0000, -0.5417)>
frame 5 speed =  0.6028270774994956 velocity =  <Vector (0.0000, 0.0000, -0.6028)>
frame 6 speed =  0.6433208687131368 velocity =  <Vector (0.0000, 0.0000, -0.6433)>
frame 7 speed =  0.6720406668526785 velocity =  <Vector (0.0000, 0.0000, -0.6720)>
frame 8 speed =  0.6933987244921247 velocity =  <Vector (0.0000, 0.0000, -0.6934)>
frame 9 speed =  0.7098515774669812 velocity =  <Vector (0.0000, 0.0000, -0.7099)>
frame 10 speed =  0.7228732105570549 velocity =  <Vector (0.0000, 0.0000, -0.7229)>
frame 11 speed =  0.733395053418168 velocity =  <Vector (0.0000, 0.0000, -0.7334)>
frame 12 speed =  0.7420480364275706 velocity =  <Vector (0.0000, 0.0000, -0.7420)>
frame 13 speed =  0.7492560713914638 velocity =  <Vector (0.0000, 0.0000, -0.7493)>
frame 14 speed =  0.7553347503142805 velocity =  <Vector (0.0000, 0.0000, -0.7553)>
frame 15 speed =  0.7605091772460675 velocity =  <Vector (0.0000, 0.0000, -0.7605)>
frame 16 speed =  0.764947394702861 velocity =  <Vector (0.0000, 0.0000, -0.7649)>
frame 17 speed =  0.7687800204009462 velocity =  <Vector (0.0000, 0.0000, -0.7688)>
frame 18 speed =  0.7721099531842459 velocity =  <Vector (0.0000, 0.0000, -0.7721)>
frame 19 speed =  0.7750134781744401 velocity =  <Vector (0.0000, 0.0000, -0.7750)>
frame 20 speed =  0.7775571987736258 velocity =  <Vector (0.0000, 0.0000, -0.7776)>
frame 21 speed =  0.7797919613901602 velocity =  <Vector (0.0000, 0.0000, -0.7798)>
frame 22 speed =  0.78175962590308 velocity =  <Vector (0.0000, 0.0000, -0.7818)>
frame 23 speed =  0.6596611896169278 velocity =  <Vector (0.0000, 0.0000, -0.6597)>
frame 24 speed =  0.4006048084511261 velocity =  <Vector (0.0000, 0.0000, 0.4006)>
frame 25 speed =  0.3498907076119587 velocity =  <Vector (0.0000, 0.0000, 0.3499)>
frame 26 speed =  0.30313409275379927 velocity =  <Vector (0.0000, 0.0000, 0.3031)>
frame 27 speed =  0.25989567186529 velocity =  <Vector (0.0000, 0.0000, 0.2599)>
frame 28 speed =  0.21979808994738081 velocity =  <Vector (0.0000, 0.0000, 0.2198)>
frame 29 speed =  0.18251641073621594 velocity =  <Vector (0.0000, 0.0000, 0.1825)>
frame 30 speed =  0.14776905415265368 velocity =  <Vector (0.0000, 0.0000, 0.1478)>
frame 31 speed =  0.11531056673818194 velocity =  <Vector (0.0000, 0.0000, 0.1153)>
frame 32 speed =  0.08492618714236397 velocity =  <Vector (0.0000, 0.0000, 0.0849)>
frame 33 speed =  0.05642753543126842 velocity =  <Vector (0.0000, 0.0000, 0.0564)>
frame 34 speed =  0.029648051011661188 velocity =  <Vector (0.0000, 0.0000, 0.0296)>
frame 35 speed =  0.004440035138811384 velocity =  <Vector (0.0000, 0.0000, 0.0044)>
frame 36 speed =  0.019326806042889423 velocity =  <Vector (0.0000, 0.0000, -0.0193)>
frame 37 speed =  0.041769969002514604 velocity =  <Vector (0.0000, 0.0000, -0.0418)>
frame 38 speed =  0.0629942652418847 velocity =  <Vector (0.0000, 0.0000, -0.0630)>
frame 39 speed =  0.08309284741136086 velocity =  <Vector (0.0000, 0.0000, -0.0831)>
frame 40 speed =  0.10215073734155207 velocity =  <Vector (0.0000, 0.0000, -0.1022)>
frame 41 speed =  0.12024379264552598 velocity =  <Vector (0.0000, 0.0000, -0.1202)>
frame 42 speed =  0.13744107480097162 velocity =  <Vector (0.0000, 0.0000, -0.1374)>
frame 43 speed =  0.1538053461127377 velocity =  <Vector (0.0000, 0.0000, -0.1538)>
frame 44 speed =  0.16939314892522045 velocity =  <Vector (0.0000, 0.0000, -0.1694)>
frame 45 speed =  0.1842565017311837 velocity =  <Vector (0.0000, 0.0000, -0.1843)>
frame 46 speed =  0.19844265523380794 velocity =  <Vector (0.0000, 0.0000, -0.1984)>
frame 47 speed =  0.0665110480737829 velocity =  <Vector (0.0000, 0.0000, -0.0665)>
frame 48 speed =  0.13075570077899304 velocity =  <Vector (0.0000, 0.0000, 0.1308)>
frame 49 speed =  0.11050409327822751 velocity =  <Vector (0.0000, 0.0000, 0.1105)>

be aware that the result maybe more accurate if you slow down your animation.

$\endgroup$
4
  • 1
    $\begingroup$ it's finaly working. Thank you very much i realy appreciate :D $\endgroup$ Commented Aug 31, 2021 at 14:32
  • 1
    $\begingroup$ my pleasure!!!! $\endgroup$
    – user131425
    Commented Aug 31, 2021 at 15:08
  • $\begingroup$ This answer doesn't work for getting velocity of a rigidbody. Sure it may work for non-rigidbody objects but i also kinda need it for rigidbodies, anyone knows? $\endgroup$
    – DageLV
    Commented Jan 13 at 13:15
  • $\begingroup$ did you read the answer completely? it says: bake keyframes. Did u do that? if yes, pls provide your blend file in which it doesn't work. thx. $\endgroup$
    – user131425
    Commented Jan 14 at 14:22

You must log in to answer this question.

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