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:

enter image description here

Source: https://gifguide2code.com/2017/01/24/python-how-to-export-all-the-uv-layouts-in-blender/

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

Answer

To the answer

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.

Attribution
Source : Link , Question Author : Emboo2 , Answer Author : HikariTW

Leave a Comment