Spaces:
Build error
Build error
Create blender_worker.py
Browse files- blender_worker.py +105 -0
blender_worker.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys, json, bpy
|
| 2 |
+
from mathutils import Vector, Matrix
|
| 3 |
+
|
| 4 |
+
a = json.loads(sys.argv[1]); J = a["job"]
|
| 5 |
+
COCO = dict(nose=0, l_sh=5, r_sh=6, l_el=7, r_el=8, l_wr=9, r_wr=10,
|
| 6 |
+
l_hip=11, r_hip=12, l_kn=13, r_kn=14, l_an=15, r_an=16)
|
| 7 |
+
|
| 8 |
+
def reset(): bpy.ops.wm.read_factory_settings(use_empty=True)
|
| 9 |
+
|
| 10 |
+
def mesh_obj(): return next(o for o in bpy.context.scene.objects if o.type == 'MESH')
|
| 11 |
+
|
| 12 |
+
def joints(kp, size, bbox, mesh):
|
| 13 |
+
"""2D keypoints -> 3D joints in mesh space. Front view: image x -> X, image y -> Z, depth Y = bbox centre."""
|
| 14 |
+
x0, y0, x1, y1 = bbox
|
| 15 |
+
bb = [mesh.matrix_world @ Vector(c) for c in mesh.bound_box]
|
| 16 |
+
zmin, zmax = min(v.z for v in bb), max(v.z for v in bb)
|
| 17 |
+
xc = (min(v.x for v in bb) + max(v.x for v in bb)) / 2
|
| 18 |
+
yc = (min(v.y for v in bb) + max(v.y for v in bb)) / 2
|
| 19 |
+
s = (zmax - zmin) / (y1 - y0)
|
| 20 |
+
P = lambda i: Vector((xc + (kp[i][0] - (x0+x1)/2) * s, yc, zmax - (kp[i][1] - y0) * s))
|
| 21 |
+
j = {n: P(i) for n, i in COCO.items()}
|
| 22 |
+
j["hips"] = (j["l_hip"] + j["r_hip"]) / 2
|
| 23 |
+
j["chest"] = (j["l_sh"] + j["r_sh"]) / 2
|
| 24 |
+
return j
|
| 25 |
+
|
| 26 |
+
# (bone, parent, head joint, tail joint)
|
| 27 |
+
SKEL = [("hips", None, "hips", "chest"), ("spine", "hips", "chest", "nose"),
|
| 28 |
+
*[(f"{s}_arm", None, f"{s}_sh", f"{s}_el") for s in "lr"],
|
| 29 |
+
*[(f"{s}_fore", f"{s}_arm", f"{s}_el", f"{s}_wr") for s in "lr"],
|
| 30 |
+
*[(f"{s}_thigh", "hips", f"{s}_hip", f"{s}_kn") for s in "lr"],
|
| 31 |
+
*[(f"{s}_shin", f"{s}_thigh", f"{s}_kn", f"{s}_an") for s in "lr"]]
|
| 32 |
+
# NOTE: parent arm bones to "spine"; set that in the loop below if you want shoulders to follow the torso.
|
| 33 |
+
|
| 34 |
+
def build_rig(j, mesh):
|
| 35 |
+
bpy.ops.object.armature_add(); arm = bpy.context.object; arm.name = "Rig"
|
| 36 |
+
bpy.ops.object.mode_set(mode='EDIT'); eb = arm.data.edit_bones
|
| 37 |
+
for b in list(eb): eb.remove(b)
|
| 38 |
+
for name, parent, h, t in SKEL:
|
| 39 |
+
b = eb.new(name); b.head, b.tail = j[h], j[t]
|
| 40 |
+
if parent: b.parent = eb[parent]
|
| 41 |
+
elif name.endswith("_arm"): b.parent = eb["spine"]
|
| 42 |
+
bpy.ops.object.mode_set(mode='OBJECT')
|
| 43 |
+
mesh.select_set(True); arm.select_set(True); bpy.context.view_layer.objects.active = arm
|
| 44 |
+
bpy.ops.object.parent_set(type='ARMATURE_AUTO')
|
| 45 |
+
return arm
|
| 46 |
+
|
| 47 |
+
def pose_to(arm, jt):
|
| 48 |
+
bpy.context.view_layer.objects.active = arm; bpy.ops.object.mode_set(mode='POSE')
|
| 49 |
+
for name, _, h, t in SKEL: # parents are listed before children
|
| 50 |
+
pb = arm.pose.bones[name]
|
| 51 |
+
tgt = (jt[t] - jt[h]).normalized()
|
| 52 |
+
cur = (pb.tail - pb.head).normalized()
|
| 53 |
+
R = cur.rotation_difference(tgt).to_matrix().to_4x4()
|
| 54 |
+
hd = pb.head.copy()
|
| 55 |
+
pb.matrix = Matrix.Translation(hd) @ R @ Matrix.Translation(-hd) @ pb.matrix
|
| 56 |
+
bpy.context.view_layer.update()
|
| 57 |
+
bpy.ops.object.mode_set(mode='OBJECT')
|
| 58 |
+
|
| 59 |
+
def export_all(arm, mesh):
|
| 60 |
+
"""Bake the pose into a plain mesh and export all three formats."""
|
| 61 |
+
dg = bpy.context.evaluated_depsgraph_get()
|
| 62 |
+
baked = bpy.data.objects.new("Posed", bpy.data.meshes.new_from_object(mesh.evaluated_get(dg)))
|
| 63 |
+
baked.data.materials.clear()
|
| 64 |
+
for m in mesh.data.materials: baked.data.materials.append(m)
|
| 65 |
+
bpy.context.collection.objects.link(baked)
|
| 66 |
+
bpy.ops.object.select_all(action='DESELECT'); baked.select_set(True)
|
| 67 |
+
bpy.context.view_layer.objects.active = baked
|
| 68 |
+
bpy.ops.export_scene.gltf(filepath=f"{J}/posed.glb", export_format='GLB', use_selection=True)
|
| 69 |
+
bpy.ops.wm.obj_export(filepath=f"{J}/posed.obj", export_selected_objects=True)
|
| 70 |
+
bpy.ops.export_scene.fbx(filepath=f"{J}/posed.fbx", use_selection=True, path_mode='COPY', embed_textures=True)
|
| 71 |
+
|
| 72 |
+
cmd = a["cmd"]
|
| 73 |
+
if cmd == "rig":
|
| 74 |
+
reset(); bpy.ops.import_scene.gltf(filepath=f"{J}/raw.glb"); m = mesh_obj()
|
| 75 |
+
d = json.load(open(f"{J}/tpose_kp.json"))
|
| 76 |
+
arm = build_rig(joints(d["kp"], d["size"], d["bbox"], m), m)
|
| 77 |
+
bpy.ops.wm.save_as_mainfile(filepath=f"{J}/rigged.blend")
|
| 78 |
+
bpy.ops.export_scene.gltf(filepath=f"{J}/rigged.glb", export_format='GLB')
|
| 79 |
+
elif cmd == "pose":
|
| 80 |
+
bpy.ops.wm.open_mainfile(filepath=f"{J}/rigged.blend"); m = mesh_obj(); arm = bpy.data.objects["Rig"]
|
| 81 |
+
t = json.load(open(f"{J}/tpose_kp.json")); o = json.load(open(f"{J}/target_kp.json"))
|
| 82 |
+
# target joints use the ORIGINAL image's own bbox for scale, same mapping
|
| 83 |
+
import numpy as np
|
| 84 |
+
pts = np.array(o["kp"]); bbox = [pts[:,0].min(), pts[:,1].min(), pts[:,0].max(), pts[:,1].max()]
|
| 85 |
+
pose_to(arm, joints(o["kp"], o["size"], bbox, m))
|
| 86 |
+
export_all(arm, m); bpy.ops.wm.save_as_mainfile(filepath=f"{J}/posed.blend")
|
| 87 |
+
elif cmd == "uv":
|
| 88 |
+
bpy.ops.wm.open_mainfile(filepath=f"{J}/posed.blend"); m = bpy.data.objects["Posed"]
|
| 89 |
+
bpy.context.view_layer.objects.active = m
|
| 90 |
+
if not m.data.uv_layers:
|
| 91 |
+
bpy.ops.object.mode_set(mode='EDIT'); bpy.ops.mesh.select_all(action='SELECT')
|
| 92 |
+
bpy.ops.uv.smart_project(island_margin=0.003); bpy.ops.object.mode_set(mode='OBJECT')
|
| 93 |
+
bpy.ops.object.mode_set(mode='EDIT'); bpy.ops.mesh.select_all(action='SELECT')
|
| 94 |
+
bpy.ops.uv.export_layout(filepath=f"{J}/uv_layout.png", size=(2048, 2048))
|
| 95 |
+
bpy.ops.object.mode_set(mode='OBJECT')
|
| 96 |
+
img = next(n.image for mat in m.data.materials for n in mat.node_tree.nodes
|
| 97 |
+
if n.type == 'TEX_IMAGE')
|
| 98 |
+
img.filepath_raw = f"{J}/texture.png"; img.file_format = 'PNG'; img.save()
|
| 99 |
+
elif cmd == "retexture":
|
| 100 |
+
bpy.ops.wm.open_mainfile(filepath=f"{J}/posed.blend"); m = bpy.data.objects["Posed"]
|
| 101 |
+
new = bpy.data.images.load(f"{J}/new_texture.png")
|
| 102 |
+
for mat in m.data.materials:
|
| 103 |
+
for n in mat.node_tree.nodes:
|
| 104 |
+
if n.type == 'TEX_IMAGE': n.image = new
|
| 105 |
+
arm = None; export_all(arm, m)
|