-
Notifications
You must be signed in to change notification settings - Fork 3
/
space_clipEditor_refine_solution.py
197 lines (173 loc) · 7.41 KB
/
space_clipEditor_refine_solution.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
# -*- coding:utf-8 -*-
# ***** GPL LICENSE BLOCK *****
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# All rights reserved.
# ***** GPL LICENSE BLOCK *****
bl_info = {
"name": "Refine tracking solution",
"author": "Stephen Leger",
"license": "GPL",
"version": (1, 1, 2),
"blender": (2, 7, 8),
"location": "Clip Editor > Tools > Solve > Refine Solution",
"description": "Refine motion solution by setting track weight according reprojection error",
"warning": "",
"wiki_url": "https://github.com/s-leger/blenderTracking/wiki",
"tracker_url": "https://github.com/s-leger/blenderTracking/issues",
"support": "COMMUNITY",
"category": "Tools",
}
import bpy
import math
from mathutils import Vector
class OP_Tracking_refine_solution(bpy.types.Operator):
"""Set track weight by error and solve camera motion"""
bl_idname = "tracking.refine_solution"
bl_label = "Refine"
bl_options = {"UNDO"}
@classmethod
def poll(cls, context):
return (context.area.spaces.active.clip is not None)
def execute(self, context):
error = context.window_manager.TrackingTargetError
smooth = context.window_manager.TrackingSmooth
clip = context.area.spaces.active.clip
try:
tracking = clip.tracking
tracks = tracking.tracks
winx = float(clip.size[0])
winy = float(clip.size[1])
aspy = 1.0 / tracking.camera.pixel_aspect
start = tracking.reconstruction.cameras[0].frame
end = tracking.reconstruction.cameras[-1].frame
except:
return {'CANCELED'}
marker_position = Vector()
for frame in range(start, end):
camera = tracking.reconstruction.cameras.find_frame(frame)
if camera is not None:
imat = camera.matrix.inverted()
projection_matrix = imat.transposed()
else:
continue
for track in tracking.tracks:
marker = track.markers.find_frame(frame)
if marker is None:
continue
# weight incomplete tracks on start and end
if frame > start + smooth and frame < end - smooth:
for m in track.markers:
if not m.mute:
tstart = m
break
for m in reversed(track.markers):
if not m.mute:
tend = m
break
dt = min(0.5 * (tend.frame - tstart.frame), smooth)
if dt > 0:
t0 = min(1.0, (frame - tstart.frame) / dt)
t1 = min(1.0, (tend.frame - frame) / dt)
tw = min(t0, t1)
else:
tw = 0.0
else:
tw = 1.0
reprojected_position = track.bundle * projection_matrix
if reprojected_position.z == 0:
track.weight = 0
track.keyframe_insert("weight", frame=frame)
continue
reprojected_position = reprojected_position / -reprojected_position.z * tracking.camera.focal_length_pixels
reprojected_position = Vector((tracking.camera.principal[0] + reprojected_position[0],tracking.camera.principal[1] * aspy + reprojected_position[1], 0))
marker_position[0] = (marker.co[0] + track.offset[0]) * winx
marker_position[1] = (marker.co[1] + track.offset[1]) * winy * aspy
dp = marker_position - reprojected_position
if dp.length == 0:
track.weight = 1.0
else:
track.weight = min(1.0, tw * error / dp.length)
track.keyframe_insert("weight", frame=frame)
bpy.ops.clip.solve_camera('INVOKE_DEFAULT')
return{'FINISHED'}
class OP_Tracking_reset_solution(bpy.types.Operator):
"""Reset track weight and solve camera motion"""
bl_idname = "tracking.reset_solution"
bl_label = "Reset"
bl_options = {"UNDO"}
@classmethod
def poll(cls, context):
return (context.area.spaces.active.clip is not None)
def execute(self, context):
clip = context.area.spaces.active.clip
try:
tracking = clip.tracking
tracks = tracking.tracks
start = tracking.reconstruction.cameras[0].frame
end = tracking.reconstruction.cameras[-1].frame
except:
return {'CANCELED'}
start = tracking.reconstruction.cameras[0].frame
end = tracking.reconstruction.cameras[-1].frame
for frame in range(start, end):
camera = tracking.reconstruction.cameras.find_frame(frame)
if camera is None:
continue
for track in tracking.tracks:
marker = track.markers.find_frame(frame)
if marker is None:
continue
track.weight = 1.0
track.keyframe_insert("weight", frame=frame)
bpy.ops.clip.solve_camera('INVOKE_DEFAULT')
return{'FINISHED'}
class RefineMotionTrackingPanel(bpy.types.Panel):
bl_label = "Refine solution"
bl_space_type = "CLIP_EDITOR"
bl_region_type = "TOOLS"
bl_category = "Solve"
@classmethod
def poll(cls, context):
return (context.area.spaces.active.clip is not None)
def draw(self, context):
layout = self.layout
box = layout.box()
row = box.row(align=True)
row.label("Refine")
row = box.row(align=True)
row.prop(context.window_manager, "TrackingTargetError", text="Target error")
row = box.row(align=True)
row.prop(context.window_manager, "TrackingSmooth", text="Smooth transition")
row = box.row(align=True)
row.operator("tracking.refine_solution")
row.operator("tracking.reset_solution")
def register():
bpy.types.WindowManager.TrackingTargetError = bpy.props.FloatProperty(
name="error",
description="Refine motion track target error",
default=0.3,
min=0.01)
bpy.types.WindowManager.TrackingSmooth = bpy.props.FloatProperty(
name="Smooth transition",
description="Smooth weight transition on start and end of incomplete tracks",
default=25,
min=1)
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_module(__name__)
del bpy.types.WindowManager.TrackingTargetError
del bpy.types.WindowManager.TrackingSmooth
if __name__ == "__main__":
register()