1
$\begingroup$

I want to get the image coordinates of vertices in my rendered image outputs. I'm trying to use the world_to_camera_view function but the results don't seem correct to me in simple tests, so I'm probably misunderstanding how to use it.

My scene has an orthographic camera, with two cubes, one in the upper-left corner of its FoV, the other cube in the lower-right: example

I made a python script which I run from command line, i.e. like: /blender -b /test.blend -P /test.py It iterates all objects and their vertices and dumps the result of world_to_camera_view for each:

import bpy, sys, os, getopt
from bpy_extras.object_utils import world_to_camera_view

scene = bpy.context.scene
cam = bpy.data.objects['Camera'] 
...
for obj in allObjects:
    print("Working on object: %s" % obj.name)
    for vert in obj.data.vertices: 
        # local to global coordinates
        co = vert.co @ obj.matrix_world
        coords2d = world_to_camera_view(scene, cam, co)
        print("coords2d: {},{}".format(coords2d.x, coords2d.y))

Since the two cubes are close to the edges of the rendered image, I would expect their computed locations on the x axis for example to be close to the min/max. The purple cube's x coordinates are all roughly about 0.4 or 0.5, I would expect them to be closer to 0:

Working on object: PurpleCube
coords2d: 0.5826725363731384,0.6069368124008179
coords2d: 0.5826725363731384,0.4183192551136017
coords2d: 0.4964485764503479,0.542599081993103
coords2d: 0.4964485764503479,0.35398149490356445
coords2d: 0.5013962984085083,0.6751911640167236
coords2d: 0.5013962984085083,0.48657360672950745
coords2d: 0.4151723384857178,0.6108534336090088
coords2d: 0.4151723384857178,0.4222358465194702

Am I misunderstanding how world_to_camera_view works?

$\endgroup$

1 Answer 1

0
$\begingroup$

You just reversed the matrix multiplication. It should be:

co = obj.matrix_world @ vert.co 
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .