Hi!

In this tutorial we're going to see how to use the export_layout function in Blender 3.0 (Alpha) to export the UV maps (or: Layouts) of all of the MESH objects available in a scene.

This script may be useful if you have to provide the UV maps as PNG images, for example if you want to publish your 3D scene on certain 3D stock websites.

The bpy.ops function we're going to use is pretty simple, actually: it requires two parameters (the filepath of the image to be created and the UV size, in pixels), but we have to use it for all of the MESH objects available in the scene, so we'll use a FOR statement on the MESH objects.

First of all, let's open a BLEND file; you can start with a brand-new file, too, but you'll have to save it somewhere, because we'll put the PNG files in the same folder of the project.

BPY 3 UV export tutorial 1

Let's do some basic imports:

import bpy

from bpy import *

As I have said before, the export_layout function requires 2 parameters, so let's define two variables (we could write the values in the function itself, but I prefer to define them here for clarity's sake):

UVpath = bpy.path.abspath(“//”) + “UV-LAYOUT---”

UVsize = (2048, 2048)

BPY 3 UV export tutorial 2

As you can see, “bpy.path.abspath” will yield the absolute path on disk of the BLEND file; we are going to add a prefix (“UV-LAYOUT---”) to all the png files, but you can change or remove that.

The UVsize parameter is a tuple of 2 numerical values, which are the dimensions – in pixels – of the images to be created; I'm writing 2048, thus creating a 2k image.

In order to execute the function on all the MESH objects available in a scene, we have to create a FOR statament, so we can write:

for o in bpy.data.objects:

then, in the for statement (so: don't forget to indentate the text), we can add a “filter” on the type of the object:

if(o.type==”MESH”):

The condition is clear: the type of the current bpy.data.object must be MESH.

The following statements must be put inside the if, so – again – don't forget to indentate the text!

bpy.context.view_layer.objects.active = o

bpy.ops.uv.export_layout(filepath = UVpath + o.name, size = UVsize)

BPY 3 UV export tutorial 3

The assignment will set the ACTIVE object of the scene, assigning the current mesh to context.view_layer.objects.active; we have to do this because the following OPS (Blender Python operation) will be performed on the one and only ACTIVE object (remember: you can select multiple objects, but there's only one active object at time).

The ops statement will export the layout of the ACTIVE object in the given filepath (made by the UVpath variable, with the “UV-LAYOUT---” prefix, plus the name of the ACTIVE object) and with the given size, in pixels.

BPY 3 UV export tutorial 4

Before closing this tutorial, I'm going to give you an assignment: edit this script in order to export both the 2k and 4k UV Layouts of the meshes available in your scenes, providing the images with 2 different prefixes (“UV-2K---” and “UV-4k---”, for example).

That's all! I hope you enjoyed this tutorial!

See you soon!