# Convert 3d coordinates to 2d.

Hello. Is there a built-in panda function to convert 3d coordinates to 2d? Maybe use it to attach a DirectLabel to a 3d model… Any tips would be appreciated. Thanks…

Here’s the solution to your issue- a word of advice though- you may want to search for keywords about your problems as this specific issue has been dealt with quite a bit.

``````def map3dToAspect2d(self, node, point): # it converts between objects on the screen and their 3D location for comparasion.
"""Maps the indicated 3-d point (a Point3), which is relative to
the indicated NodePath, to the corresponding point in the aspect2d
scene graph. Returns the corresponding Point3 in aspect2d.
Returns None if the point is not onscreen. """

# Convert the point to the 3-d space of the camera
p3 = base.cam.getRelativePoint(node, point)

# Convert it through the lens to render2d coordinates
p2 = Point2()
if not base.camLens.project(p3, p2):
return None

r2d = Point3(p2[0], 0, p2[1])

# And then convert it to aspect2d coordinates
a2d = aspect2d.getRelativePoint(render2d, r2d)

return a2d	``````

I have found that the above doesn’t work well when the node is not parented to render itself, but to some other node. The following modification works well in any case:

``````def nodeCoordIn2d(nodePath):
coord3d = nodePath.getPos(base.cam)
coord2d = Point2()
base.camLens.project(coord3d, coord2d)
coordInRender2d = Point3(coord2d[0], 0, coord2d[1])
coordInAspect2d = aspect2d.getRelativePoint(render2d,
coordInRender2d)
return coordInAspect2d``````