-
Notifications
You must be signed in to change notification settings - Fork 1
/
rig_tools.py
91 lines (73 loc) · 3.25 KB
/
rig_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import io
import os
import _s4animtools.serialization
import _s4animtools.serialization
from _s4animtools.rcol.rcol_wrapper import RCOL
from _s4animtools.rig.create_rig import Rig
from _s4animtools.rcol.skin import VertexGroup
from _s4animtools.serialization.fnv import get_32bit_hash, get_64bithash
import bpy
class ExportRig(bpy.types.Operator):
bl_idname = "s4animtools.export_rig"
bl_label = "Export Rig"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
armature = bpy.context.object.data
bpy.ops.object.mode_set(mode='EDIT', toggle=False)
anim_path = os.path.expanduser("~/Desktop") + os.sep + "Animation Workspace"
if not os.path.exists(anim_path):
os.mkdir(anim_path)
serialized_rig = Rig().create(armature.edit_bones[:])
all_data = io.BytesIO()
_s4animtools.serialization.recursive_write([*serialized_rig.serialize()], all_data)
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
with open(os.path.join(anim_path, self.get_filename(context)), "wb") as file:
file.write(all_data.getvalue())
def get_filename(self, context):
return "8EAF13DE!00000000!{}.{}.Rig.binary".format(get_64bithash(context.object.name), context.object.name)
def invoke(self, context, event):
# this is important, it will run the modal function
# without this the operator would just execute and end
self.execute(context)
return {'FINISHED'}
class SyncRigToMesh(bpy.types.Operator):
bl_idname = "s4animtools.sync_rig_to_mesh"
bl_label = "Sync Rig To Mesh"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
return {"FINISHED"}
rcol = RCOL()
obj = bpy.context.object
ready_vertex_groups = []
for group in bpy.data.objects["Cube"].vertex_groups:
print(group)
group_name = group.name
armature = obj.data
bones = armature.edit_bones[:]
hashed_name = get_32bit_hash(group_name)
for bone in bones:
if bone.name == group_name:
# print("{},{},{}".format(group_name, bone.name, hashed_name))
mat_values = []
parent_bone = bone.parent
bp1 = bone.matrix
#bp2 = parent_bone.matrix
bp3 = armature.edit_bones["b__ROOT__"].matrix
world_matrix = bpy.context.object.matrix_world
matrix_data = (bp3.inverted() @ bp1).inverted()
current_col = 0
current_idx = 0
for col in matrix_data:
if current_col >= 3:
break
for val in col:
mat_values.append(round(val, 4))
current_idx += 1
current_col += 1
ready_vertex_groups.append(VertexGroup(hashed_name, mat_values))
rcol.sync_rig_to_mesh(ready_vertex_groups)
all_data = io.BytesIO()
_s4animtools.serialization.recursive_write([*rcol.serialize()], all_data)
def invoke(self, context, event):
self.execute(context)
return {'FINISHED'}