How to control new windows size?

I am using this super simple script to open up a new window and turn it into UV window but currently the UV window is almost full screen so I wonder if there is a way to control the size of that new window? I could not find anything by doing some google search

import bpy


def main(context):
    bpy.ops.wm.window_new() #open new window split
    bpy.context.area.ui_type = 'UV' #turn it into UV window
    


class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.open_uv_window"
    bl_label = "Open UV window"


    def execute(self, context):
        main(context)
        return {'FINISHED'}


def menu_func(self, context):
    self.layout.operator(SimpleOperator.bl_idname, text=SimpleOperator.bl_label)


# Register and add to the "object" menu (required to also use F3 search "Simple Object Operator" for quick access).
def register():
    bpy.utils.register_class(SimpleOperator)
    bpy.types.VIEW3D_MT_object.append(menu_func)


def unregister():
    bpy.utils.unregister_class(SimpleOperator)
    bpy.types.VIEW3D_MT_object.remove(menu_func)


if __name__ == "__main__":
    register()

I couldn’t find anything when I had this issue a few years ago and ended up giving up on it.
Then I found this repo: https://github.com/schroef/Custom-Preferences-Size
I haven’t actually tested it yet, but it claims to do what you’re asking.
Hope it helps!

1 Like

Thanks, I think I ran into this before as well but it seemed a bit hacky so I didnt give it a shot. I tried it this time and it seems to actually work. Only issue for me is that

`area.type = 'IMAGE EDITOR'`

Does not seem to recognize “UV” as a mode so I cant make it open UV view. In my own script I used this command to change the mode into “UV” .


bpy.context.area.ui_type = 'UV'
import bpy

class OpenUvWindow(bpy.types.Operator):
    bl_idname = "object.open_uv_window"
    bl_label = "Open UV window"

    def execute(self, context):
#        bpy.ops.wm.window_new()
#        bpy.context.scene.render.resolution_x = 500
#        bpy.ops.render.view_show("INVOKE_DEFAULT")
#        bpy.context.area.ui_type = 'UV'



        preferences = context.preferences

        # Modify scene settings
        render = bpy.context.scene.render
        prefs = bpy.context.preferences
        view = prefs.view
        orgResX = render.resolution_x
        orgResY = render.resolution_y
        render.resolution_x = 500
        render.resolution_y = 500
        orgDispMode = view.render_display_type
        view.render_display_type = "WINDOW"

        # Call image editor window
        bpy.ops.render.view_show("INVOKE_DEFAULT")

        # Change area type
        area = bpy.context.window_manager.windows[-1].screen.areas[0]
        area.type = 'IMAGE EDITOR'
        
        

        # Restore old values
        view.render_display_type = orgDispMode
        render.resolution_x = orgResX
        render.resolution_y = orgResY

        

        return {'FINISHED'}

def register() :
    bpy.utils.register_class(OpenUvWindow)



def unregister() :
    bpy.utils.unregister_class(OpenUvWindow)
1 Like

Making the above change made it work for me: since you already grabbed the area you need, you can just use the area variable to switch to the UV editor directly.

I think the issue was that changing bpy.context.area to ‘UV’ might have been running in an area that didn’t have ‘UV’ as a subtype, but by specifying that the area you’re changing is bpy.context.window_manager.windows[-1].screen.areas[0] Blender will know that it should be run only in the new screen you’ve created which does have a ‘UV’ type.

I does work but there is a strange thing. It open us the UV window with the “render result” active so I need to click to close it.

I simplified the code a lot basically this is enough for me to open up new window with 500px resolution and turn it UV mode. Only issue is that I would like it to be straight in the right mode without needing to click this X to close the render result :smile:

import bpy

class OpenUvWindow(bpy.types.Operator):
    bl_idname = "object.open_uv_window"
    bl_label = "Open UV window"

    def execute(self, context):

        bpy.context.scene.render.resolution_x = 500
        bpy.context.scene.render.resolution_y = 500
        # Call image editor window
        bpy.ops.render.view_show("INVOKE_DEFAULT")        
        area = bpy.context.window_manager.windows[-1].screen.areas[0]
        area.ui_type = 'UV'


        

        return {'FINISHED'}

def register() :
    bpy.utils.register_class(OpenUvWindow)



def unregister() :
    bpy.utils.unregister_class(OpenUvWindow)

I wonder if its possible to clear the render result by code?

1 Like

You can clear the render result via:

render_result = bpy.data.images['Render Result']
bpy.data.images.remove(image=render_result)
1 Like