why does pack_islands work but snap_selected errors with incorrect context?

import bpy

bpy.ops.object.mode_set(mode='EDIT', toggle=False)

bpy.ops.uv.pack_islands(margin=0.05, rotate=False)

#No problem either
bpy.data.screens['UV Editing'].areas[1].spaces[0].image = bpy.data.images[imgName]

#Fails, context incorrect

I've tried some workarounds, like that:

import bpy
bpy.context.area.type = 'IMAGE_EDITOR'

This changes the active viewport (usually a space 3d from where the code is run) to an image editor. It works, but I can't set an image here.

Update: I now found a solution to set an image to this area

bpy.context.space_data.image = bpy.data.images[imgName]

Still, my question remains, why is this necessary? Is there maybe a more elegant solution?

  • $\begingroup$ Stating the problems but not saying describing the background of what you are trying to achieve just makes a question harder to resolve. If you spend the time to describe what exactly you were trying to achieve, someone may come up with a solution that may look really different to the current one you are looking at. $\endgroup$
    – hawkenfox
    Jan 18, 2016 at 9:14
  • $\begingroup$ My questions are basically 1.) Why is this necessary for snap_selected, but not for pack_islands? 2.) How to execute the bpy.ops.uv stuff properly without running into this problem and without this more or less ugly workaround? $\endgroup$
    – bortran
    Jan 18, 2016 at 10:43


You must log in to answer this question.

Browse other questions tagged .