Skip to content

Instantly share code, notes, and snippets.

@Dragorn421
Created October 30, 2021 09:49
Show Gist options
  • Save Dragorn421/66a6f82e9a720d2a093ecfbbe3648642 to your computer and use it in GitHub Desktop.
Save Dragorn421/66a6f82e9a720d2a093ecfbbe3648642 to your computer and use it in GitHub Desktop.
Meant to help figure out MATRIX() arguments in zzplayas manifests when reusing dlists across limbs
"""
Blender 2.79-2.93 script
Meant to help figure out MATRIX() arguments in zzplayas manifests
when reusing dlists across limbs
(for example, if the hilt of the kokiri sword is shared between
the left hand limb and the sheath limb)
Run this by copypasting into a new text block in the Scripting layout
Instructions:
Be in object mode
Have the armature rest pose **folded**
Change the names below
Run script
Find mesh object "Hilt_sheathed_temp" (rigged to the armature),
the Location/Rotation/Scale part of the N-panel is then basically
the same as the z64online matrix editor.
The transforms can also be changed in the viewport but it's a bit
confusing to transform in, unless you have the armature in rest pose.
"""
# Change these three names according to what your blend uses
source_object_name = "Hilt.1" # name of the mesh object with the hilt
source_limb_name = "limb_15" # left hand
target_limb_name = "limb_19" # sheath limb
temp_object_name = "Hilt_sheathed_temp"
temp_mesh_name = temp_object_name + "_mesh"
import bpy
import bmesh
assert bpy.context.mode == "OBJECT"
scene = bpy.context.scene
source_object = bpy.data.objects[source_object_name]
assert source_object.type == "MESH"
assert len(source_object.modifiers) == 1
assert source_object.modifiers[0].type == "ARMATURE"
assert source_limb_name in source_object.vertex_groups
source_vertex_group_index = source_object.vertex_groups.find(source_limb_name)
assert source_vertex_group_index >= 0
armature = source_object.find_armature()
assert armature is not None
armature_bones = armature.data.bones
assert source_limb_name in armature_bones
assert target_limb_name in armature_bones
source_limb = armature_bones[source_limb_name]
target_limb = armature_bones[target_limb_name]
if temp_object_name in bpy.data.objects:
bpy.data.objects.remove(bpy.data.objects[temp_object_name])
if temp_mesh_name in bpy.data.meshes:
bpy.data.meshes.remove(bpy.data.meshes[temp_mesh_name])
temp_mesh = source_object.data.copy()
temp_mesh.name = temp_mesh_name
temp_obj = source_object.copy()
temp_obj.name = temp_object_name
temp_obj.data = temp_mesh
if bpy.app.version < (2, 80, 0):
scene.objects.link(temp_obj)
scene.update()
else:
scene.collection.objects.link(temp_obj)
bpy.context.view_layer.update()
bm = bmesh.new()
bm.from_mesh(temp_mesh)
vertices_not_rigged_to_source_limb = [
vert for vert in bm.verts
if (
source_vertex_group_index not in vert[bm.verts.layers.deform.active]
or vert[bm.verts.layers.deform.active][source_vertex_group_index] == 0
)
]
print("deleting vertices", len(vertices_not_rigged_to_source_limb))
bmesh.ops.delete(bm,
geom=vertices_not_rigged_to_source_limb,
context=(1 if bpy.app.version < (2,80,0) else "VERTS"))
bmesh.ops.translate(bm, vec=-source_limb.head_local, verts=bm.verts)
bm.to_mesh(temp_mesh)
bm.free()
if target_limb_name in temp_obj.vertex_groups:
temp_obj.vertex_groups.remove(temp_obj.vertex_groups[target_limb_name])
temp_obj.vertex_groups[source_vertex_group_index].name = target_limb_name
displace = target_limb.head_local
for i in range(3):
direction = "XYZ"[i]
amount = displace[i]
modifier = temp_obj.modifiers.new("Displace " + direction, "DISPLACE")
modifier.direction = direction
modifier.space = "GLOBAL"
modifier.mid_level = 0
modifier.strength = amount
ctx = {
"object": temp_obj,
"modifier": modifier,
}
for k in ("screen", "area", "window"):
ctx[k] = getattr(bpy.context, k)
while temp_obj.modifiers.find(modifier.name) > 0:
bpy.ops.object.modifier_move_up(ctx, modifier=modifier.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment