Reputation: 660
After a good deal of work, I have developed the following code in Python to plot a vector [In this case (2,2,2)] so that it points in the way you would expect from the origin. It took me some time to gather what the three rotation parameters meant in terms of roll, pitch and yaw. You may need to set Euler XYZ.
I have used a long thin cylinder to be my vector, which suits my purpose and fits with my thin experience of blender. This code plots a vector with an Arrow on it (a cone) half way along and suits my purpose quite well but is somewhat of a bodge. I works for most vectors but fails when x<0 and y>0 and z>0
import bpy
import math
from math import *
x=-5
y=-10
z=12
yParameter=-1.0
if y < 0:
if x < 0:
yParameter = 1.0
#print ("y para is ",yParameter
for i in range (0,1):
length=sqrt(z*z+y*y+x*x)
#Create a vector at correct orientation at the origin
bpy.ops.mesh.primitive_cylinder_add(vertices=16, radius=0.04, depth=length, end_fill_type='NGON', view_align=False, enter_editmode=False, location=(0,0,0),rotation=(-acos(z/sqrt(x*x+y*y+z*z)),0,yParameter*acos(y/sqrt(x*x+y*y))))
bpy.ops.transform.translate(value=(x/2, y/2, z/2))
bpy.ops.mesh.primitive_cone_add(vertices=32, radius1=0.1, radius2=0, depth=0.4, end_fill_type='NGON', view_align=False, enter_editmode=False, location=(0,0,0), rotation=(-acos(z/sqrt(x*x+y*y+z*z)),0,yParameter*acos(y/sqrt(x*x+y*y))))
bpy.ops.transform.translate(value=(x/2, y/2, z/2))
I feel certain that the huge API in vectors and matrix manipulation should make this job easier but I am struggling with finding how to do it other than with this self-developed cartesian work.
Can any one point me to an understandable code snippet or maybe a tutorial on how to manipulate vectors (in the mathematical sense) within blender python
I find that the blender API is pretty clear on the names of parameters and how to code them but I can find little or nothing on what the parameters actually mean.
Upvotes: 4
Views: 2650
Reputation: 3027
This will create you a cylinder with (0,0,0) as one end and (x,y,z) as the other:
def createVectorLikeThing(x,y,z):
v = Vector((x,y,z))
up = Vector((0,0,1))
if v!=-up:
rot = up.rotation_difference(v)
else:
rot = Quaternion((1,0,0),pi)
bpy.ops.mesh.primitive_cylinder_add(vertices=16, radius=0.01, depth=v.length, end_fill_type='NGON', view_align=False, enter_editmode=True)
bpy.ops.transform.translate(value=(0,0,v.length/2))
bpy.ops.object.editmode_toggle()
bpy.ops.transform.rotate(value=(rot.angle,), axis=rot.axis)
The code works with blender 2.63 but not with 2.65.
For 2.65 change the last line to:
bpy.ops.transform.rotate(value=rot.angle, axis=rot.axis)
Upvotes: 2