I am trying to get the pixel coordinates of the object in the rendered image in blender using the How to find image coordinates of the rendered vertex? But I get the error AttributeError: 'Mesh' object has no attribute 'view_frame'

# Test the function using the active object (which must be a camera)
# and the 3D cursor as the location to find.
import bpy
import bpy_extras

scene = bpy.context.scene
obj = bpy.context.object
co = bpy.context.scene.cursor_location

co_2d = bpy_extras.object_utils.world_to_camera_view(scene, obj, co)
print("2D Coords:", co_2d)

Camera object

the argument obj must be a camera

>>> bpy_extras.object_utils.world_to_camera_view(
world_to_camera_view(scene, obj, coord)
Returns the camera space coords for a 3d point.
(also known as: normalized device coordinates - NDC).
Where (0, 0) is the bottom left and (1, 1)
is the top right of the camera frame.
values outside 0-1 are also supported.
A negative 'z' value means the point is behind the camera.
Takes shift-x/y, lens angle and sensor size into account
as well as perspective/ortho projections.
:arg scene: Scene to use for frame size.
:type scene: :class:`bpy.types.Scene`
:arg obj: Camera object.
:type obj: :class:`bpy.types.Object`
:arg coord: World space location.
:type coord: :class:`mathutils.Vector`
:return: a vector where X and Y map to the view plane and
   Z is the depth on the view axis.
:rtype: :class:`mathutils.Vector`

Running your question script with a mesh object as context will produce the error shown. Make sure obj.type == 'CAMERA'.


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.