Adding an empty UV Map to a mesh

I’m trying to modify my export to FBX to bake materials to vertex colours on the way out.

Through some fiddling, it seems it will work as long as there is a UV Map on the mesh. I don’t need one and would rather not need to add it to every object.

I’ve tried all sorts of ways to add the UV Map in Python but Just can’t get it to work - can anyone help? I don’t want to unwrap either as I’d like to have all the vests in one corner.

Ideally I’d add the UV Map, bake the vert colour, remove the UV Map, export.

import bpy

def mesh_data(obj):
	bpy.ops.object.mode_set(mode='OBJECT')
	bpy.context.scene.objects.active = obj
	bpy.ops.mesh.select_all(action='SELECT')


	if self.replace_active_layer and obdata.vertex_colors.active:
		vertex_colors = obdata.vertex_colors.active
	else:
		vertex_colors = obdata.vertex_colors.new(name="Baked UV texture")


	# if self.replace_active_layer and obdata.uv_textures.active:
	# 	uv_textures = obdata.uv_textures.active
	# else:
	# 	uv_textures = obdata.uv_textures.new(name="UV Map")


	# if obdata.uv_textures.active is None:
	# 	uv_tex = obdata.uv_textures.new().data
	# else:
	# 	uv_tex = obdata.uv_textures.active.data


	bpy.ops.mesh.uv_textures.new("foo")


	bpy.context.scene.render.bake_type = 'TEXTURE'
	bpy.context.scene.render.use_bake_to_vertex_color = True
	bpy.ops.object.bake_image()

I’ve tried mesh.uv_textures.new, mesh.uv_textures.add() etc. just can’t get this to work :frowning:

(name=“Foo”)?

Yeah tried with name= and without name=.

Interestingly bpy.ops.mesh.uv_texture_add() in the Python console works perfectly. just not in my script…

Running it in the Python console adds a UVMap layer.

import bpy

def mesh_data(obj):
	bpy.ops.object.mode_set(mode='OBJECT')
	bpy.context.scene.objects.active = obj
	bpy.ops.mesh.select_all(action='SELECT')


	if self.replace_active_layer and obdata.vertex_colors.active:
		vertex_colors = obdata.vertex_colors.active
	else:
		vertex_colors = obdata.vertex_colors.new(name="Col")


    bpy.ops.mesh.uv_texture_add()
    
    bpy.context.scene.render.bake_type = 'TEXTURE'
	bpy.context.scene.render.use_bake_to_vertex_color = True
	bpy.ops.object.bake_image()

I found a reply from Campbell on developer.blender.org recommending uv_textures.new()

https://developer.blender.org/T25854

import bpy

def mesh_data(obj):
    bpy.ops.object.mode_set(mode='OBJECT')
    bpy.context.scene.objects.active = obj
    bpy.ops.mesh.select_all(action='SELECT')


    if self.replace_active_layer and obdata.vertex_colors.active:
        vertex_colors = obdata.vertex_colors.active
    else:
        vertex_colors = obdata.vertex_colors.new(name="Col")


    #bpy.ops.mesh.uv_texture_add()


    uvlayer = obj.data.uv_textures.new()


    bpy.context.scene.render.bake_type = 'TEXTURE'
    bpy.context.scene.render.use_bake_to_vertex_color = True
    bpy.ops.object.bake_image()

Again, script runs, no UVMap added :frowning:

Hmm, even if I manually add UVMap layer, it still doesn’t bake… sigh.