1
$\begingroup$

I'm trying to get the rendered position of bones, but it seems that they are off by some amount and I don't find the source of the offset. This is the code I'm using:

import bpy
from bpy_extras.object_utils import world_to_camera_view

hand_pose = bpy.data.objects["Armature"].pose;

render_scale=bpy.context.scene.render.resolution_percentage/100;

height = 512
width = 512

bpy.context.scene.render.resolution_x = width
bpy.context.scene.render.resolution_y = height
bpy.context.scene.render.resolution_percentage = 100

render_size = ( 
    bpy.context.scene.render.resolution_x * render_scale,
    bpy.context.scene.render.resolution_y * render_scale
    )

bpy.context.scene.render.filepath = 'sample.png'
bpy.ops.render.render(write_still=True)

fingers=[ 'finger_pinky.03.L', 'finger_ring.03.L', 'finger_middle.03.L', 'finger_index.03.L', 'thumb.03.L' ]

for i, finger in enumerate(fingers):
    pos = world_to_camera_view(bpy.context.scene,     bpy.context.scene.camera, hand_pose.bones[finger].head)
    print(finger + ' position is ', render_size[0]*pos.x, height-render_size[1]*pos.y-1)

for which I get an output like this:

finger_pinky.03.L position is  255.89732360839844 297.16163635253906
finger_ring.03.L position is  249.3601531982422 292.98570251464844
finger_middle.03.L position is  246.95179748535156 286.85406494140625
finger_index.03.L position is  249.28578186035156 279.54920959472656
thumb.03.L position is  266.2782287597656 270.7700500488281

The bones correspond to the tip of each finger. The obtained positions are not in the right place, as shown in the following figure:

Projection of bones

Can anyone give a hint on what I'm doing wrong?

Hernan

$\endgroup$

1 Answer 1

2
$\begingroup$

You should multiple the coordinate by the matrix_world before passing to world_to_camera_view. The coordinates are not modified when you rotate, translate, scale an object. Instead those transformations are placed in the matrix_world.

for obj in bpy.data.objects:
  if obj.type == "MESH":
    for v in obj.data.vertices:
      co = obj.matrix_world * Vector((v.co.x, v.co.y, v.co.z, 1))
      co = world_to_camera_view(scene, camera, co)
$\endgroup$

You must log in to answer this question.

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