[solved] Issue with Edit Mode, Bake Render and UV layers

I’m cleaning up my Paper Model script to blender 2.80 cleaning up its mesh access. I’d like to do all mesh processing in Edit Mode and BMesh since it allows me to read and modify all the necessary data directly.

The problem I encountered now is that UV layers created in Edit Mode are not seen by the Bake Render operator until I switch to Object Mode. More precisely, I do this:

  1. Switch a Mesh object to Edit Mode
  2. Add a new UV layer
  3. Call bpy.ops.object.bake() with appropriate parameters
  4. Get an error: No active UV layer found in the object.

I understand that not all data is updated while within Edit Mode. What I don’t understand in this case is that the UV layer is already listed in bpy.context.object.data.uv_layers. How come that RNA sees it but the Bake operator (= CustomData API) does not? Is this all right or is it a bug?

Can I somehow update the UV Layers structure without doing a Object <-> Edit Mode switch?

The issue apparently is not related to 2.80 nor BMesh – 2.79 throws exactly the same error.

You can use update_from_editmode to update without switching modes:
https://docs.blender.org/api/blender_python_api_master/bpy.types.Object.html#bpy.types.Object.update_from_editmode

When you create the UV layer in edit mode then the layer is created, but it is not filled with anything in object mode.

Thank you! It works.

In the meantime, I discovered that the bake operator behaves quite weirdly, and I posted it as bug T60093. But that’s probably a different story.