0
$\begingroup$

I'm trying to execute the command bpy.ops.uv.snap_selected(target='PIXELS') in Blender Python, but i keep getting that context is incorrect.

I've run the following Python script in order to find the correct context

import bpy

#all the area types except 'EMPTY' from https://docs.blender.org/api/current/bpy.types.Area.html?highlight=area%20type#bpy.types.Area.type
types = {'VIEW_3D', 'IMAGE_EDITOR','NODE_EDITOR','SEQUENCE_EDITOR','CLIP_EDITOR','DOPESHEET_EDITOR','GRAPH_EDITOR','NLA_EDITOR','TEXT_EDITOR','CONSOLE','INFO','TOPBAR','STATUSBAR','OUTLINER','PROPERTIES','FILE_BROWSER','PREFERENCES'}

#save the current area
area = bpy.context.area.type

#try each type
for type in types:
    #set the context
    bpy.context.area.type = type

    #print out context where operator works
    if bpy.ops.uv.snap_selected.poll():
        print(type)

#leave the context where it was
bpy.context.area.type = area

But I'm getting nothing printed. I've tested this with the operator bpy.ops.uv.pack_islands for example and I'm getting results.

Why I'm getting no valid area type for bpy.ops.uv.snap_selected? Is it a special operator? Can it be run from Python code?

$\endgroup$
0
$\begingroup$

Turns out you have to modify not only the area type but also the ui_type for this to work. For the specific case of bpy.ops.uv.snap_selected operator, the area_type is IMAGE_EDITOR and the ui_type is UV. This piece of code works:

bpy.context.area.type = 'IMAGE_EDITOR'
bpy.context.area.ui_type = 'UV'
bpy.ops.uv.snap_selected(target='PIXELS')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.