Skip to content

Instantly share code, notes, and snippets.

@Pullusb
Last active August 19, 2016 10:43
Show Gist options
  • Save Pullusb/f281824c0a15be91421c946381bcee2c to your computer and use it in GitHub Desktop.
Save Pullusb/f281824c0a15be91421c946381bcee2c to your computer and use it in GitHub Desktop.
Blender python exemple how to create a 90 degree vector from a current vector
import bpy
from bpy import context as C
from bpy import data as D
from mathutils import Vector
from math import sqrt
def VectorLength(size, A, B):
'''
Calculate the vector lenght
return the coefficient to multiply this vector
to have a vector of the size given in paramerter
'''
Vlength = sqrt((A[0] - B[0])**2 + (A[1] - B[1])**2 + (A[2] - B[2])**2)
return (size / Vlength)
#to test, create 3 object named as follow and place foo and bar in space
foo = D.objects['foo'].location
bar = D.objects['bar'].location
#middle location between 2 vector is calculated by adding the two vector and divide by two
mid = (foo + bar) / 2
#between = vector from foo to bar (invert the orperators to get negative vector)
between = foo - bar
#create a generic Up vector (on Y or Z)
up = Vector([0,1.0,0])
#the cross product return a 90 degree Vector
new = Vector.cross(up, between)
'''
####cross product also accessible throuh Numpy module:
###import numpy as np
###new = np.cross(up, between)
###new = Vector(new)
'''
#position of the point in space is found by adding the new vector to the mid location
#but the lenght is according to original foo->bar vector
perpendicular = mid + new
C.scene.cursor_location = perpendicular
#get the coefficient to multiply the new vector to get the same direction bu size 1
coeff = VectorLength(1, mid, perpendicular)
#print ('perp', perpendicular, '\ncoeff', coeff)
#position the point in space by adding the new vector multiplied by coeff value to get wanted lenght
C.scene.cursor_location = mid + (new * coeff)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment