-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_deshaker_damper.py
141 lines (123 loc) · 4.21 KB
/
import_deshaker_damper.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
import bpy
#from bpy.props import *
import math
from bpy_extras.io_utils import ImportHelper
from bpy.props import StringProperty
from bpy.types import Operator
#parameters
# damping factor func - 0.95 to 0.35 depending on black bars size
def damping_function(xv):
v = abs(xv)
if (v>400): return 0.35
return 0.95-(v/400)*0.6
# for rotation
def damping_function_r(xv):
v = abs(xv)
if (v>15): return 0.35
return 0.95-(v/15)*0.6
reset_to_zero_on_new_scenes = False # ignore new_scene flag in log
bl_info = {
"name": "Import Deshaker log",
"author": "Sergey menshikov",
"version": (1, 1),
"blender": (2, 65, 0),
"location": "Sequencer -> Track View Properties",
"description": "Load a deshaker formatted transform data into sequencer as a transform strip",
"warning": "",
"wiki_url": "",
"category": "Import-Export",
}
# get values from file, translate to x y in source pixel scale and r angle
def value_generator(filepath):
x=0.0
y=0.0
r=0.0
with open(filepath) as f:
for line in f:
a = line.split()
new_scene = (len(a)>5 and a[5]=='new_scene')
if(a[1]=='skipped'): continue
if(reset_to_zero_on_new_scenes and new_scene): # ignore new scenes (option)
x=0.0
y=0.0
r=0.0
else:
# 2D translation
a_rad = 0 if r==0 else (r*math.pi/180)
xi = float(a[1])
yi = float(a[2]) * -1 # negative vertical axis
dx = xi * math.cos(a_rad) - yi * math.sin(a_rad)
dy = xi * math.sin(a_rad) + yi * math.cos(a_rad)
x+=dx
y+=dy
r+=(-1*float(a[3]))
x*=damping_function(x)
y*=damping_function(y)
r*=damping_function_r(r)
kf = int(a[0])
yield (kf+1,x,y,r,new_scene)
class ImportDeshaker_Class(Operator, ImportHelper):
"""Deshaker log format importer"""
bl_idname = "deshaker1.log"
bl_label = "Import Deshaker Format"
filename_ext = ".log"
filter_glob = StringProperty(default="*.log", options={"HIDDEN"})
def execute(self, context):
self.import_deshaker_file(context, self.filepath)
#print(self.filepath)
return {"FINISHED"}
def import_deshaker_file(self, context, filepath):
# check if a scene is selected
if(not bpy.context.screen.scene):
self.report({'ERROR'}, "Please select an active scene with a strip in Video Sequence Editor.")
return
if(not bpy.context.scene.sequence_editor.active_strip):
self.report({'ERROR'}, "Please select the strip to apply Deshaker log to.")
return
x_percent = 100.0 / (bpy.context.screen.scene.render.resolution_x * bpy.context.screen.scene.render.resolution_percentage / 100.0 )
y_percent = 100.0 / (bpy.context.screen.scene.render.resolution_y * bpy.context.screen.scene.render.resolution_percentage / 100.0 )
# detect context
screen = bpy.context.window.screen
for area in screen.areas:
if area.type == 'SEQUENCE_EDITOR':
break
context = {
'window': bpy.context.window,
'scene': bpy.context.scene,
'screen': screen,
'area': area,
}
# create transform strip
bpy.ops.sequencer.effect_strip_add(context,type='TRANSFORM')
strip = bpy.context.scene.sequence_editor.active_strip
strip.translation_unit = 'PERCENT'
# set zero transform for frame #0
strip.translate_start_x = 0
strip.translate_start_y = 0
strip.rotation_start = 0
strip.keyframe_insert(data_path="translate_start_x", frame=1)
strip.keyframe_insert(data_path="translate_start_y", frame=1)
strip.keyframe_insert(data_path="rotation_start", frame=1)
# start import
kf = 0
x=0.0
y=0.0
r=0.0
new_scene = False
for (kf,x,y,r,new_scene) in value_generator(filepath):
strip.translate_start_x = x * x_percent
strip.translate_start_y = y * y_percent
strip.rotation_start = r
strip.keyframe_insert(data_path="translate_start_x", frame=kf)
strip.keyframe_insert(data_path="translate_start_y", frame=kf)
strip.keyframe_insert(data_path="rotation_start", frame=kf)
def menu_func_import(self, context):
self.layout.operator(ImportDeshaker_Class.bl_idname, text="Deshaker Log (.log)")
def register():
bpy.utils.register_class(ImportDeshaker_Class)
bpy.types.INFO_MT_file_import.append(menu_func_import)
def unregister():
bpy.utils.unregister_class(ImportDeshaker_Class)
bpy.types.INFO_MT_file_import.remove(menu_func_import)
if __name__ == "__main__":
register()