-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutofit_Framerange.py
280 lines (158 loc) · 7.61 KB
/
Autofit_Framerange.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import bpy
from bpy.app.handlers import persistent
from . import Utility_Function
@ persistent
def load_setting(scene):
if bpy.context.scene.FR_TU_Autofit_Keyframe:
bpy.context.scene.FR_TU_Autofit_Keyframe = True
@persistent
def handler_fit_keyframe(scene):
scn = bpy.context.scene
if scn.FR_TU_Auto_Frame_Range_Settings.Mode == "ACTION":
if scn.FR_TU_Auto_Frame_Range_Settings.Selected:
objects = bpy.context.selected_objects
else:
objects = scn.objects
ranges_start = []
ranges_end = []
for obj in objects:
if obj.animation_data:
action = obj.animation_data.action
if action:
start = None
end = None
if scn.FR_TU_Auto_Frame_Range_Settings.Action_Mode == "ACTION":
if action.use_frame_range:
start = int(action.frame_start)
end = int(action.frame_end)
else:
start = int(action.curve_frame_range[0])
end = int(action.curve_frame_range[1])
if scn.FR_TU_Auto_Frame_Range_Settings.Action_Mode == "KEYFRAME":
start = int(action.curve_frame_range[0])
end = int(action.curve_frame_range[1])
if start is not None and end is not None:
ranges_start.append(start)
ranges_end.append(end)
if len(ranges_start) > 0:
scn.frame_start = int(min(ranges_start))
if len(ranges_end) > 0:
scn.frame_end = int(max(ranges_end))
if scn.FR_TU_Auto_Frame_Range_Settings.Mode == "NLA":
ranges_start = []
ranges_end = []
objects = scn.objects
for object in objects:
if object.animation_data:
if object.animation_data.nla_tracks:
for track in object.animation_data.nla_tracks:
if track.strips:
for strip in track.strips:
if scn.FR_TU_Auto_Frame_Range_Settings.Selected:
if strip.select:
ranges_start.append(strip.frame_start)
ranges_end.append(strip.frame_end+1)
else:
ranges_start.append(strip.frame_start)
ranges_end.append(strip.frame_end+1)
if len(ranges_start) > 0:
scn.frame_start = int(min(ranges_start))
if len(ranges_end) > 0:
scn.frame_end = int(max(ranges_end) -1)
if scn.FR_TU_Auto_Frame_Range_Settings.Mode == "SEQUENCE":
list_end = []
list_start = []
for sequence in bpy.context.sequences:
if scn.FR_TU_Auto_Frame_Range_Settings.Selected:
if sequence.select:
list_start.append(sequence.frame_final_start)
list_end.append(sequence.frame_final_end)
else:
list_start.append(sequence.frame_final_start)
list_end.append(sequence.frame_final_end)
if len(list_start) > 0:
scn.frame_start = int(min(list_start))
if len(list_end) > 0:
scn.frame_end = int(max(list_end) -1)
def autofit_keyframe(self, context):
scn = context.scene
if context.area:
if context.area.ui_type == "SEQUENCE_EDITOR":
scn.FR_TU_Auto_Frame_Range_Settings.Mode = "SEQUENCE"
if context.area.ui_type == "NLA_EDITOR":
scn.FR_TU_Auto_Frame_Range_Settings.Mode = "NLA"
if context.scene.FR_TU_Autofit_Keyframe:
bpy.app.handlers.depsgraph_update_post.append(handler_fit_keyframe)
# context.scene.Autofit_Sequence = False
if context.scene.FR_TU_Autofit_Keyframe == False:
if handler_fit_keyframe in bpy.app.handlers.depsgraph_update_post:
bpy.app.handlers.depsgraph_update_post.remove(handler_fit_keyframe)
# try:
# if context.scene.FR_TU_Autofit_Keyframe == False:
#
# bpy.app.handlers.depsgraph_update_post.remove(handler_fit_keyframe)
# except:
# print("Fail to Remove Handlers")
if scn.FR_TU_Autofit_Keyframe:
handler_fit_keyframe(context)
def draw_item_keyframe(self, context):
preferences = Utility_Function.get_addon_preferences()
if preferences.TU_Auto_Frame_Range:
layout = self.layout
layout.separator()
layout.prop(context.scene, "FR_TU_Autofit_Keyframe", text="", icon="TIME")
# layout.menu("FR_TU_auto_frame_range", text="", icon="TRIA_DOWN")
layout.popover(
panel="FR_PT_TU_auto_frame_range",
text="",
)
class FR_PT_TU_Auto_Frame_Range(bpy.types.Panel):
bl_label = "Auto Frame Range"
bl_idname = "FR_PT_TU_auto_frame_range"
bl_options = {'HIDE_HEADER'}
bl_region_type = 'HEADER'
bl_space_type = 'DOPESHEET_EDITOR'
def draw(self, context):
layout = self.layout
scn = context.scene
layout.prop(scn.FR_TU_Auto_Frame_Range_Settings, "Mode", text="Mode", expand=True)
if scn.FR_TU_Auto_Frame_Range_Settings.Mode == "ACTION":
layout.prop(scn.FR_TU_Auto_Frame_Range_Settings, "Action_Mode", text="Only Selected", expand=True)
layout.prop(scn.FR_TU_Auto_Frame_Range_Settings, "Selected", text="Only Selected")
Mode = [("ACTION","Action","Action"),("NLA","NLA","NLA Strips"),("SEQUENCE","Sequence","Sequencer Strips")]
# Mode = [("ACTION","Action","Action"),("NLA","NLA","NLA Strips"),("SEQUENCE","Sequence","Sequencer Strips")]
Action_Mode = [("ACTION","Settings","Settings"), ("KEYFRAME","Keyframe","Keyframe")]
class FR_TU_Auto_Frame_Range_Settings(bpy.types.PropertyGroup):
Mode : bpy.props.EnumProperty(items=Mode, default="ACTION")
Action_Mode : bpy.props.EnumProperty(items=Action_Mode, default="ACTION")
Selected: bpy.props.BoolProperty(default=False)
classes = [
FR_TU_Auto_Frame_Range_Settings,
FR_PT_TU_Auto_Frame_Range
]
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.FR_TU_Autofit_Keyframe = bpy.props.BoolProperty(default=False, update=autofit_keyframe)
bpy.types.Scene.FR_TU_Auto_Frame_Range_Settings = bpy.props.PointerProperty(type=FR_TU_Auto_Frame_Range_Settings)
bpy.types.TIME_MT_editor_menus.append(draw_item_keyframe)
bpy.types.DOPESHEET_MT_editor_menus.append(draw_item_keyframe)
bpy.types.GRAPH_MT_editor_menus.append(draw_item_keyframe)
bpy.types.NLA_MT_editor_menus.append(draw_item_keyframe)
bpy.types.SEQUENCER_MT_editor_menus.append(draw_item_keyframe)
bpy.app.handlers.load_post.append(load_setting)
def unregister():
if handler_fit_keyframe in bpy.app.handlers.depsgraph_update_post:
bpy.app.handlers.depsgraph_update_post.remove(handler_fit_keyframe)
for cls in classes:
bpy.utils.unregister_class(cls)
bpy.app.handlers.load_post.remove(load_setting)
bpy.types.TIME_MT_editor_menus.remove(draw_item_keyframe)
bpy.types.DOPESHEET_MT_editor_menus.remove(draw_item_keyframe)
bpy.types.GRAPH_MT_editor_menus.remove(draw_item_keyframe)
bpy.types.NLA_MT_editor_menus.remove(draw_item_keyframe)
bpy.types.SEQUENCER_MT_editor_menus.remove(draw_item_keyframe)
del bpy.types.Scene.FR_TU_Autofit_Keyframe
del bpy.types.Scene.FR_TU_Auto_Frame_Range_Settings
if __name__ == "__main__":
register()