Python: How to Export All the UV Layouts in Blender

Hate it when that happens.

This week is not a Python breakdown week. It’s just the code because my brain is still recovering from Scheme tutorials. And I really wanted to make a functional (or semi-functional) batch UV layout exporter. The script below does a blanket Smart UV Project for all selected objects and stashes the resultant UV layouts into a specified folder.

Step 1: Select all the objects you need maps for.

This step is important. The script starts off with a for loop going through the selected objects. If they aren’t selected, it won’t work.*

*You know how I know that? Not because I wrote the damn thing. because I wrote it, forgot to select the objects, spent a half hour staring at Blender wondering why the stupid thing wasn’t working, considered throwing the computer out the window, decided against it because it’d be a dick move to punish the computer for Blender’s problems, ate a 7 ounce chocolate bar with a jar of peanut butter, rewrote the script, and realized that I’m an idiot.

Step 2: Paste this script into the Text Editor.

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

Step 3: Change the file path to your preferred UV map location.

And remember that Python wants double backslashes in its file paths. For instance “C:\\Users\\Desktop\\UVMaps\\“. As written the script actually assigns the file path to variable f by combining the string “C:\\” and the name of the loop’s current object. Thankfully, Python makes it easy to combine strings with a simple + sign.

f="C:\\Users\\Desktop\\UVLayouts\\" + bpy.data.objects[selObj[i]].name

A tip: When naming the objects, don’t let Blender do the default Plane.001, Plane.002, Plane.003. The script won’t work if you have spare periods in the name. You can, however, get away with Plane001, Plane002, Plane003.

How exciting.

Step 4: Run it.

If all has gone well, you should be able to navigate to the file and see a nice, long list of .png files.

All the PNGs

Then you can go nuts with the texturing. I personally enjoy this node set-up greatly because if you set the UV map to O opacity, you can plug it into a mixed transparency/diffuse node and light up the seams with any color sun light you please. Like so.

How... Stephen King.

 

 

9 Replies to “Python: How to Export All the UV Layouts in Blender”

      1. Hi gifguide2code,
        Any chance of updating this to 2.79b?
        This seems extremely useful..
        but despite my best efforts,
        I’m only getting errors

        Like

        1. Hi Em,
          I use Blender 2.79b–what kind of error you getting? This script can be a bit touchy–I have to go through a mental checklist every time I use it. Do I have my objects selected? Do any of the selected objects have periods in the name (if so, you gotta rename them)? Is my filepath mapped to an actual file?

          Like

          1. Hi,

            Thanks for your reply! I got the script to work, by naming the path correctly. Who knew? lol.

            I also made a few changes to the modes to ensure it would work for my purposes. For example : (action=”SELECT”) instead of (action=”TOGGLE”)

            Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: