How to "Unwrap UV2 for Lightmap/AO" from GDscript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By unfa

My game uses 3D level geometry imported from COLLADA files exported from Blender.
When I manually Unwrap UV2 for Lightmaps in Godot it works only in the engine viewports - as soon as I run the game, the .dae file is being re-imported, the UV2 mapping is gone and the lightmaps don’t work.

If I discard instancing of my 3D scene - it will work, but I can’t easily update my geometry from Blender export.

I’d like to be able to automatically unwrap UV2 fro lightmaps as soon as the COLLADA file is being imported so that I can have it instanced and use lightmaps at the same time.

How can I do this?

:bust_in_silhouette: Reply From: Dadaskis

It’s been a long time since you posted this question, but i’m running into this problem. My attempt to solve it is using code (which can be runned as tool script if you wish) which will unwrap UV2.

extends MeshInstance

export(float) var lightmap_texel_size = 0.4

func process_uv2():
	var old_mesh = mesh as Mesh
	mesh = ArrayMesh.new()
	for surface_id in range(old_mesh.get_surface_count()):
		mesh.add_surface_from_arrays(
			Mesh.PRIMITIVE_TRIANGLES, 
			old_mesh.surface_get_arrays(surface_id)
		)
		var old_mat = old_mesh.surface_get_material(surface_id)
		mesh.surface_set_material(surface_id, old_mat)

	mesh.lightmap_unwrap(global_transform, lightmap_texel_size)
	use_in_baked_light = true

func _ready():
	process_uv2()

I won’t consider it to be an answer, but it can be useful for others.