Python: How to Export All the UV Layouts in Blender

Can anyone confirm this script works in 2.79b? (I can't use 2.8 at the moment) The script is supposed to export the UV layouts of all selected meshes at once to a specific folder, but I'm no coder, and I'm not having any luck with it yet.

Error Message:

import bpy

selObj = []

for obj in bpy.context.selected_objects:
selObj.append(obj.name)

bpy.ops.object.select_all(action='TOGGLE')

i=0
while i < len(selObj):
bpy.context.scene.objects.active = bpy.context.scene.objects[selObj[i]]
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.uv.smart_project(angle_limit=66.0, island_margin=0.0, user_area_weight=0.0)
f="C:\\Users\\Desktop\\UVLayouts\\" + bpy.data.objects[selObj[i]].name
bpy.ops.uv.export_layout(filepath=f, mode='PNG', opacity=0)
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='TOGGLE')
i+=1

• Hi. What do you mean "Can anyone confirm this script works"? What error are you getting? What is leading you to believe that it isn't working? – Ray Mairlot May 3 '19 at 17:49
• By "confirm this script works," I just meant test it. Here is the error message: ibb.co/6rhN7gk – Emboo2 May 3 '19 at 18:02
• sample blendfile: pasteall.org/blend/index.php?id=51680 The script seems to require a folder called " UVLayouts " on the desktop. – Emboo2 May 3 '19 at 18:13
• In future, please use the built-in image uploader. See: meta.stackexchange.com/questions/75491/… – Ray Mairlot May 3 '19 at 20:11

It won't work if the code in line 17 - "C:\\Users\\Desktop\\UVLayouts\\" doesn't exist on your computer as a Directory (or blender doesn't allowed to modify in that directory). You could change the path of it or create a directory on that location with the same naming.

The mechanism of how bpy.ops.uv.export_layout work on opening a file make this error. It use open, a python built-in function to deal with file.

It requires the parent folder of the filepath exist, as discussion in here.

You should check out the discussion carefully since that is a basic knowledge of python. And for my advise, you could always search the error in google, the answer for your problem is there most of the time. You need to search it with a good key words, in this case FileNotFoundError: [Errno 2] No such file or directory will do the work.

The Script

As Martin Z point out, we should fix this script a little bit to make it more robust (for those not familiar with programming):

import bpy
import os

# You should change this varibale in "here" to match your own directory path
# use '/' or '\\' for hirecacy
target_dir = "C:/some/directory/"

selObj = []

for obj in bpy.context.selected_objects:
selObj.append(obj.name)

bpy.ops.object.select_all(action='TOGGLE')

i=0
while i < len(selObj):
bpy.context.scene.objects.active = bpy.context.scene.objects[selObj[i]]
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.uv.smart_project(angle_limit=66.0, island_margin=0.0, user_area_weight=0.0)
full_file_name=target_dir + bpy.data.objects[selObj[i]].name

# This code detected whether target_dir exist, if not, then create it
dirname = os.path.dirname(full_file_name)
if not os.path.exists(dirname):
os.makedirs(dirname)

bpy.ops.uv.export_layout(filepath=full_file_name, mode='PNG', opacity=0)
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.select_all(action='TOGGLE')
i+=1



Run this script in blender 2.7x text editor, and change target_dir = "C:/some/directory/" to your desired location. And still, strongly recommend to read the code carefully.

• I fail to see this as an answer to the question. It is not clear to me how one could export UV layouts of all objects in a scene in Blender after reading this. – Martynas Žiemys May 5 '19 at 10:10
• @MartinZ Well, my bad. Going the issue too deep into basic python problem. But I thought I did answer the question in a better way when people doesn't know how script is work. Anyway, update a little bit more answer. – Hikariztw May 5 '19 at 10:43