1
$\begingroup$

I'm implementing a simple script to calculate the location of an object (which is a 2D plane) in the rendered image:

import bpy
import bpy_extras

context = bpy.context
scene = context.scene

camera = scene.camera
render = scene.render

def image_location_of(object):
    width = render.resolution_x * render.resolution_percentage / 100.0
    height = render.resolution_y * render.resolution_percentage / 100.0

    projected_location = bpy_extras.object_utils.world_to_camera_view(
        scene=scene,
        obj=camera,
        coord=object.location
    )

    return (
        projected_location.x * width,
        projected_location.y * height
    )

Basically, the script calls world_to_camera_view to convert the global location of the object (i.e. object.location) to the location under the camera's view, then scales this location with the width and the height of the image.

Unfortunately, the returned (x,y) is very wrong (in many times it is even bigger than the size of the image). Did I do anything wrong?

$\endgroup$
4
  • 1
    $\begingroup$ Hello ! Are you sure the object's origin is located in the view frustum of the camera ? $\endgroup$
    – Gorgious
    Commented Mar 11 at 13:35
  • $\begingroup$ Thank you. Yes, I'm sure. I've implemented a piece of code to move the object to the view frustum (if it wasn't), and I can even observe that the object appears in the rendered image. $\endgroup$ Commented Mar 12 at 2:28
  • $\begingroup$ The object might appear in the rendered image but its origin might be far from its geometry. Here you are testing the object location which is its origin $\endgroup$
    – Gorgious
    Commented Mar 12 at 14:33
  • $\begingroup$ Cmon bro, there is node which gives you position in window space its called "texture coordinate". And you can get position for every vertex of mesh. But its limited to shading editor. So maybe im actually wrong $\endgroup$ Commented Mar 13 at 0:18

0

You must log in to answer this question.

Browse other questions tagged .