-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
151 lines (108 loc) · 4.04 KB
/
__init__.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
bl_info = {
"name": "AnimJ exporter",
"description": "Writes Animation to AnimJ",
"author": "Rixx",
"version": (0, 1),
"blender": (2, 93, 2),
"location": "File > Export > AnimJ",
"warning": "",
"wiki_url": "",
"tracker_url": "https://github.com/Rixx-vr/animj_exporter/issues",
"support": 'TESTING',
"category": "Import-Export"
}
import bpy
import bmesh
from bpy import context
from collections import Counter
import json
class JsonSerializable:
def toJSON(self):
return json.dumps(self, default=lambda o: o.__dict__)
class Animation(JsonSerializable):
def __init__(self, name, globalDuration=0):
self.name = name
self.globalDuration = globalDuration
self.tracks = list()
def add_track(self, track):
self.tracks.append(track)
class Tack(JsonSerializable):
def __init__(self, trackType="Curve", valueType="float"):
self.trackType = trackType
self.valueType = valueType
self.data = None
class Data(JsonSerializable):
def __init__(self, node, property):
self.node = node
self.property = property
self.keyframes = list()
def add_keyframe(self, keyframe):
self.keyframes.append(keyframe)
class Keyframe(JsonSerializable):
def __init__(self, time, value, interpolation="Linear"):
self.time = time
self.value = value
if interpolation:
self.interpolation = interpolation
def __create_amimation(name, objects):
NAME = ['x', 'y', 'z', 'w']
TIME_INDEX = 0
VALUE_INDEX = 1
animation = Animation(name)
for obj in objects:
curves = obj.animation_data.action.fcurves
property_name = [x.data_path for x in curves]
properties = Counter(property_name)
chanel = {}
i = 0
for property, dimention in properties.items():
track = Tack(valueType='float{}'.format(dimention))
data = Data(obj.name_full, property)
track.data = data
for key_index in range(len(curves[i].keyframe_points)):
keyframe = {}
time = curves[i].keyframe_points[key_index].co[TIME_INDEX]
for dim_index in range(dimention):
keyframe[NAME[dim_index]] = curves[i + dim_index].keyframe_points[key_index].co[VALUE_INDEX]
print(keyframe[NAME[dim_index]])
data.add_keyframe(Keyframe(time, keyframe))
animation.add_track(track)
i += dimention
return animation
def writeAnimJ(self, pathani):
file_path = self.filepath
if self.selected:
objects = bpy.context.selected_objects
else:
objects = bpy.data.objects
animation = __create_amimation(file_path, objects)
with open(file_path, 'w') as export_file:
export_file.write(animation.toJSON())
export_file.close()
return {'FINISHED'}
class AnimJExport(bpy.types.Operator):
bl_idname = "object.export_animj"
bl_label = "AnimJ Export"
bl_options = {'REGISTER', 'UNDO'}
filename_ext = ".animj"
filter_glob = bpy.props.StringProperty(default="*.animj", options={'HIDDEN'}, maxlen=255)
selected: bpy.props.BoolProperty(name="Selected only", description="Export selected mesh items only", default=True)
filepath: bpy.props.StringProperty(subtype="FILE_PATH")
def execute(self, context):
if(context.active_object.mode == 'EDIT'):
bpy.ops.object.mode_set(mode='OBJECT')
writeAnimJ(self, context);
return {'FINISHED'}
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def menu_func_export(self, context):
self.layout.operator(AnimJExport.bl_idname, text="Animation JSON (.AnimJ)")
def register():
bpy.utils.register_class(AnimJExport)
bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
def unregister():
bpy.utils.unregister_class(AnimJExport)
bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
if __name__ == "__main__":
register()