5
$\begingroup$
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): transparent background

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

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.

$\endgroup$
  • 2
    $\begingroup$ A simple way is to hack the addon here: developer.blender.org/diffusion/BA/browse/master/… $\endgroup$ – lemon Sep 5 at 7:27
  • 1
    $\begingroup$ @lemon But how am I supposed to change Blender's source code from my Blender addon? $\endgroup$ – Flo Sep 5 at 7:44
  • 1
    $\begingroup$ 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...)? $\endgroup$ – lemon Sep 5 at 7:53
  • $\begingroup$ @lemon Sounds good! I will try it :) $\endgroup$ – Flo Sep 5 at 8:35
4
+50
$\begingroup$

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

enter image description here

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:

enter image description here

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

Github (Comment included)

| improve this answer | |
$\endgroup$
2
$\begingroup$

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.

comparison

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.

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ That's definitely a good idea! Unfortunately I also need other colors than black :) $\endgroup$ – Flo Sep 9 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.