Blender Python script: how to position a plane to apparently fit camera frame

In the past days I faced another challenge while working to my Blender 3D experiments.
An idea came to me when I asked myself: « Which is the optimal resolution of a texture, depending on the distance of an object from the camera? » and I started to split the problem in smaller tasks.

The first answer was: « Assume that we are watching a square plane, which dimensions apparently fit the frame of the camera. And assume that the texture perfectly fits the borders of the plane (a common situation, obtained by executing “Unwrap” command, in “Editing” mode). The condition, required to obtain such result, is to place the plane at a “special” distance from the camera (distance that I will call Critical Distance). At that distance the plane texture can have exactly the same resolution of the final render frame. Beyond the Critical Distance, the texture resolution can be lower. At distances lower then the CD, texture must have higher resolution then the render frame. ».

By default, Blender 3D render frame output resolution is configured at 1920 * 1080 px. Therefore, the texture of our plane can be (for example) the nearest power of 2 square: 2048 * 2048 px.

So, my first challenge was to find the Critical Distance and (why not) to place the plane at that distance in front of the camera.

Critical Distance is obtained by de formula:

 

Where alpha is the FOV of the camera along X direction and l is the width of the object.

So I coded a simple add-on for Blender (still early alpha: to run it use ALT-P in the Text Editor).

 

This is the Blender 3D file: texture_size_&_camera_distance.blend.