Page MenuHome

Python: "view3d_utils.region_2d_to_origin_3d(region, rv3d, (0,0))" does not work correctly in Ortho
Closed, ResolvedPublic


Blender Version

Short description of error
Hi. I asked a question about Viewport position and direction here .
I was recommended to use "view3d_utils.region_2d_to_origin_3d(region, rv3d, (0,0))" to get position of the viewport.
But "view3d_utils.region_2d_to_origin_3d(region, rv3d, (0,0))" does not work correctly in Ortho.
In OrthoView it shows the same values of "RegionView3D.view_location" (view target of viewport).

Here is my videotest:
And here how it works in Perspective:

Is it possible to get Viewport position in Ortho view correctly?
This is very important for scripts developers. We need to get Viewport position in Ortho correctly.

If you need any other test examples just usk.


Event Timeline

paul geraskin (mifth) raised the priority of this task from to Needs Triage by Developer.
paul geraskin (mifth) updated the task description. (Show Details)
paul geraskin (mifth) set Type to Bug.

This is not a bug, view matrices in ortho & perspective behave different.

For functions which work with both ortho & perspective views, see:

paul geraskin (mifth) renamed this task from Python: "rv3d.view_matrix.inverted().translation" does not work correctly in Ortho to Python: "view3d_utils.region_2d_to_origin_3d(region, rv3d, (0,0))" does not work correctly in Ortho.Jan 11 2015, 2:30 PM
paul geraskin (mifth) updated the task description. (Show Details)

The code of region_2d_to_origin_3d is located here.

In case one is using the coordinates of the center of the window region,

region.width / 2.0 and
region.height / 2.0

dx and dy are both zero and the function returns a copy of view_matrix.inverted().translation as well.

By the way "zooming" in and out should only change the scaling factor not the view origin.

The view origin is relevant for clipping / visibility test.

There is an issue where ortho views dont have any good way to choose how far back their point of origin is.

Julian Eisel (Severin) lowered the priority of this task from Needs Triage by Developer to Normal.Jan 11 2015, 5:32 PM

@Gyro Gearloose (pink.vertex)
I changed "rv3d.view_matrix.inverted().translation" method to a different one. As it makes position a bit not correct. The position becomes behind the camera (in Perspective mode). Possibly, because of cameraClipping. But it's a bit behind.

I changed this issue to "view3d_utils.region_2d_to_origin_3d(region, rv3d, (0,0))" as it's documented and it makes the position correctly (in Perspective mode). Also, this method always make the same position even if coordinates are different (in Perspective mode). But sure, you are right. It's better to use "region.width / 2.0" and "region.height / 2.0".

Corrected: view3d_utils.region_2d_to_origin_3d(region, rv3d, (region.width/2.0, region.height/2.0))

@Campbell Barton (campbellbarton)
I also noticed one thing about OrthoDirection. Direction is always the same in this method (in Ortho mode):
"view_vector_mouse = view3d_utils.region_2d_to_vector_3d(region, rv3d, (coord.x, coord.y))"
if there will be any coords - the direction will be always the same. But in Perspective mode the direction is always different depending on coords of a mouse. Is it ok for Orthographic mode?
I mean, if you will add the corrected OrthoPosition then should we also add corrected OrthoDirection?

Just for notification and not to forget:
CGCookie team has fixed the issue with Ortho. it was discussed here:

And here is the code they did:

@Campbell Barton (campbellbarton) said in IRC that he would make it a bit in a different way. This code is reference only.


view3d_utils.region_2d_to_origin_3d(region, rv3d, (0,0))

This is not documented anywhere in Blender.

Docs infact say:

:arg coord: 2d coordinates relative to the region;
   (event.mouse_region_x, event.mouse_region_y) for example.

The only issue is that the point isn't projected backwards to the limit of the far clipping plane.
This is because the value is often very large (far clip of 5000+ is common),

So a way to optionally allow using the far clip would be useful. (or % of it)

Thank you a lot! THanks thanks thanks!!!!!
Super cool!!! Yahoo!

i have just tested it and Ortho works great!
Origin is great now!

Thanks again. Now we (users) can make our tools with cool picking.