my logic cleanup
This commit is contained in:
parent
101e8bc86b
commit
97d294e559
93
__init__.py
93
__init__.py
@ -135,54 +135,6 @@ def add_capped_frustum(bm: bmesh.types.BMesh,
|
||||
pass
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Bone data collection — mode-aware
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def collect_bone_data(arm_obj) -> list:
|
||||
"""
|
||||
Returns a list of dicts with world-space head/tail and radii.
|
||||
Reads from the appropriate source based on the armature's current mode:
|
||||
OBJECT → arm.bones (rest pose)
|
||||
POSE → obj.pose.bones (current posed positions)
|
||||
EDIT → arm.edit_bones (what you see in edit mode)
|
||||
"""
|
||||
mode = arm_obj.mode
|
||||
world_mat = arm_obj.matrix_world
|
||||
result = []
|
||||
|
||||
if mode == 'POSE':
|
||||
for pb in arm_obj.pose.bones:
|
||||
b = pb.bone # underlying data bone for radii
|
||||
result.append({
|
||||
'head': world_mat @ pb.head,
|
||||
'tail': world_mat @ pb.tail,
|
||||
'head_r': max(b.head_radius, 0.001),
|
||||
'tail_r': max(b.tail_radius, 0.001),
|
||||
})
|
||||
|
||||
elif mode == 'EDIT':
|
||||
# edit_bones is accessible while in edit mode
|
||||
for eb in arm_obj.data.edit_bones:
|
||||
result.append({
|
||||
'head': world_mat @ eb.head,
|
||||
'tail': world_mat @ eb.tail,
|
||||
'head_r': max(eb.head_radius, 0.001),
|
||||
'tail_r': max(eb.tail_radius, 0.001),
|
||||
})
|
||||
|
||||
else: # OBJECT / everything else → rest pose
|
||||
for bone in arm_obj.data.bones:
|
||||
result.append({
|
||||
'head': world_mat @ bone.head_local,
|
||||
'tail': world_mat @ bone.tail_local,
|
||||
'head_r': max(bone.head_radius, 0.001),
|
||||
'tail_r': max(bone.tail_radius, 0.001),
|
||||
})
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Operator
|
||||
# ---------------------------------------------------------------------------
|
||||
@ -213,24 +165,43 @@ class ARMATURE_OT_build_envelope_mesh(bpy.types.Operator):
|
||||
|
||||
# Snapshot bone data NOW, while we're still in the original mode.
|
||||
# This is especially important for EDIT mode where edit_bones are live.
|
||||
bones_data = collect_bone_data(arm_obj)
|
||||
# bones_data = collect_bone_data(arm_obj) // we arent doing this anymore
|
||||
|
||||
mode = arm_obj.mode
|
||||
world_mat = arm_obj.matrix_world
|
||||
|
||||
bm = bmesh.new()
|
||||
segs = self.segments
|
||||
|
||||
def parts(mat, rad):
|
||||
return (world_mat @ mat, max(rad, 0.001))
|
||||
|
||||
def meshify(head_matrix, head_radius, tail_matrix, tail_radius):
|
||||
|
||||
head_matrix_final, head_radius_final = parts(head_matrix, head_radius)
|
||||
tail_matrix_final, tail_radius_final = parts(tail_matrix, tail_radius)
|
||||
|
||||
add_sphere(bm, head_matrix_final, head_radius_final, segs)
|
||||
add_sphere(bm, tail_matrix_final, tail_radius_final, segs)
|
||||
add_capped_frustum(bm,
|
||||
head_matrix_final, head_radius_final,
|
||||
tail_matrix_final, tail_radius_final,
|
||||
segs)
|
||||
|
||||
if mode == 'EDIT':
|
||||
# edit_bones is accessible while in edit mode
|
||||
for bone in arm_obj.data.edit_bones:
|
||||
meshify(bone.head, bone.head_radius, bone.tail, bone.tail_radius)
|
||||
|
||||
else: # OBJECT / everything else → rest pose
|
||||
for bone in arm_obj.data.bones:
|
||||
meshify(bone.head_local, bone.head_radius, bone.tail_local, bone.tail_radius)
|
||||
|
||||
|
||||
# Must be in OBJECT mode to create and link new mesh objects.
|
||||
if original_mode != 'OBJECT':
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
|
||||
# Build the BMesh
|
||||
bm = bmesh.new()
|
||||
segs = self.segments
|
||||
|
||||
for bd in bones_data:
|
||||
add_sphere(bm, bd['head'], bd['head_r'], segs)
|
||||
add_sphere(bm, bd['tail'], bd['tail_r'], segs)
|
||||
add_capped_frustum(bm,
|
||||
bd['head'], bd['head_r'],
|
||||
bd['tail'], bd['tail_r'],
|
||||
segs)
|
||||
|
||||
# Merge verts from shared joints (parent/child bone connections)
|
||||
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.0001)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user