finding the middle point of a Vector

While it is not often required, it can be very useful to know how to compute the middle point between two 3D Vectors. Most code that generates shapes can work with absolute positions from the previous step, but more complicated structures like a plant need a way to calculate the middle point between to 3D positions (Vectors).
import bpy
from mathutils import Vector

def calc_midpoint( a, b ):
    a = Vector(a)
    b = Vector(b)
    dif = b-a
    return a + (dif*0.5)

x = [1,2,3]
y = [3,2,1]
print(calc_midpoint(x,y))

x = bpy.data.objects['Camera'].location
y = bpy.data.objects['Cube'].location

middle = calc_midpoint(x,y)

bpy.ops.mesh.primitive_monkey_add(location=middle)

Comments

Popular Posts