74 lines
4.2 KiB
Python
74 lines
4.2 KiB
Python
"""Anatomical weights for STL arms; avoid rolling helper-bone discontinuities."""
|
|
import numpy as np,bmesh
|
|
from mathutils import Vector
|
|
|
|
def clean_arm_contacts(obj):
|
|
if obj.get("arm_contacts_cleaned",False):return
|
|
path=[Vector(p) for p in [(.150,.015,1.70),(.170,.025,1.57),(.190,.035,1.40),(.235,.005,1.25),(.257,-.015,1.10),(.270,-.035,1.00)]]
|
|
bm=bmesh.new();bm.from_mesh(obj.data);region=bm.verts.layers.int.get('SemanticRegion');remove=[]
|
|
# The printed boots touch at the centre. Split that contact into two capped
|
|
# soles, otherwise one walking foot drags triangles from the other shoe.
|
|
lower=[f for f in bm.faces if all(v[region]==0 and v.co.z<.96 for v in f.verts)]
|
|
geom=set(lower)
|
|
for f in lower: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(v[region]==0 and abs(v.co.x)<1e-6 and v.co.z<.96 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)
|
|
foot_side=bm.verts.layers.int.new('FootSide')
|
|
for vertex in bm.verts:
|
|
if vertex[region]==0 and vertex.co.z<.95:
|
|
sign=1 if sum(f.calc_center_median().x for f in vertex.link_faces)>0 else -1
|
|
vertex[foot_side]=sign
|
|
if abs(vertex.co.x)<1e-6:vertex.co.x=sign*.0003
|
|
|
|
for face in bm.faces:
|
|
if not all(vertex[region] in [4,5] for vertex in face.verts):continue
|
|
p=face.calc_center_median();p.x=abs(p.x);p.z+=.15
|
|
if p.z<1.13 or p.z>1.65:continue
|
|
distance=10
|
|
for a,b in zip(path[:-1],path[1:]):
|
|
delta=b-a;t=max(0,min(1,(p-a).dot(delta)/delta.length_squared));distance=min(distance,(p-a-delta*t).length)
|
|
radius=.046 if p.z>1.30 else .042
|
|
if distance>radius:remove.append(face)
|
|
if remove:bmesh.ops.delete(bm,geom=remove,context='FACES')
|
|
for side in [4,5]:
|
|
edges=[e for e in bm.edges if e.is_boundary and all(v[region]==side for v in e.verts)]
|
|
if edges:bmesh.ops.holes_fill(bm,edges=edges,sides=0)
|
|
bmesh.ops.recalc_face_normals(bm,faces=list(bm.faces));bmesh.ops.triangulate(bm,faces=list(bm.faces));bm.to_mesh(obj.data);bm.free();obj.data.validate(clean_customdata=False);obj.data.update()
|
|
obj['arm_contacts_cleaned']=True
|
|
print('INFERNAL trimmed print-contact arm fins',len(remove),flush=True)
|
|
|
|
def refine_weights(obj):
|
|
clean_arm_contacts(obj)
|
|
attrs=obj.data.attributes['SemanticRegion'].data
|
|
names={g.name:g for g in obj.vertex_groups}
|
|
for vertex in obj.data.vertices:
|
|
x,y,z=vertex.co;zo=z+.15;region=attrs[vertex.index].value
|
|
if region==0 and z<.95:
|
|
side='L' if x>0 else 'R'
|
|
if z<.18:
|
|
for group in list(vertex.groups):obj.vertex_groups[group.group].remove([vertex.index])
|
|
names['foot.'+side].add([vertex.index],1,'REPLACE')
|
|
else:
|
|
for group in list(vertex.groups):
|
|
name=obj.vertex_groups[group.group].name
|
|
if name.endswith('.R' if side=='L' else '.L'):obj.vertex_groups[group.group].remove([vertex.index])
|
|
total=sum(group.weight for group in vertex.groups)
|
|
for group in list(vertex.groups):obj.vertex_groups[group.group].add([vertex.index],group.weight/max(total,1e-8),'REPLACE')
|
|
if region not in [4,5]:continue
|
|
side='L' if x>0 else 'R'
|
|
if zo>1.65 and y>.085:
|
|
weights={'Wing.'+side:1.0}
|
|
else:
|
|
wrist=float(np.clip((1.17-zo)/.08,0,1))
|
|
upper=float(np.clip((zo-1.36)/.12,0,1))
|
|
shoulder=float(np.clip((zo-1.62)/.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())
|
|
for group in list(vertex.groups):obj.vertex_groups[group.group].remove([vertex.index])
|
|
for name,value in weights.items():
|
|
if value>1e-5:names[name].add([vertex.index],value/total,'REPLACE')
|