import bpy
import bmesh
import math
import os, sys, time

argv = sys.argv
argv = argv[argv.index("--") + 1:]

obj_name = argv[0]
basepath = os.getcwd()
print("Object: " + obj_name)

target_obj = None
for obj in bpy.data.objects:
    if obj.name == obj_name:
        target_obj = obj
        break

target_obj.select_set(True)
bpy.context.view_layer.objects.active = target_obj
l = target_obj.data.uv_layers.new(name="UVMapCustom")
target_obj.data.uv_layers.active = l

bpy.ops.object.mode_set(mode='EDIT')
#bpy.ops.mesh.select_mode(type="FACE")
#bpy.ops.mesh.select_all(action='SELECT')
#bpy.ops.uv.unwrap(method='UNWRAP', fill_holes=True, correct_aspect=True, use_subsurf_data=False, margin=0.002)
## bpy.ops.uv.smart_project(angle_limit=66.0, island_margin=0.0, user_area_weight=0.0, use_aspect=True, stretch_to_bounds=True)
# bpy.ops.uv.unwrap()
bpy.ops.mesh.select_all(action='SELECT')
# bpy.ops.uv.unwrap()
bm = bmesh.new()
bm = bmesh.from_edit_mesh(obj.data)
uv_layer = bm.loops.layers.uv["UVMapCustom"]
for f in bm.faces:
    f.select = True
bpy.ops.uv.unwrap(method='ANGLE_BASED', fill_holes=True, correct_aspect=True, margin=0.001)
#uv_layer = bm.loops.layers.uv.verify()
if False:
    for f in bm.faces:
        for l in f.loops:
            vert = l.vert
            if vert.co.x >= 0.0:
                if vert.co.x < 0.225:
                    if vert.co.y >= 0.0:
                        l[uv_layer].uv.x = vert.co.x
                        if vert.co.z < 1.04:
                            #legs
                            px = vert.co.x - 0.1
                            pd = vert.co.y
                            a = (math.atan2(-pd, px) + math.pi) / (2.0 * math.pi)
                            l[uv_layer].uv.y = vert.co.z * 0.4
                            l[uv_layer].uv.x = a * 0.27
                        elif vert.co.z < 1.3477:
                            l[uv_layer].uv.y = vert.co.z * 0.5 - 0.1
                        elif vert.co.z < 1.5497:
                            #chest
                            l[uv_layer].uv.y = vert.co.z * 0.9 - 0.65
    #                    elif vert.co.z < 1.5531:
    #                        l[uv_layer].uv.y = vert.co.z * 0.99 - 0.8
                        else:
                            # head
                            pd = vert.co.y
                            px = vert.co.x
                            a = (math.atan2(-pd, px) + math.pi) / (2.0 * math.pi)
                            l[uv_layer].uv.y = vert.co.z * 0.8 - 0.47
                            l[uv_layer].uv.x = a * 0.8 - 0.2
                    else:
                        l[uv_layer].uv.x = 0.5 - vert.co.x
                        if vert.co.z < 1.04:
                            #legs
                            px = vert.co.x - 0.1
                            pd = vert.co.y
                            a = (math.atan2(-pd, px) + math.pi) / (2.0 * math.pi)
                            l[uv_layer].uv.y = vert.co.z * 0.4
                            l[uv_layer].uv.x = 0.195 + a * 0.3
                        elif vert.co.z < 1.3477:
                            l[uv_layer].uv.y = vert.co.z * 0.5 - 0.1
                        elif vert.co.z < 1.5497:
                            #chest
                            l[uv_layer].uv.y = vert.co.z * 0.9 - 0.65
                        else:
                            # head
                            pd = vert.co.y
                            px = vert.co.x
                            a = (math.atan2(-pd, px) + math.pi) / (2.0 * math.pi)
                            l[uv_layer].uv.y = vert.co.z * 0.8 - 0.47
                            l[uv_layer].uv.x = -0.2 + a * 0.8
                else:
                    # arms
                    px = (vert.co.z - 1.395)
                    py = (-vert.co.x + 0.915)
                    pd = vert.co.y
                    a = (math.atan2(-pd, px - 0.049) + math.pi) / (2.0 * math.pi)
                    print(a)
                    l[uv_layer].uv.x = a * 0.14 + 0.18
                    l[uv_layer].uv.y = (-vert.co.x + 0.915) * 0.7 + 0.05
            else:
                if vert.co.x < 0.225:
                    #not arms
                    l[uv_layer].uv.x = vert.co.x - 5.0
                    l[uv_layer].uv.y = vert.co.z
                else:
                    #arms
                    l[uv_layer].uv.x = vert.co.x - 5.0
                    l[uv_layer].uv.y = vert.co.z
for f in bm.faces:
    for l in f.loops:
        vert = l.vert
        if vert.co.z < 1.04:
            #legs
            px = vert.co.x - 0.1
            pd = vert.co.y
            a = (math.atan2(-pd, px) + math.pi) / (2.0 * math.pi)
            l[uv_layer].uv.y = vert.co.z * 0.4
            if vert.co.x >= 0.0:
               l[uv_layer].uv.x = a * 0.27
            else:
               l[uv_layer].uv.x = a * 0.27 - 1.0

bmesh.update_edit_mesh(obj.data)

bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.wm.save_as_mainfile(filepath="tmp.blend")



