Replace imps with rigged swamp demon and adapt gait and activities
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
"""Import the supplied textured FBX, add a game rig, and export a skinned GLB.
|
||||
Run with Blender 5: blender -b --python tools/build_swamp_imp.py
|
||||
"""
|
||||
import bpy, json, hashlib
|
||||
from pathlib import Path
|
||||
from mathutils import Vector
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
SOURCE=next((ROOT/'tools/model_sources/swamp_demon').rglob('*.fbx'))
|
||||
OUT=ROOT/'Demon_v0.19/art/imps/swamp_v01';OUT.mkdir(parents=True,exist_ok=True)
|
||||
bpy.ops.wm.read_factory_settings(use_empty=True)
|
||||
bpy.ops.import_scene.fbx(filepath=str(SOURCE))
|
||||
mesh=next(o for o in bpy.context.scene.objects if o.type=='MESH')
|
||||
bpy.context.view_layer.objects.active=mesh;mesh.select_set(True)
|
||||
bpy.ops.object.transform_apply(location=True,rotation=True,scale=True)
|
||||
coords=[v.co.copy() for v in mesh.data.vertices];lo=min(v.z for v in coords);hi=max(v.z for v in coords)
|
||||
scale=1.28/(hi-lo)
|
||||
for v in mesh.data.vertices:v.co=Vector((v.co.x*scale,v.co.y*scale,(v.co.z-lo)*scale))
|
||||
# Joint positions use Godot's +Z forward and +Y up. Preserve the crouched silhouette.
|
||||
joints={
|
||||
'Body':([0,.57,-.10],None),
|
||||
'Head':([0,.78,.20],'Body'),
|
||||
'LegL':([-.24,.43,-.16],'Body'),'KneeL':([-.27,.27,-.20],'LegL'),'FootL':([-.28,.065,.16],'KneeL'),
|
||||
'LegR':([.24,.43,-.16],'Body'),'KneeR':([.27,.27,-.20],'LegR'),'FootR':([.28,.065,.16],'KneeR'),
|
||||
'ArmL':([-.35,.80,-.03],'Body'),'ElbowL':([-.46,.45,.12],'ArmL'),'HandL':([-.47,.16,.30],'ElbowL'),
|
||||
'ArmR':([.35,.80,-.03],'Body'),'ElbowR':([.46,.45,.12],'ArmR'),'HandR':([.47,.16,.30],'ElbowR')}
|
||||
def bv(p):return Vector((p[0],-p[2],p[1]))
|
||||
tips={'Body':[0,1.02,-.25],'Head':[0,.78,.55]}
|
||||
for side,sign in [('L',-1),('R',1)]:
|
||||
tips['Leg'+side]=joints['Knee'+side][0];tips['Knee'+side]=joints['Foot'+side][0];tips['Foot'+side]=[sign*.28,.065,.39]
|
||||
tips['Arm'+side]=joints['Elbow'+side][0];tips['Elbow'+side]=joints['Hand'+side][0];tips['Hand'+side]=[sign*.47,.06,.40]
|
||||
armdata=bpy.data.armatures.new('SwampRig');arm=bpy.data.objects.new('SwampRig',armdata);bpy.context.collection.objects.link(arm)
|
||||
bpy.context.view_layer.objects.active=arm;mesh.select_set(False);arm.select_set(True);bpy.ops.object.mode_set(mode='EDIT')
|
||||
for name,(head,parent) in joints.items():
|
||||
b=armdata.edit_bones.new(name);b.head=bv(head);b.tail=bv(tips[name]);
|
||||
if parent:b.parent=armdata.edit_bones[parent]
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
# Heat weights follow connected anatomy around shoulders, hips and elbows.
|
||||
bpy.ops.object.select_all(action='DESELECT');mesh.select_set(True);arm.select_set(True);bpy.context.view_layer.objects.active=arm
|
||||
bpy.ops.object.parent_set(type='ARMATURE_AUTO')
|
||||
assert any(len(v.groups)>0 for v in mesh.data.vertices),'Automatic weights failed'
|
||||
# The crouched source has hands almost touching feet. Prevent heat bleeding
|
||||
# between those close surfaces while retaining the shoulder/hip heat solution.
|
||||
for v in mesh.data.vertices:
|
||||
x=abs(v.co.x);h=v.co.z;forward=-v.co.y;side='R' if v.co.x>0 else 'L'
|
||||
allowed=None
|
||||
if x<.14 and forward>.32:allowed=['Head']
|
||||
elif h<.50 and x<.37 and x>.13:allowed=['Body','Leg'+side,'Knee'+side,'Foot'+side]
|
||||
elif h>1.03:allowed=['Head'] if forward>.10 else ['Body']
|
||||
elif h>.50 and x<.30 and forward<.05:allowed=['Body']
|
||||
elif h<.48 and x>.40:allowed=['Arm'+side,'Elbow'+side,'Hand'+side]
|
||||
elif h<.53 and x<.30 and forward<.22:allowed=['Body','Leg'+side,'Knee'+side,'Foot'+side]
|
||||
elif x<.14 and forward>.32:allowed=['Head']
|
||||
if allowed is None:continue
|
||||
scores={mesh.vertex_groups[g.group].name:g.weight for g in v.groups if mesh.vertex_groups[g.group].name in allowed}
|
||||
total=sum(scores.values())
|
||||
if total<.00001:
|
||||
def dist(n):
|
||||
a=bv(joints[n][0]);b=bv(tips[n]);d=b-a;t=max(0,min(1,(v.co-a).dot(d)/d.length_squared));return (v.co-a-d*t).length
|
||||
scores={n:1/(.025+dist(n))**5 for n in allowed};total=sum(scores.values())
|
||||
for group in mesh.vertex_groups:group.remove([v.index])
|
||||
for n,w in scores.items():mesh.vertex_groups[n].add([v.index],w/total,'REPLACE')
|
||||
|
||||
# Relax the region boundaries over mesh edges, then keep four normalized
|
||||
# influences explicitly so the exported skin matches the editable source.
|
||||
adj=[set() for _ in mesh.data.vertices]
|
||||
for edge in mesh.data.edges:
|
||||
a,b=edge.vertices;adj[a].add(b);adj[b].add(a)
|
||||
weights=[{g.group:g.weight for g in v.groups} for v in mesh.data.vertices]
|
||||
for _ in range(8):
|
||||
relaxed=[]
|
||||
for i,w in enumerate(weights):
|
||||
mixed={n:v*.65 for n,v in w.items()}
|
||||
for j in adj[i]:
|
||||
for n,v in weights[j].items():mixed[n]=mixed.get(n,0)+v*.35/max(1,len(adj[i]))
|
||||
relaxed.append(mixed)
|
||||
weights=relaxed
|
||||
for v,w in zip(mesh.data.vertices,weights):
|
||||
pairs=sorted(w.items(),key=lambda p:p[1],reverse=True)[:4];total=sum(w for _,w in pairs)
|
||||
assert total>.00001,'Unweighted vertex'
|
||||
for group in mesh.vertex_groups:group.remove([v.index])
|
||||
for n,w in pairs:mesh.vertex_groups[n].add([v.index],w/total,'REPLACE')
|
||||
|
||||
# FBX may use relative image paths; explicitly bind the provided colour texture.
|
||||
tex=next(SOURCE.parent.glob('*.png'));img=bpy.data.images.load(str(tex),check_existing=True)
|
||||
for mat in mesh.data.materials:
|
||||
mat.use_nodes=True;nodes=mat.node_tree.nodes;bs=next(n for n in nodes if n.type=='BSDF_PRINCIPLED')
|
||||
image=next((n for n in nodes if n.type=='TEX_IMAGE'),None) or nodes.new('ShaderNodeTexImage');image.image=img
|
||||
mat.node_tree.links.new(image.outputs['Color'],bs.inputs['Base Color']);bs.inputs['Roughness'].default_value=.86
|
||||
img.pack()
|
||||
mesh.name='SwampDemon';mesh.data.name='SwampDemonMesh'
|
||||
for p in mesh.data.polygons:p.use_smooth=True
|
||||
bpy.ops.wm.save_as_mainfile(filepath=str(ROOT/'tools/model_sources/swamp_demon/swamp_imp.blend'))
|
||||
bpy.ops.export_scene.gltf(filepath=str(OUT/'swamp_imp.glb'),export_format='GLB',export_yup=True,export_animations=False,export_apply=False)
|
||||
manifest={'source_file':SOURCE.name,'source_sha256':hashlib.sha256(SOURCE.read_bytes()).hexdigest(),'texture_sha256':hashlib.sha256(tex.read_bytes()).hexdigest(),'vertices':len(mesh.data.vertices),'triangles':sum(len(p.vertices)-2 for p in mesh.data.polygons),'height':1.28,'joints':{n:{'position':h,'parent':p or ''} for n,(h,p) in joints.items()},'animation':'Runtime imp_gait.gd IK and imp_activities.gd; original FBX has no rig or clips.'}
|
||||
(OUT/'manifest.json').write_text(json.dumps(manifest,ensure_ascii=False,indent=2),encoding='utf-8')
|
||||
print('SWAMP_EXPORT',json.dumps(manifest))
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
|
After Width: | Height: | Size: 3.1 MiB |
Binary file not shown.
Reference in New Issue
Block a user