Last active
June 14, 2024 09:36
-
-
Save undefined9071/7d7611ae51bf3d695aded19f4d108e5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Original source code: https://github.com/artellblender/empties_to_bones | |
# Author: artellblender | |
# This code has been modified to create bones for an existing armature. | |
import math | |
import bpy | |
from mathutils import Euler, Matrix, Vector | |
armature_name = "Avatar_Girl_Claymore_Noel_Manekin" | |
empty_parent_ary = [ | |
{"empty": "+HairS L B01", "parent": "Bip001 Head"}, | |
{"empty": "+HairS L B02", "parent": "+HairS L B01"}, | |
{"empty": "+HairS L B03", "parent": "+HairS L B02"}, | |
] | |
tail_multiply = 0.06 | |
def mat3_to_vec_roll(mat): | |
vec = mat.col[1] | |
vecmat = vec_roll_to_mat3(mat.col[1], 0) | |
vecmatinv = vecmat.inverted() | |
rollmat = vecmatinv @ mat | |
roll = math.atan2(rollmat[0][2], rollmat[2][2]) | |
return vec, roll | |
def vec_roll_to_mat3(vec, roll): | |
target = Vector((0, 0.1, 0)) | |
nor = vec.normalized() | |
axis = target.cross(nor) | |
if axis.dot(axis) > 0.0000000001: | |
axis.normalize() | |
theta = target.angle(nor) | |
bMatrix = Matrix.Rotation(theta, 3, axis) | |
else: | |
updown = 1 if target.dot(nor) > 0 else -1 | |
bMatrix = Matrix.Scale(updown, 3) | |
bMatrix[2][2] = 1.0 | |
rMatrix = Matrix.Rotation(roll, 3, nor) | |
mat = rMatrix @ bMatrix | |
return mat | |
def get_edit_bone(name): | |
return bpy.context.object.data.edit_bones.get(name) | |
armature = bpy.data.objects.get(armature_name) | |
for obj in empty_parent_ary: | |
empty_name = obj["empty"] | |
parent_name = obj["parent"] | |
emp = bpy.data.objects.get(empty_name) | |
vec, roll = mat3_to_vec_roll(emp.matrix_world.to_3x3()) | |
new_bone = armature.data.edit_bones.new(empty_name) | |
new_bone.head = emp.matrix_world.to_translation() | |
new_bone.tail = new_bone.head + (vec) * tail_multiply | |
new_bone.roll = roll | |
bone_parent = get_edit_bone(parent_name) | |
get_edit_bone(new_bone.name).parent = bone_parent |
Author
undefined9071
commented
Dec 17, 2023
- Edit L6-L11
- Run
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment