"""
calculate and set camera values
for overscan in one camera setup

overscan is not uniform. It matches
image proportions

default overscan:
    10 / 10 pixels :: left / right


###
w = 1920.0
h = 1440.0
w_osc = 1980
w_extra = w_osc - w
osc_factor = w / w_osc
h_osc = h / osc_factor

print '\\noriginal res: ', w, '*', h
print 'overscan res', w_osc, '*', h_osc
print 'overscan factor (uniform)', osc_factor
print 'extra pixels (width, height)', w_osc - w, h_osc - h
###
"""


from fractions import Fraction
import pymel.core


def set_camera_post_scale(ratio):
    cam = None

    try:
        cam = pymel.core.selected().pop().getShape()
    except:
        pass

    if cam:
        cam.setAttr('postScale', ratio)


def set_osc_resolution(pixels):
    rendersettings = pymel.core.PyNode('defaultResolution')
    res_x = rendersettings.getAttr('width')
    res_y = rendersettings.getAttr('height')
    image_ratio = Fraction(res_x, res_y)
    res_y_new = res_y+(pixels*2)
    postscale_ratio = Fraction(res_y, res_y_new)
    res_x_new = float(res_y_new * image_ratio)

    rendersettings.setAttr('width', res_x_new)
    rendersettings.setAttr('height', res_y_new)

    set_camera_post_scale(float(postscale_ratio))

    return (res_x_new, res_y_new)



def main(**kwargs):
    set_osc_resolution(pixels=kwargs.get('pixels', 10))



def test():
    import setoverscan_tests

    try:
        reload(setoverscan_tests)
    except:
        import importlib

        importlib.reload(setoverscan_tests)

    setoverscan_tests.main()

@@ -176,546 +176,493 @@
 ht%0A%0A
-%0A###%0Aw = 1920.0%0Ah = 1440.0%0Aw_osc = 1980%0Aw_extra = w_osc - w%0Aosc_factor = w / w_osc%0Ah_osc = h / osc_factor%0A%0Aprint '%5C%5Cnoriginal res: ', w, '*', h%0Aprint 'overscan res', w_osc, '*', h_osc%0Aprint 'overscan factor (uniform)', osc_factor%0Aprint 'extra pixels (width, height)', w_osc - w, h_osc - h%0A###%0A%22%22%22%0A%0A%0Afrom fractions import Fraction%0Aimport pymel.core%0A%0A%0Adef set_camera_post_scale(ratio):%0A    cam = None%0A%0A    try:%0A        cam = pymel.core.selected().pop().getShape()%0A    except:%0A        pass%0A%0A    if cam:%0A        cam.setAttr('postScale', ratio
+without selection only the render%0Aresolution is set!%0Arender resolution is always set!%0A%22%22%22%0A%0A%0Afrom fractions import Fraction%0Aimport pymel.core%0A%0A%0Adef set_camera_post_scale(ratio):%0A    cam = None%0A%0A    try:%0A        cam = pymel.core.selected().pop().getShape()%0A    except:%0A        pass%0A%0A    # assert cam.getAttr('postScale') == 1.0%0A%0A    if cam and cam.getAttr('postScale') == 1.0:%0A        cam.setAttr('postScale', ratio)%0A    else:%0A        raise Exception('--%3E Camera already has post scale!'
 )%0A%0A%0A

set overscan update; works with camera; errors if camera is set already