As question states, I want to "extract" global coordinates of 3D point that generated information in a specific pixel in camera view(rendered image).

  1. I have a pixel coordinate in a rendered image as (X,Y)<=> (point P1)
  2. I have (point O) as origin of ray

How can I get (X,Y,Z) in global coordinates of (point P2) from this setup?

enter image description here

  • $\begingroup$ Related in that it shoots a resolution_x by resolution_y grid of rays from camera onto object of focus, before reflecting onto mirror object. $\endgroup$ – batFINGER Aug 2 '18 at 14:45

You obtain the rays like this:

Casting rays from the camera into the scene for each pixel (2nd part of answer - F12 solution)

Then you build a BVH tree from your mesh:

import bpy
from mathutils.bvhtree import BVHTree

bvhtree = BVHTree.FromObject(bpy.context.active_object, bpy.context.scene)

And you intersect this tree with the ray:

location, normal, index, dist = bvhtree.ray_cast(ray_origin, ray_direction)

You are interested in the location. The index and normal refer to the first polygon it intersected. If there's no hit, it returns (None, None, None, None) tuple.

It's wise to cache the bvhtree for performance reasons, its building is costly, but speeds up ray casting. The tree needs to be rebuild only when the object changes, this you get from the object.is_updated and object.is_updated_data flags.

|improve this answer|||||
  • $\begingroup$ So basicaly with def 2d_to_ray(context, point_px) I get ray_origin, ray_direction as ray_origin, ray_vector and then by running ray_cast from bvhtree I get in the location variable (X,Y and Z) of point of interest? $\endgroup$ – Dan Butmalai Aug 2 '18 at 15:05
  • $\begingroup$ @DanButmalai Yes. The 2d_to_ray() function is for getting viewport 2d pixels in 3d space when you are looking through the camera. If you only need to cast your rays from render pixels (this what I assume from your picture) and not viewport screen pixels you can skip that part as discussed in bottom of that answer. $\endgroup$ – Jaroslav Jerryno Novotny Aug 2 '18 at 16:31
  • $\begingroup$ Thanks for support provided, from all sources and discussions from other threads I managed to get a script that performs as required. $\endgroup$ – Dan Butmalai Aug 2 '18 at 18:39
  • $\begingroup$ Thanks for the answer! Do you know in which space normal is specified? $\endgroup$ – Sibbs Gambling Jul 2 '19 at 0:22
  • $\begingroup$ For the sake of future readers: I just verified with a plane that normal is in fact in the object's local space, which makes sense, since location is also in the object's local space. $\endgroup$ – Sibbs Gambling Jul 2 '19 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.