0
$\begingroup$

I have been thinking about this for a long time. I decided that I would try finding a workaround or jump on the python train. But unfortunately I still don't have solution and I am at my wit's end. Here's what I could gather:-

Setting border render coordinates using python:-

bpy.ops.view3d.render_border(xmin=188, xmax=423, ymin=271, ymax=489)

Setting Render/Camera resolution using python:-

for scene in bpy.data.scenes:
  scene.render.resolution_x = 1920
  scene.render.resolution_y = 1080

My main objective is to match the camera/render resolution to the Border render coordinates so that the camera fits to the drawn render border. I want to do this mainly because it's easier and saves a lot of time than manually adjusting the resolution and the location of the camera.

Question

How do fit the camera/resolution size to the render border through python ?

$\endgroup$
3
  • $\begingroup$ What do you exactly want, as I understand it is pressing the crop button (next to the render border) In python: use_crop_to_border. Changing camera position is tricky as it will lead to different results. It might be possible by changing the offset of the camera. $\endgroup$
    – J. Bakker
    Commented Jan 15, 2018 at 8:50
  • $\begingroup$ I want the camera size/render resolution size to fit the drawn Render border. $\endgroup$
    – Retrax
    Commented Jan 15, 2018 at 20:27
  • $\begingroup$ Ray Mairlot made an addon (paid) that should do what you want. $\endgroup$
    – David
    Commented Feb 15, 2018 at 3:31

1 Answer 1

1
$\begingroup$

For perspective camera's you can use this. Original code from https://github.com/ChameleonScales/Camera-Cropper-Splitter just cleaned up a bit.

It uses the shift property of the camera to match the original camera image. For some cases this is useful, but other cases it ain't. Then you really need to move the camera.

scene = bpy.data.scenes['Scene']
render = scene.render
camera = scene.camera

orig_res_x = render.resolution_x
orig_res_y = render.resolution_y

border_width = render.border_max_x-render.border_min_x
border_height = render.border_max_y-render.border_min_y
border_left = render.border_max_x+render.border_min_x
border_top = render.border_max_y+render.border_min_y
border_res_x = orig_res_x*border_width
border_res_y = orig_res_y*border_height

# ▼ Focal length change ▼ 
if (orig_res_x>orig_res_y):
    if border_res_x > border_res_y:
        camera.data.lens /= border_width
    else:
        camera.data.lens *= orig_res_x/border_res_y
else:
    if border_res_x > border_res_y:
        camera.data.lens *= orig_res_y/border_res_x
    else :
        camera.data.lens /= border_width

# ▼ Resolution change ▼ 
render.resolution_x = round(border_res_x)
render.resolution_y = round(border_res_y)

# ▼ Shift X ▼ 
if border_res_x>border_res_y:
    camera.data.shift_x = (0.5 * (border_top - 1)) / border_width
else :
    camera.data.shift_x = (0.5 * border_res_x * (border_left - 1))/(border_res_y * border_width)

# ▼ Shift Y ▼ 
if border_res_x>border_res_y:
    camera.data.shift_y = (0.5 * border_res_y * (border_left - 1))/(border_res_x * border_height)
else :
    camera.data.shift_y = (0.5 * (border_top - 1)) / border_height
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .