I can’t find a way to get the direction vector for a camera in the API. Do I have to figure it out from the object matrix? Same goes for the up vector.
Window.GetViewMatrix() returns the view matrix. There are some other view goodies in that module.
Thanks, I’ll give it a go.
For camera objects, the direction vector is the Z axis. so, you’d have to get the Z-axis from the object, which I think is the third row of a 3x3 matrix (I’m not sure though).
joeedh
For camera objects, the direction vector is the Z axis. so, you’d have to get the Z-axis from the object, which I think is the third row of a 3x3 matrix (I’m not sure though).
joeedh[/quote]
Think this will work
mat= camob.matrixWorld
dir= Mathutils.Vector(0,0,1,1)*mat
dir.resize3D()
dir.normalize() # optonal
# dir is the camera direction
Is there an easy way to get the perspective “camera” / View direction ?
I’m trying to project rays from the view TM, but can’t figure out how. I thought I had it. But is not right. Using the view spaces I think and getting 3x3 matrix and multiply by vector of 0,0,1. So Z is pointing / aligned from view into scene.
Thanks !
I’ve been trying to figure this out yet, and think I’m close maybe, but still can’t get it working right…
Seems like if I use this code, I can create a Camera/Set an object to be aligned to the Perspective TM/View, but the Project is not going in the right direction/using the proper vector.
How can I get the Local Z Dir from the Matrix?
I don’t even really want/need to create a Camera, but was using it as a test.
Mainly just trying to Raycast from the ViewTM, onto another Mesh. Raycast from VIew.
cam = bpy.data.cameras.new("Camera_ShrinkWrapper")
cam_ob = bpy.data.objects.new("Camera_ShrinkWrapper", cam)
bpy.context.scene.objects.link(cam_ob)
cam_ob.matrix_world = area.spaces.active.region_3d.view_matrix.inverted()
mat_camera = cam_ob.matrix_world * mathutils.Vector((0.0, 0.0, 1.0)) #area.spaces.active.region_3d.view_matrix.to_3x3()
trans_world = mat_camera
trans_world.normalize()
Edit: Lol… and just like that, I got it working…! I looked at some of my other code I had/forgot about.
Basically just had to convert to 3x3 seems like, and still multiply in the Vector.
vec = mathutils.Vector((0.0, 0.0, 1.0))
trans_world = (area.spaces.active.region_3d.view_matrix.inverted()).to_3x3() * vec
trans_world.normalize()
vec = mathutils.Vector((0.0, 0.0, 1.0))
trans_world = (area.spaces.active.region_3d.view_matrix.inverted()).to_3x3() * vec
trans_world.normalize()
I tried this code in blender 2.8 but I don’t fully understand the second line from the quote.
If I try bpy.context.area.spaces.active.(There is nothing here in blender 2.8)
My main goal is to get the perspective view location.
It has probably changed in 2.8. The spaces / viewport stuff. I haven’t tried to convert anything to 2.8 yet. So I may hit that as well. Maybe I’ll try this weekend if the weather ends up bad.