Skip to content

Instantly share code, notes, and snippets.

@allanlw
Created September 11, 2016 00:28
Show Gist options
  • Save allanlw/fddbf04870488873030823578db805b7 to your computer and use it in GitHub Desktop.
Save allanlw/fddbf04870488873030823578db805b7 to your computer and use it in GitHub Desktop.
A blender script to flip import an obj with borked UVs!
# invoke like blender --background --python belnder_obj_to_blend_flipped_v.py -- input.obj output.blend
import bpy
import sys
argv = sys.argv
argv = argv[argv.index("--") + 1:] # get all args after "--"
obj_out = argv[0]
# Delete all existing meshes
for obj in list(bpy.data.objects):
if obj.type == "CAMERA": continue
obj.select = True
bpy.ops.object.delete()
# Drop a lamp at (0, -5, 5)
lamp_data = bpy.data.lamps.new(name="Face Lamp", type='POINT')
lamp_object = bpy.data.objects.new(name="Face Lamp", object_data=lamp_data)
bpy.context.scene.objects.link(lamp_object)
lamp_object.location = (0, -5, 5)
bpy.ops.import_scene.obj(filepath=obj_out, axis_forward='-Z', axis_up='Y', split_mode="OFF")
# Flip our y axis on all our UVs
for obj in list(bpy.data.objects):
if obj.type != "MESH": continue
for layer in obj.data.uv_layers:
for loop in layer.data.values():
loop.uv[1] *= -1
bpy.ops.file.pack_all()
bpy.ops.wm.save_as_mainfile(filepath=argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment