# Add driver to render resolution

**URL:** <https://blenderartists.org/t/add-driver-to-render-resolution/601348>\
**Category:** Animation and Rigging\
**Created:** [February 13, 2014, 4:30am UTC](https://blenderartists.org/t/add-driver-to-render-resolution/601348 "2014-02-13T04:30:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lell](https://blenderartists.org/user_avatar/blenderartists.org/lell/32/13469_2.png) [@Lell](https://blenderartists.org/u/Lell)\
**Post date:** [February 13, 2014, 4:30am UTC](https://blenderartists.org/t/add-driver-to-render-resolution/601348/1 "2014-02-13T04:30:51Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![batFINGER](https://blenderartists.org/user_avatar/blenderartists.org/batfinger/32/5981_2.png) [@batFINGER](https://blenderartists.org/u/batFINGER)\
**Post date:** [February 13, 2014, 4:43am UTC](https://blenderartists.org/t/add-driver-to-render-resolution/601348/2 "2014-02-13T04:43:58Z")

</div>

Not as a driver, maybe as a handler

```auto

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](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

---

<div class="post-metadata">

**Author:** ![Lell](https://blenderartists.org/user_avatar/blenderartists.org/lell/32/13469_2.png) [@Lell](https://blenderartists.org/u/Lell)\
**Post date:** [February 13, 2014, 5:22am UTC](https://blenderartists.org/t/add-driver-to-render-resolution/601348/3 "2014-02-13T05:22:00Z")

</div>

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

```auto
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!

---

<div class="post-metadata">

**Author:** ![Lell](https://blenderartists.org/user_avatar/blenderartists.org/lell/32/13469_2.png) [@Lell](https://blenderartists.org/u/Lell)\
**Post date:** [February 13, 2014, 5:26am UTC](https://blenderartists.org/t/add-driver-to-render-resolution/601348/4 "2014-02-13T05:26:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![batFINGER](https://blenderartists.org/user_avatar/blenderartists.org/batfinger/32/5981_2.png) [@batFINGER](https://blenderartists.org/u/batFINGER)\
**Post date:** [February 13, 2014, 5:32am UTC](https://blenderartists.org/t/add-driver-to-render-resolution/601348/5 "2014-02-13T05:32:46Z")

</div>

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

```auto

camera = scene.camera

```

since scene is passed to the handler.

---

<div class="post-metadata">

**Author:** ![Lell](https://blenderartists.org/user_avatar/blenderartists.org/lell/32/13469_2.png) [@Lell](https://blenderartists.org/u/Lell)\
**Post date:** [February 13, 2014, 6:39am UTC](https://blenderartists.org/t/add-driver-to-render-resolution/601348/6 "2014-02-13T06:39:04Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![batFINGER](https://blenderartists.org/user_avatar/blenderartists.org/batfinger/32/5981_2.png) [@batFINGER](https://blenderartists.org/u/batFINGER)\
**Post date:** [February 13, 2014, 7:29am UTC](https://blenderartists.org/t/add-driver-to-render-resolution/601348/7 "2014-02-13T07:29:36Z")

</div>

> [@Lell](#):
>
> 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…

```auto

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.

---

<div class="post-metadata">

**Author:** ![Lell](https://blenderartists.org/user_avatar/blenderartists.org/lell/32/13469_2.png) [@Lell](https://blenderartists.org/u/Lell)\
**Post date:** [February 13, 2014, 1:15pm UTC](https://blenderartists.org/t/add-driver-to-render-resolution/601348/8 "2014-02-13T13:15:42Z")

</div>

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.

```auto

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"] &lt;= 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!
