0
$\begingroup$

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
$\endgroup$
  • 1
    $\begingroup$ 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? $\endgroup$ – Ray Mairlot May 3 at 17:49
  • $\begingroup$ By "confirm this script works," I just meant test it. Here is the error message: ibb.co/6rhN7gk $\endgroup$ – Emboo2 May 3 at 18:02
  • $\begingroup$ sample blendfile: pasteall.org/blend/index.php?id=51680 The script seems to require a folder called " UVLayouts " on the desktop. $\endgroup$ – Emboo2 May 3 at 18:13
  • 1
    $\begingroup$ In future, please use the built-in image uploader. See: meta.stackexchange.com/questions/75491/… $\endgroup$ – Ray Mairlot May 3 at 20:11
2
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Martynas Žiemys May 5 at 10:10
  • 1
    $\begingroup$ @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. $\endgroup$ – Hikariztw May 5 at 10:43

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.