Hi!
Didn’t found matching thread, so i’m gonna ask here. :o
I’m writing little operator, which have to generate for mesh new UVLayer, then new image for baking, then Vertex color layer, colorize vertices somehow, and finally bake vertex colors into this newly generated image.
So part till baking works fine. But when i’m trying to bake Vcolors via bpy.ops.object.bake_image() blender says, that there is no image to bake to. I have to manually enter edit mode, and in the UV/Image editor assign newly generated image to UV’s. Only then i can bake. Here is the question!
How can i assign generated image to UVTexture via python?
Here some code:
Ob = context.active_object
bpy.ops.object.mode_set(mode='EDIT')
UVLayer = Ob.data.uv_textures.new(name='MathTextureUV') if 'MathTextureUV' not in Ob.data.uv_textures.keys() else Ob.data.uv_textures['MathTextureUV']
UVLayer.active = UVLayer.active_render = True
bpy.ops.uv.unwrap()
Image = D.images.new('MathTextureImage', self.texture_size, self.texture_size)
for uv in UVLayer.data:
uv.image = Image
VLayer = Ob.data.vertex_colors.new(name='MathTextureVColor') if 'MathTextureVColor' not in Ob.data.vertex_colors.keys() else Ob.data.vertex_colors['MathTextureVColor']
VLayer.active = VLayer.active_render = True
bpy.ops.object.mode_set(mode='VERTEX_PAINT')
#... ... ... boring stuff
bpy.ops.object.mode_set(mode='EDIT')
context.scene.render.bake_type = 'VERTEX_COLORS'
bpy.ops.object.bake_image()
It is known that many ops are only available in certain modes. Usually this makes sense and is also true in the ui (you can only apply a modifier in object mode, for instance). In this case I don’t know why this only works in object mode. But thanks for figuring out that that was the issues. Can you make your thread solved?
It is known that many ops are only available in certain modes
But this is not about an operator! UVTexture.data[#].image is a property. Some RNA properties are read-only in certain modes, or it’s writable but only a temporary copy of some data which doesn’t get flushed back and is thus discarded on next mode change (the reason behind this is that different data structures are needed for different modes, and they are not always converted back and forth, because there’s usually no need for it).
I don’t think this is written down anywhere, but as a rule of thumb, use object mode for properties like UVs, select etc. Editmode is required for EditBones, but afaik you can set properties on them and they will also be available for the corresponding Bone.