Skip to content

Instantly share code, notes, and snippets.

@loonaticx
Last active November 8, 2022 00:00
Show Gist options
  • Save loonaticx/37eeebecea6093ef79493f770bba7dee to your computer and use it in GitHub Desktop.
Save loonaticx/37eeebecea6093ef79493f770bba7dee to your computer and use it in GitHub Desktop.
Batch UV Exporter on *.obj files, configured to be used in a Toontown resources environment.
import bpy
import os, subprocess, sys, re
class extractUV():
def __init__(self):
self.view_layer = bpy.context.view_layer
self.mFilePath = None
#scene = bpy.data.scenes.new("Scene")
bpy.ops.object.select_all()
bpy.ops.object.delete() # Delete the default stuff
pass
def parseModel(self):
#bpy.ops.object.select_all(action='SELECT')
selection = bpy.context.selected_objects
bpy.ops.object.select_all(action='DESELECT')
if self.mFilePath is None:
return
for obj in selection:
self.obj = obj
obj.select_set(True)
self.view_layer.objects.active = obj
self.extractUV()
bpy.ops.object.delete()
def extractUV(self):
if not bpy.context.object.data.uv_layers:
print(self.obj.name + " does not have UV map, skipping")
else:
exportPath = self.mFilePath + "_UV"
exportPath = "C:/Users/Azrael/Desktop/blender test stuff/" + self.obj.name
bpy.ops.uv.export_layout(filepath=exportPath, mode='PNG', opacity=45)
def addModel(self, modelFilePath):
self.mFilePath = modelFilePath
bpy.ops.import_scene.obj(filepath=self.mFilePath) # maybe replace with glob
uv = extractUV()
os.chdir("C:/Users/Azrael/Desktop/Toontown/resources/")
allFiles = []
selectedPhases = ['5']
inputFile = ".obj"
for phase in selectedPhases:
if not os.path.exists('phase_%s' % phase):
continue
for root, _, files in os.walk('phase_%s' % phase):
for file in files:
if not file.endswith(inputFile): # Input file
continue
file = os.path.join(root, file)
allFiles.append(file)
for file in allFiles:
uv.addModel(file)
uv.parseModel()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment