Page MenuHome

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

Description

Blender Version
2.73RC

Short description of error
Hi. I asked a question about Viewport position and direction here http://blender.stackexchange.com/questions/22963/viewport-position-and-direction .
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: http://youtu.be/Ia7wjCMafqA
And here how it works in Perspective: http://youtu.be/tL9IXVDs7Cg

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.

Thanks.

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:
http://www.blender.org/api/blender_python_api_2_73_release/bpy_extras.view3d_utils.html#module-bpy_extras.view3d_utils

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:
http://blenderartists.org/forum/showthread.php?355154-Addon-Retopo-MT&p=2793598&viewfull=1#post2793598

And here is the code they did:
https://github.com/CGCookie/retopology-lib/blob/087bc8a40a872943c7b1d9af9a9a4a0a2055c70a/common_utilities.py#L343

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

Re:

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! http://youtu.be/2y2Qz8O2oyg
Origin is great now!

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