187 lines
12 KiB
Python
187 lines
12 KiB
Python
"""Adapt approved Meshy paint to the existing Infernal rig without paid services.
|
|
Blender 5.0: --background --python-exit-code 1 --python tools/build_meshy_succubus.py
|
|
"""
|
|
import bpy,bmesh,json,math
|
|
from pathlib import Path
|
|
from mathutils import Vector
|
|
from mathutils.kdtree import KDTree
|
|
import numpy as np
|
|
ROOT=Path(__file__).resolve().parent.parent
|
|
OUT=ROOT/'Demon_v0.19/art/succubus/meshy_v02';OUT.mkdir(parents=True,exist_ok=True)
|
|
QA=ROOT/'Demon_v0.19/captures/meshy-integration';QA.mkdir(parents=True,exist_ok=True)
|
|
def log(*v):print('MESHY_GAME',*v,flush=True)
|
|
def active(o):
|
|
bpy.ops.object.select_all(action='DESELECT');o.select_set(True);bpy.context.view_layer.objects.active=o
|
|
bpy.ops.wm.open_mainfile(filepath=str(ROOT/'tools/model_sources/infernal_game.blend'))
|
|
scene=bpy.context.scene;source=bpy.data.objects['Infernal_Game_Mesh'];rig=bpy.data.objects['INFERNAL_RIG']
|
|
rig.animation_data.action=None
|
|
for tr in rig.animation_data.nla_tracks:tr.mute=True
|
|
for bone in rig.pose.bones:bone.matrix_basis.identity()
|
|
bpy.context.view_layer.update()
|
|
regions=source.data.attributes['SemanticRegion'].data
|
|
samples=[v.co.copy() for v in source.data.vertices]
|
|
source_weights=[[(source.vertex_groups[g.group].name,g.weight) for g in v.groups if g.weight>1e-6] for v in source.data.vertices]
|
|
tree=KDTree(len(samples))
|
|
for i,v in enumerate(samples):tree.insert(v,i)
|
|
tree.balance()
|
|
parts={}
|
|
for role in range(6):
|
|
ids=[i for i in range(len(samples)) if regions[i].value==role];kd=KDTree(len(ids))
|
|
for i in ids:kd.insert(samples[i],i)
|
|
kd.balance();parts[role]=kd
|
|
leg_trees={}
|
|
for sign in [-1,1]:
|
|
ids=[i for i in range(len(samples)) if regions[i].value==0 and samples[i].x*sign>0 and any(w>.01 and not n.endswith('.R' if sign>0 else '.L') for n,w in source_weights[i])]
|
|
kd=KDTree(len(ids))
|
|
for i in ids:kd.insert(samples[i],i)
|
|
kd.balance();leg_trees[sign]=kd
|
|
bpy.ops.import_scene.gltf(filepath=str(ROOT/'tools/model_sources/meshy_v01/painted_v02/succubus_painted.glb'))
|
|
obj=next(o for o in bpy.context.selected_objects if o.type=='MESH');obj.name='Meshy_Succubus_Game'
|
|
active(obj);bpy.ops.object.transform_apply(location=True,rotation=True,scale=True)
|
|
lo=min(v.co.z for v in obj.data.vertices);hi=max(v.co.z for v in obj.data.vertices);scale=2.050177/(hi-lo)
|
|
for v in obj.data.vertices:v.co=Vector((v.co.x*scale,v.co.y*scale,(v.co.z-lo)*scale))
|
|
modifier=obj.modifiers.new('Realtime topology preserving UV','DECIMATE');modifier.ratio=100000/1351422;modifier.use_collapse_triangulate=True
|
|
bpy.ops.object.modifier_apply(modifier=modifier.name)
|
|
# Remove any fused shoe contact, retaining interpolated UVs on the cut.
|
|
bm=bmesh.new();bm.from_mesh(obj.data)
|
|
faces=[f for f in bm.faces if all(v.co.z<.98 for v in f.verts)]
|
|
geom=set(faces)
|
|
for f in faces:geom.update(f.verts);geom.update(f.edges)
|
|
result=bmesh.ops.bisect_plane(bm,geom=list(geom),plane_co=(0,0,0),plane_no=(1,0,0),dist=1e-7)
|
|
cut=[e for e in result['geom_cut'] if isinstance(e,bmesh.types.BMEdge)]
|
|
if cut:bmesh.ops.split_edges(bm,edges=cut)
|
|
for sign in [-1,1]:
|
|
boundary=[e for e in bm.edges if e.is_boundary and all(abs(v.co.x)<1e-6 and v.co.z<.98 for v in e.verts) and e.link_faces and e.link_faces[0].calc_center_median().x*sign>0]
|
|
if boundary:bmesh.ops.holes_fill(bm,edges=boundary,sides=0)
|
|
bmesh.ops.triangulate(bm,faces=list(bm.faces));bm.to_mesh(obj.data);bm.free();obj.data.update()
|
|
# Establish surface ownership before posing, so wings cannot receive hand weights.
|
|
roles=[];distances=[]
|
|
# Classify exposed arm surfaces using the approved red skin texture, including
|
|
# the rear of the forearm; spatial nearest-source alone confuses these with wings.
|
|
mask_data=np.load(ROOT/'tools/model_sources/meshy_v01/painted_v02/skin_mask.npz')
|
|
image=mask_data['skin'];uv=obj.data.uv_layers.active.data
|
|
skin=np.zeros(len(obj.data.vertices),dtype=bool)
|
|
for loop in obj.data.loops:
|
|
u,v=uv[loop.index].uv
|
|
if image[int((1-v)%1*(image.shape[0]-1)),int(u%1*(image.shape[1]-1))]:skin[loop.vertex_index]=True
|
|
arm_ids=[]
|
|
for v in obj.data.vertices:
|
|
x,y,z=v.co;minimum=float(np.interp(z,[.75,1.0,1.25,1.42,1.58],[.25,.21,.18,.15,.14]))
|
|
if skin[v.index] and .72<z<1.58 and abs(x)>minimum and y<.13:arm_ids.append(v.index)
|
|
arm_tree=KDTree(len(arm_ids))
|
|
for i in arm_ids:arm_tree.insert(obj.data.vertices[i].co,i)
|
|
arm_tree.balance()
|
|
for v in obj.data.vertices:
|
|
p=v.co;_,index,d=tree.find(p);role=regions[index].value
|
|
_,_,arm_distance=arm_tree.find(p)
|
|
if arm_distance<.018:role=4 if p.x>0 else 5
|
|
elif role in [4,5]:role=0 if abs(p.x)<.20 else (1 if p.x>0 else 2)
|
|
roles.append(role);distances.append(d)
|
|
log('red arm seeds',len(arm_ids),'arm surface vertices',sum(r in [4,5] for r in roles))
|
|
# Duplicate vertices per surface owner while copying every face-corner UV.
|
|
old=obj.data;vertices=[];faces=[];mapping={};new_roles=[];uvs=[]
|
|
for face in old.polygons:
|
|
labels=[roles[i] for i in face.vertices];role=max(set(labels),key=labels.count);indices=[]
|
|
for index in face.vertices:
|
|
key=(role,index)
|
|
if key not in mapping:
|
|
mapping[key]=len(vertices);vertices.append(tuple(old.vertices[index].co));new_roles.append(role)
|
|
indices.append(mapping[key])
|
|
faces.append(indices)
|
|
if old.uv_layers.active:uvs.extend([tuple(old.uv_layers.active.data[i].uv) for i in face.loop_indices])
|
|
mesh=bpy.data.meshes.new('Meshy separated anatomical surfaces');mesh.from_pydata(vertices,[],faces);mesh.update()
|
|
for material in old.materials:mesh.materials.append(material)
|
|
if uvs:
|
|
uv=mesh.uv_layers.new(name='UVMap')
|
|
for entry,value in zip(uv.data,uvs):entry.uv=value
|
|
obj.data=mesh;roles=new_roles
|
|
for bone in rig.data.bones:obj.vertex_groups.new(name=bone.name)
|
|
for face in mesh.polygons:assert len({roles[i] for i in face.vertices})==1
|
|
for v,role in zip(obj.data.vertices,roles):
|
|
p=v.co.copy();x,y,z=p;sign=1 if x>0 else -1
|
|
if abs(x)<.00001 and z<.95:
|
|
face_x=sum(f.center.x for f in obj.data.polygons if v.index in f.vertices)
|
|
sign=1 if face_x>=0 else -1
|
|
x=sign*.0003;p.x=x;v.co=p
|
|
if role in [4,5] and z<1.54:
|
|
old_x=float(np.interp(z,[.81,.93,1.09,1.25,1.42,1.54],[.275,.257,.228,.194,.170,.153]))
|
|
new_x=float(np.interp(z,[.81,.95,1.10,1.25,1.42,1.54],[.38,.355,.31,.265,.205,.16]))
|
|
p.x-=sign*(new_x-old_x) # Query only: never move a subset of the visible arm.
|
|
if role in [4,5]:
|
|
side='L' if role==4 else 'R'
|
|
wrist=float(np.clip((1.02-z)/.08,0,1));upper=float(np.clip((z-1.21)/.12,0,1));shoulder=float(np.clip((z-1.47)/.09,0,1))
|
|
weights={'wrist.'+side:wrist,'lowerarm01.'+side:(1-wrist)*(1-upper),'upperarm01.'+side:upper*(1-shoulder),'spine02':upper*shoulder}
|
|
total=sum(weights.values());weights={n:w/total for n,w in weights.items() if w>1e-6}
|
|
elif role==0 and z<.18:
|
|
weights={'foot.L' if sign>0 else 'foot.R':1.0}
|
|
else:
|
|
weights={}
|
|
kd=leg_trees[sign] if role==0 and z<.95 else parts[role]
|
|
for _,index,distance in kd.find_n(p,8):
|
|
factor=1/max(.002,distance)**2
|
|
for name,w in source_weights[index]:
|
|
if role==0 and z<.95 and name.endswith('.R' if sign>0 else '.L'):continue
|
|
weights[name]=weights.get(name,0)+w*factor
|
|
pairs=sorted(weights.items(),key=lambda kv:-kv[1])[:4];total=sum(w for _,w in pairs);weights={n:w/total for n,w in pairs}
|
|
assert weights and all(math.isfinite(w) for w in weights.values()),(v.index,tuple(p),role)
|
|
for n,w in weights.items():obj.vertex_groups[n].add([v.index],w,'REPLACE')
|
|
# Keep coincident shoulder seam vertices on the same continuous skin field.
|
|
names=[g.name for g in obj.vertex_groups];name_to_index={n:i for i,n in enumerate(names)}
|
|
weights_array=np.zeros((len(obj.data.vertices),len(names)),dtype=np.float32)
|
|
coords=np.array([tuple(v.co) for v in obj.data.vertices]);shoulder=(np.abs(coords[:,0])>.105)&(np.abs(coords[:,0])<.34)&(coords[:,2]>1.34)&(coords[:,2]<1.65)&(coords[:,1]<.13)&np.isin(np.array(roles),[0,4,5])
|
|
seams={}
|
|
for v in obj.data.vertices:
|
|
for g in v.groups:weights_array[v.index,g.group]=g.weight
|
|
if shoulder[v.index] and roles[v.index] in [0,4,5]:seams.setdefault(tuple(np.round(v.co,5)),[]).append(v.index)
|
|
edges=np.array([tuple(e.vertices) for e in obj.data.edges],dtype=np.int32)
|
|
mask=shoulder[edges[:,0]]&shoulder[edges[:,1]]&np.isin(np.array(roles)[edges[:,0]],[0,4,5])&np.isin(np.array(roles)[edges[:,1]],[0,4,5]);smooth=edges[mask]
|
|
first=np.concatenate((smooth[:,0],smooth[:,1]));second=np.concatenate((smooth[:,1],smooth[:,0]));counts=np.maximum(np.bincount(first,minlength=len(coords)),1)
|
|
for iteration in range(10):
|
|
average=np.zeros_like(weights_array)
|
|
for column in range(len(names)):average[:,column]=np.bincount(first,weights=weights_array[second,column],minlength=len(coords))/counts
|
|
weights_array[shoulder]=weights_array[shoulder]*.4+average[shoulder]*.6
|
|
for indices in seams.values():
|
|
if len(indices)>1:weights_array[indices]=weights_array[indices].mean(axis=0)
|
|
for v in obj.data.vertices:
|
|
if not shoulder[v.index]:continue
|
|
w=weights_array[v.index];indices=np.argsort(w)[-4:];total=float(w[indices].sum())
|
|
if total<1e-8:continue
|
|
for g in list(v.groups):obj.vertex_groups[g.group].remove([v.index])
|
|
for i in indices:
|
|
if w[i]/total>1e-6:obj.vertex_groups[int(i)].add([v.index],float(w[i])/total,'REPLACE')
|
|
for f in obj.data.polygons:f.use_smooth=True
|
|
modifier=obj.modifiers.new('Approved animation skeleton','ARMATURE');modifier.object=rig;obj.parent=rig
|
|
# UV and all PBR textures from Meshy remain attached to the optimized surface.
|
|
obj.data.update();bpy.context.view_layer.update()
|
|
report={'source_triangles':1351422,'game_vertices':len(obj.data.vertices),'game_triangles':sum(len(f.vertices)-2 for f in obj.data.polygons),'bones':len(rig.data.bones),'clips':[t.name for t in rig.animation_data.nla_tracks],'max_nearest_source_distance':max(distances),'textures':[list(i.size) for i in bpy.data.images if i.size[0]>0 and i.users>0],'source_task':'01a18454-81f3-762f-b9b7-57b5e54af4a9'}
|
|
for v in obj.data.vertices:assert abs(sum(g.weight for g in v.groups)-1)<1e-4
|
|
edges=np.array([tuple(e.vertices) for e in obj.data.edges],dtype=np.int32)
|
|
base=np.array([tuple(v.co) for v in obj.data.vertices]);rest_lengths=np.linalg.norm(base[edges[:,0]]-base[edges[:,1]],axis=1)
|
|
checks={}
|
|
for track in rig.animation_data.nla_tracks:
|
|
track.mute=False;values=[]
|
|
for frame in range(1,int(track.strips[0].frame_end)+1,8):
|
|
scene.frame_set(frame);bpy.context.view_layer.update();evaluated=obj.evaluated_get(bpy.context.evaluated_depsgraph_get());mesh=evaluated.to_mesh();coords=np.array([tuple(v.co) for v in mesh.vertices]);evaluated.to_mesh_clear()
|
|
assert np.isfinite(coords).all()
|
|
growth=np.linalg.norm(coords[edges[:,0]]-coords[edges[:,1]],axis=1)-rest_lengths
|
|
values.append(float(np.max(growth)))
|
|
if values[-1]>.12:
|
|
ix=int(np.argmax(growth));log('EDGE',track.name,frame,values[-1],[list(base[j]) for j in edges[ix]],[roles[j] for j in edges[ix]])
|
|
checks[track.name]={'max_edge_growth_m':max(values)};track.mute=True
|
|
report['deformation_checks']=checks
|
|
assert all(c['max_edge_growth_m']<.12 for c in checks.values()),checks
|
|
log(json.dumps(report))
|
|
for o in list(scene.objects):
|
|
if o not in [obj,rig] and o.name!='Infernal animated whip':bpy.data.objects.remove(o,do_unlink=True)
|
|
for tr in rig.animation_data.nla_tracks:tr.mute=False
|
|
rig.animation_data.action=None;scene.frame_set(1)
|
|
for bone in rig.pose.bones:bone.matrix_basis.identity()
|
|
active(obj);rig.select_set(True)
|
|
whip=bpy.data.objects.get('Infernal animated whip')
|
|
if whip:whip.select_set(True)
|
|
bpy.ops.export_scene.gltf(filepath=str(OUT/'succubus_game.glb'),export_format='GLB',use_selection=True,export_animations=True,export_animation_mode='NLA_TRACKS',export_nla_strips=True,export_force_sampling=True,export_frame_range=False,export_skins=True,export_materials='EXPORT',export_yup=True)
|
|
(OUT/'manifest.json').write_text(json.dumps(report,indent=2),encoding='utf-8')
|
|
bpy.context.preferences.filepaths.save_version=0
|
|
bpy.ops.wm.save_as_mainfile(filepath=str(QA/'meshy_game.blend'))
|
|
log('EXPORT READY',OUT/'succubus_game.glb')
|