0
$\begingroup$

I'm trying to use the following code to get 2D points out of Blender. Ultimately my goal is to generate HPGL for a pen plotter but for now I'll be happy with a printed list of coordinates.

Whenever the code calls world_to_camera_view I get the error Matrix object has no attribute z. This appears to be a problem inside the world_to_camera_view function?

I'm at a loss as to how to fix it and/or what data the function is actually expecting?

Thanks for the help!

import bpy  
from bpy_extras.object_utils import world_to_camera_view

scene = bpy.context.scene

# needed to rescale 2d coordinates
render = scene.render
res_x = render.resolution_x
res_y = render.resolution_y
cam = bpy.data.objects['Camera']

current_obj = bpy.context.active_object

scene = bpy.context.scene
camObject = bpy.data.objects [ 'Camera' ]

print("="*40) # printing marker  
for face in current_obj.data.polygons:  
    verts_in_face = face.vertices[:]  
    print("face index", face.index)  
    print("normal", face.normal)  

    coords_2d = [world_to_camera_view(scene, cam, coord) for coord in verts_in_face ]

    # 2d data printout:
    rnd = lambda i: round(i)

    print('x,y')
    for x, y, distance_to_lens in coords_2d:
        print("{},{}".format(rnd(res_x*x), rnd(res_y*y)))
$\endgroup$
1
$\begingroup$

Seems like you are passing a vertex though you named it coord.

verts = current_obj.data.vertices
coords_2d = [world_to_camera_view(scene, cam, verts[index].co) for index in face.vertices ]

face.vertices does not contain a collection of mesh vertices, but a list of vertex indices. You need to retrieve the actual vertex and use its coordinate (v.co).

$\endgroup$
  • $\begingroup$ This solved it for me, thanks for the code. When I did a debug print of the vertex it gave me a 3 value tuple so I assumed that was coordinates... $\endgroup$ – scottinthebooth Feb 17 '14 at 1:21

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.