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)))