0

I would like to fit in the image in Texture Paint WORKSPACE, but I still got the same error RuntimeError: Operator bpy.ops.image.view_all.poll() failed, context is incorrect. -> I do not understand how it is possible because before this part I wrote bpy.context.window.workspace = bpy.data.workspaces["Texture Paint"]. Could someone help me with the context. I thought that the bpy.ops.image.view_all(fit_view=True) is for Texture Paint context (I am maybe a bit confused about what is WORKSPACE and what is context). Thank you :)

1 Answers1

1

To use bpy.ops.image.view_all your context needs to be within the IMAGE_EDITOR. Read more about context here

import bpy

area_type = 'IMAGE_EDITOR' # change this to use the correct Area Type context you want to process in areas = [area for area in bpy.context.window.screen.areas if area.type == area_type]

if len(areas) <= 0: raise Exception(f"Make sure an Area of type {area_type} is open or visible in your screen!")

override = { 'window': bpy.context.window, 'screen': bpy.context.window.screen, 'area': areas[0], 'region': [region for region in areas[0].regions if region.type == 'WINDOW'][0], }

bpy.ops.image.view_all(override, fit_view=True)

Harry McKenzie
  • 10,995
  • 8
  • 23
  • 51