Imageand3dmodel / blender_worker.py
ChBysk's picture
Create blender_worker.py
2e49cba verified
Raw History Blame Contribute Delete
5.6 kB
import sys, json, bpy
from mathutils import Vector, Matrix
a = json.loads(sys.argv[1]); J = a["job"]
COCO = dict(nose=0, l_sh=5, r_sh=6, l_el=7, r_el=8, l_wr=9, r_wr=10,
l_hip=11, r_hip=12, l_kn=13, r_kn=14, l_an=15, r_an=16)
def reset(): bpy.ops.wm.read_factory_settings(use_empty=True)
def mesh_obj(): return next(o for o in bpy.context.scene.objects if o.type == 'MESH')
def joints(kp, size, bbox, mesh):
"""2D keypoints -> 3D joints in mesh space. Front view: image x -> X, image y -> Z, depth Y = bbox centre."""
x0, y0, x1, y1 = bbox
bb = [mesh.matrix_world @ Vector(c) for c in mesh.bound_box]
zmin, zmax = min(v.z for v in bb), max(v.z for v in bb)
xc = (min(v.x for v in bb) + max(v.x for v in bb)) / 2
yc = (min(v.y for v in bb) + max(v.y for v in bb)) / 2
s = (zmax - zmin) / (y1 - y0)
P = lambda i: Vector((xc + (kp[i][0] - (x0+x1)/2) * s, yc, zmax - (kp[i][1] - y0) * s))
j = {n: P(i) for n, i in COCO.items()}
j["hips"] = (j["l_hip"] + j["r_hip"]) / 2
j["chest"] = (j["l_sh"] + j["r_sh"]) / 2
return j
# (bone, parent, head joint, tail joint)
SKEL = [("hips", None, "hips", "chest"), ("spine", "hips", "chest", "nose"),
*[(f"{s}_arm", None, f"{s}_sh", f"{s}_el") for s in "lr"],
*[(f"{s}_fore", f"{s}_arm", f"{s}_el", f"{s}_wr") for s in "lr"],
*[(f"{s}_thigh", "hips", f"{s}_hip", f"{s}_kn") for s in "lr"],
*[(f"{s}_shin", f"{s}_thigh", f"{s}_kn", f"{s}_an") for s in "lr"]]
# NOTE: parent arm bones to "spine"; set that in the loop below if you want shoulders to follow the torso.
def build_rig(j, mesh):
bpy.ops.object.armature_add(); arm = bpy.context.object; arm.name = "Rig"
bpy.ops.object.mode_set(mode='EDIT'); eb = arm.data.edit_bones
for b in list(eb): eb.remove(b)
for name, parent, h, t in SKEL:
b = eb.new(name); b.head, b.tail = j[h], j[t]
if parent: b.parent = eb[parent]
elif name.endswith("_arm"): b.parent = eb["spine"]
bpy.ops.object.mode_set(mode='OBJECT')
mesh.select_set(True); arm.select_set(True); bpy.context.view_layer.objects.active = arm
bpy.ops.object.parent_set(type='ARMATURE_AUTO')
return arm
def pose_to(arm, jt):
bpy.context.view_layer.objects.active = arm; bpy.ops.object.mode_set(mode='POSE')
for name, _, h, t in SKEL: # parents are listed before children
pb = arm.pose.bones[name]
tgt = (jt[t] - jt[h]).normalized()
cur = (pb.tail - pb.head).normalized()
R = cur.rotation_difference(tgt).to_matrix().to_4x4()
hd = pb.head.copy()
pb.matrix = Matrix.Translation(hd) @ R @ Matrix.Translation(-hd) @ pb.matrix
bpy.context.view_layer.update()
bpy.ops.object.mode_set(mode='OBJECT')
def export_all(arm, mesh):
"""Bake the pose into a plain mesh and export all three formats."""
dg = bpy.context.evaluated_depsgraph_get()
baked = bpy.data.objects.new("Posed", bpy.data.meshes.new_from_object(mesh.evaluated_get(dg)))
baked.data.materials.clear()
for m in mesh.data.materials: baked.data.materials.append(m)
bpy.context.collection.objects.link(baked)
bpy.ops.object.select_all(action='DESELECT'); baked.select_set(True)
bpy.context.view_layer.objects.active = baked
bpy.ops.export_scene.gltf(filepath=f"{J}/posed.glb", export_format='GLB', use_selection=True)
bpy.ops.wm.obj_export(filepath=f"{J}/posed.obj", export_selected_objects=True)
bpy.ops.export_scene.fbx(filepath=f"{J}/posed.fbx", use_selection=True, path_mode='COPY', embed_textures=True)
cmd = a["cmd"]
if cmd == "rig":
reset(); bpy.ops.import_scene.gltf(filepath=f"{J}/raw.glb"); m = mesh_obj()
d = json.load(open(f"{J}/tpose_kp.json"))
arm = build_rig(joints(d["kp"], d["size"], d["bbox"], m), m)
bpy.ops.wm.save_as_mainfile(filepath=f"{J}/rigged.blend")
bpy.ops.export_scene.gltf(filepath=f"{J}/rigged.glb", export_format='GLB')
elif cmd == "pose":
bpy.ops.wm.open_mainfile(filepath=f"{J}/rigged.blend"); m = mesh_obj(); arm = bpy.data.objects["Rig"]
t = json.load(open(f"{J}/tpose_kp.json")); o = json.load(open(f"{J}/target_kp.json"))
# target joints use the ORIGINAL image's own bbox for scale, same mapping
import numpy as np
pts = np.array(o["kp"]); bbox = [pts[:,0].min(), pts[:,1].min(), pts[:,0].max(), pts[:,1].max()]
pose_to(arm, joints(o["kp"], o["size"], bbox, m))
export_all(arm, m); bpy.ops.wm.save_as_mainfile(filepath=f"{J}/posed.blend")
elif cmd == "uv":
bpy.ops.wm.open_mainfile(filepath=f"{J}/posed.blend"); m = bpy.data.objects["Posed"]
bpy.context.view_layer.objects.active = m
if not m.data.uv_layers:
bpy.ops.object.mode_set(mode='EDIT'); bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.uv.smart_project(island_margin=0.003); bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT'); bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.uv.export_layout(filepath=f"{J}/uv_layout.png", size=(2048, 2048))
bpy.ops.object.mode_set(mode='OBJECT')
img = next(n.image for mat in m.data.materials for n in mat.node_tree.nodes
if n.type == 'TEX_IMAGE')
img.filepath_raw = f"{J}/texture.png"; img.file_format = 'PNG'; img.save()
elif cmd == "retexture":
bpy.ops.wm.open_mainfile(filepath=f"{J}/posed.blend"); m = bpy.data.objects["Posed"]
new = bpy.data.images.load(f"{J}/new_texture.png")
for mat in m.data.materials:
for n in mat.node_tree.nodes:
if n.type == 'TEX_IMAGE': n.image = new
arm = None; export_all(arm, m)