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 :)
Asked
Active
Viewed 65 times
0
Michael Hajný
- 3
- 2
-
So you want to downsize the image as in reduce it's resolution or just make it smaller so it can be seen in the viewport? – Jakemoyo Aug 25 '22 at 15:25
-
@Jakemoyo I edited the question and now should be more clear. – Michael Hajný Aug 25 '22 at 15:29
-
Try looking into this sort of thing except you will want to use context.temp_override() instead of create a context dictionary. That is a recent update to the API post 3.0 IIRC. The procedure is essentially the same though. – Jakemoyo Aug 25 '22 at 15:33
-
I maybe don't get it, but what is the purpose of overriding the context, and what is the difference between the workspace and context? :O Because I thought that all I need to do is to change the workspace and work with that. – Michael Hajný Aug 25 '22 at 15:39
-
https://blender.stackexchange.com/a/270716/142292 – Harry McKenzie Aug 25 '22 at 15:54
1 Answers
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