Add driver to render resolution

Hi! I would like to add a driver to the render resolution, so that it changes based on a custom propriety of the active camera. I would drive only the Y resolution. Is it possible?

Thanks!

Not as a driver, maybe as a handler


import bpy


def my_handler(scene):
    print("Frame Change", scene.frame_current)
    cube = scene.objects.get("Cube")
    if cube:
        scene.render.resolution_y = cube["prop"]


bpy.app.handlers.frame_change_pre.append(my_handler)

You might want to investigate using pre_render as a handler instead. http://www.blender.org/documentation/blender_python_api_2_61_4/bpy.app.handlers.html?highlight=bpy.app.handlers#bpy.app.handlers.persistent

PS the code above looks for a custom prop named prop on the Cube, change Cube to Camera name and prop to property name to suit your needs.

PPS the active camera is scene.camera

Ok, I got it. I changed the code this way:

import bpy

def my_handler(scene):
    print("Frame Change", scene.frame_current)
    camera = bpy.context.scene.camera
    if camera:
        scene.render.resolution_y = scene.render.resolution_x * camera["Ratio"]

#bpy.app.handlers.frame_change_pre.append(my_handler)
bpy.app.handlers.scene_update_post.append(my_handler)

I changed the last line so now the resolution adapts every time the X resolution is changed. The active camera has an aspect ratio propriety. So now in the same scene I can have vertical frame, horizontal frames square frames etc. I just need to set the aspect ratio proriety of every camera. When it becomes active, the render resolution adapts.

Thankyou very much!

Is there a way to make this script start every time Blender starts?

The simplest way is to give it a .py extension (the text block name) and click the register checkbox on the text editor header. This will make it run in the current blend when opened.

Scene update runs a lot … will pay to remove the print statement so as not to blow out the system console.

Also, not that it really matters


camera = scene.camera

since scene is passed to the handler.

Ok, thankyou!
I realize now that a camera, when it is created, doesn’t have the ratio propriety. So I wonder if there is a way to make that, when I create a camera, it already has a custom propriety that I want…


import bpy


def my_handler(scene):
    #print("Frame Change", scene.frame_current)
    camera = scene.camera
    if camera:
        if "Ratio" not in camera.keys():
            camera["Ratio"] = 1.0
        scene.render.resolution_y = scene.render.resolution_x * camera["Ratio"]


bpy.app.handlers.scene_update_post.append(my_handler)

Change 1.0 to suite what would be the default value.

ok, this work very well. Thank you again.

Now, I want to bring this to a new level. I made the script so that, if the ratio is 1 or less, the x resolution sets the Y resolution, while if the ratio is greater than 1, the Y resolution sets the X resolution. But in this way, changing continuously between a camera with vertical frame and a camera with horizontal frame, makes the resolution reduce each time. So I need a resolution tweak the first time the script runs on the new active camera. I tryied to create a global boolean variable that works for this purpose. I post the code I wrote so you can understand what I mean.


import bpy

global changed 
changed = False

def my_handler(scene):
    camera = scene.camera
    if camera:
        if "Ratio" not in camera.keys():
            camera["Ratio"] = 0.666
        if camera["Ratio"] <= 1:
            if changed == False:
                scene.render.resolution_x = scene.render.resolution_x / camera["Ratio"]*2
                changed = True
            scene.render.resolution_y = scene.render.resolution_x * camera["Ratio"]
            
        else: 
            if changed == True:
                
                changed = False
            scene.render.resolution_x = scene.render.resolution_y / camera["Ratio"]

bpy.app.handlers.scene_update_post.append(my_handler)

This doesn’t work, and I think that I can’t use that kind of variable in that process. So I ask if there is a way to recognize the event of active camera change to perform the resolution tweak.

Thanks!