# Python: How to Save UV Layout With Specific Background Color?

select_obj(some_obj)
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.select_all(action='TOGGLE')
# how to set background color of this image?
bpy.ops.uv.export_layout(filepath='uv_layout.png', size=(1024, 1024), opacity=1)
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='TOGGLE')


The image looks like this (it has a transparent background):

However, I want to achieve a colored background like this:

I've tried to import PIL and do the image manipulation with this module, but it seems as if it isn't included in Blender.

• A simple way is to hack the addon here: developer.blender.org/diffusion/BA/browse/master/… – lemon Sep 5 at 7:27
• @lemon But how am I supposed to change Blender's source code from my Blender addon? – Flo Sep 5 at 7:44
• In that case, can reuse the code? As bpy.ops.uv.export_layout doesn't do it, what an answer can be except rewriting it? Or reprocess the file exported by bpy.ops.uv.export_layout (but a lot slower...)? – lemon Sep 5 at 7:53
• @lemon Sounds good! I will try it :) – Flo Sep 5 at 8:35

It is not that hard to overwrite the function provide by the module itself (it is still in Python scope):

import io_mesh_uv_layout
from io_mesh_uv_layout.export_uv_png import *

def new_export(filepath, face_data, colors, width, height, opacity):
offscreen = gpu.types.GPUOffScreen(width, height)
offscreen.bind()

try:
bgl.glClearColor(0.0, 1.0, 0.0, 0.9)  # Here, a green, 0.9 alpha background
bgl.glClear(bgl.GL_COLOR_BUFFER_BIT)
draw_image(face_data, opacity)

pixel_data = get_pixel_data_from_current_back_buffer(width, height)
save_pixels(filepath, pixel_data, width, height)
finally:
offscreen.unbind()
offscreen.free()

io_mesh_uv_layout.export_uv_png.export = new_export  # Directly overwrite it



In your addon, you can provide a color picker(from a color float property) for user. And after updating the value, patch the original export function on the fly. There shouldn't be any tricky part and no performance issue at all.

A more complex method is to modify the uv layout export itself:

Which needs to modify the operator itself, kind of tricky but more flexible to use:

(Comment included)

If black background is good enough you could import the created image and remove the alpha channel.

select_obj(some_obj)
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.select_all(action='TOGGLE')
# how to set background color of this image?
bpy.ops.uv.export_layout(filepath='uv_layout.png', size=(1024, 1024), opacity=1)

img = bpy.data.images.load("C:\\File\\Path\\Here\\uv_layout.png") #Import image. Remember to change filepath
img.alpha_mode = "NONE" #Ignore alpha channel
img.save() #Save the image

bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='TOGGLE')


Just make sure you change the filepath to point to your file.

This can be done manually in image editor from the Alpha drop-down menu by selecting None. Alpha Drop-down menu is under Image tab. Image tab visibility can be toggled by pressing n.

• That's definitely a good idea! Unfortunately I also need other colors than black :) – Flo Sep 9 at 19:21