# How to use a series of points in a 2D image to place objects from the perspective of the camera using Python

I'm trying to randomly place many dense objects onto a plane centered at the origin (0,0,0) without overlap. I figured out a way to do this using 3D mesh intersection but the time it takes to conduct placement with lots of object instances is undesirable. I have a camera that points directly downward at the plane (0,0,Z) on which the objects will be placed. Since the camera resolution is known, I had an idea to create a 2D list of pixels in the rendered camera image and use 2D collision algorithms with the bounds of my objects as seen from the camera to place my objects instead. I could effectively calculate object positions in 2D and then use the list of center points and rotations to place the objects in Blender using bpy. For my purposes I am fine with all of the objects resting flat on the plane.

The problem is I'm not sure how to go about translating my object bounds from 3D to 2D, and then translating my calculated origin points from 2D back to 3D. I know what the exact dimensions of my objects are in Blender units but I don't know how to translate that to measurements in rendered image pixels. I also don't know how to effectively map my 2D points onto the plane in Blender from the camera's perspective. I'm fairly new to Blender and don't know too much about matrix math, but I imagine there must be a way to do this using view3d_utils.region_3d_to_location_2d and view3d_utils.region_2d_to_location_3d. Any assistance in how to get something like this set up would be greatly appreciated.

• I suspect you’re actually making this way too hard, but without further information on the subject, I don’t know for sure. I do suggest you use an orthographic camera. Mar 16 at 15:27
• I'm definitely open to suggestions for alternative methods to accomplish this. My current method involves brute force checking every combination of objects with BVHTree overlap which works, but is very very slow. Doing the calculation in 2D was the first thing I thought of to simplify the algorithm but this isn't my area of expertise so there may be something better. If it simplifies the problem at all we can assume that the objects are rectangular prisms which can be rotated along the Z axis and the viewport of the camera perfectly encapsulates the plane. Mar 16 at 15:59
• bpy_extras.object_utils.world_to_camera_view can help you? I can suggest an inverse function of this if needed. Mar 17 at 6:51