How to convert 3d view into camera coordinates?


I’m trying to make camera based on current 3d view coordinates but I have no idea how to transform matrix returned by getPerspMatrix to something like matrixWorld I get from camera object.
I know that there have to be such way because I can do this in blender with ctrl+alt+0, but how to do this in python?

Heya, I dont know what you’re trying to achieve but if your cam object is ob_Cam, then you directly set the cam matrix to the 3D-view matrix (not the Persp matrix) by this

from Blender import *
import bpy

sce = Scene.GetCurrent()
ob_Cam = Object.Get("Camera")
mat_View = Window.GetViewMatrix()

I think that you’d need to re-direct the cam to “see” something reasonable cause after the above the cam will be directed to where the Window view vector points.


I want to create new camera and set it in the same position as 3d view. Like ctrl+alt+0 in blender.
I’ve tried your code, but it creates camera in different place.

Well, I think it places the cam in accordance with the mat_View matrix. Apparently, this is NOT equal to the viewer’s viewing point, which is - I guess - what you’re trying to achieve… Just wanna mention that I’ve been interested in that task too… Still interested cause I dont have a solution - to determine cam position + direction given a pic or a clip/video taken from THAT position… My findings are that it is NOT possible to determine cam position based on a single pic cause pics are proportional and you can only determine the direction (vector). Cam position can be anywhere on that vector backwards… Should you have a second sufficiently different pic of the same scene/objects, you may be able to determine your cam position as well. This is similar to navigating ships in oceans by looking at stars in old times, ok? :wink:

But if you press ctrl+alt+0 then active blender camera is going to your current 3d view position. So it’s possible. The only problem is with transformation matrix of 3d view. It’s position and rotations are defined and 3d view normally has 35mm lens so it have to be somewhere in world space. Of course if it’s perspective view.

May I ask you to describe what exactly happens when you press CTRL-ALT-0? At me doesnt happen anything like like the cam moving anywhere… :eek:

Hum, well… now I know what you mean! :wink: In a default scene with a cube, cam and a lamp I first press NumPad1 to go into Front View, then I press CTRL-ALT-0 and the cam goes to position (2.056,-17.056,0.000)… Only Z coordinate have something in common with the front view, right? I was only able to notice that THIS point (after pressing CTRL-ALT-0) depends on the Zoom… i.e. when you Zoom the view, the point is closer to the Cube. :eek:

Of course it is closer, because by default 3d view has the same angle of view as 35mm lens :slight_smile:

does that do what you want?

This invert() property is interesting indeed. It sets the cam (in my case - see my previous posting) at location (2.056,-0.364,0.000) which differs only on Y when compared with the result of CTRL-ALT-0… Rotation is ok, but the coordinate Y?? What is invert() actually doing? Is it producing such a matrix M2 on the basis of the original M1 so that M1*M2 = -1?

Thanks, it works perfect :slight_smile: