How to center a brush texture stencil

When you load a texture for a brush in Texture Paint mode, the default position is at the bottom left side of the screen. Is there a quick method to center this through code? If not how would I get the 3D View screen width/height and subtract it from half of the width/height of the stencil so I could create this code? Thanks.

Just use this :confused: Thanks me.



bpy.context.region.width

bpy.context.region.height


I spent a few minutes trying to figure out how to align the texture stencil with the screen width and height before I realized that the stencil’s x,y origin points are in its center so this equation requires no extra information about the stencil you are trying to center’s width or height. This is convenient.

I ended up using some code someone else had helped me out with in a previous thread to finish this one off. Here it is for anyone who would like it built in with some safety nets. Throw this in the script editor and hit “Run Script” it will work.


import bpy

v3d_list = [area for area in bpy.context.screen.areas if area.type == 'VIEW_3D']
if v3d_list:
    mainV3D = max(v3d_list, key=lambda area: area.width * area.height)
       
    x = mainV3D.width / 2
    y = mainV3D.height / 2
    
    try:
        if bpy.context.sculpt_object:   
            brushName = bpy.context.tool_settings.sculpt.brush.name
        else:
            brushName = bpy.context.tool_settings.image_paint.brush.name
            
        bpy.data.brushes[brushName].stencil_pos.xy = x, y
    except:
        pass