mirrored from https://projects.blender.org/blender/blender-addons.git
-
Notifications
You must be signed in to change notification settings - Fork 189
/
space_clip_editor_refine_solution.py
211 lines (175 loc) · 7.05 KB
/
space_clip_editor_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# SPDX-FileCopyrightText: 2017-2022 Blender Foundation
#
# SPDX-License-Identifier: GPL-2.0-or-later
bl_info = {
"name": "Refine tracking solution",
"author": "Stephen Leger",
"license": "GPL",
"version": (1, 1, 5),
"blender": (2, 80, 0),
"location": "Clip Editor > Tools > Solve > Refine Solution",
"description": "Refine motion solution by setting track weight according"
" to reprojection error",
"warning": "",
"doc_url": "{BLENDER_MANUAL_URL}/addons/video_tools/refine_tracking.html",
"category": "Video Tools",
}
import bpy
from bpy.types import (
Operator,
Panel,
)
from bpy.props import FloatProperty
from mathutils import Vector
class TRACKING_OP_refine_solution(Operator):
bl_idname = "tracking.refine_solution"
bl_label = "Refine"
bl_description = "Set track weight by error and solve camera motion"
bl_options = {"UNDO"}
@classmethod
def poll(cls, context):
return (context.area and context.area.spaces and
hasattr(context.area.spaces.active, 'clip') and
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 {'CANCELLED'}
marker_position = Vector()
for frame in range(start, end):
camera = tracking.reconstruction.cameras.find_frame(frame=frame)
if camera is not None:
camera_invert = camera.matrix.inverted()
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 = camera_invert @ track.bundle
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_point_pixels[0] + reprojected_position[0],
tracking.camera.principal_point_pixels[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 TRACKING_OP_reset_solution(Operator):
bl_idname = "tracking.reset_solution"
bl_label = "Reset"
bl_description = "Reset track weight and solve camera motion"
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 {'CANCELLED'}
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=frame)
if camera is None:
continue
for track in tracking.tracks:
marker = track.markers.find_frame(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 TRACKING_PT_RefineMotionTracking(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
col = layout.column(align=True)
col.prop(context.window_manager, "TrackingTargetError", text="Target error")
col.prop(context.window_manager, "TrackingSmooth", text="Smooth transition")
sub_box = col.box()
sub_box.scale_y = 0.25
row = col.row(align=True)
row.operator("tracking.refine_solution")
row.operator("tracking.reset_solution")
classes =(
TRACKING_OP_refine_solution,
TRACKING_OP_reset_solution,
TRACKING_PT_RefineMotionTracking
)
def register():
bpy.types.WindowManager.TrackingTargetError = FloatProperty(
name="Target Error",
description="Refine motion track target error",
default=0.3,
min=0.01
)
bpy.types.WindowManager.TrackingSmooth = FloatProperty(
name="Smooth Transition",
description="Smooth weight transition on start and end of incomplete tracks",
default=25,
min=1
)
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
del bpy.types.WindowManager.TrackingTargetError
del bpy.types.WindowManager.TrackingSmooth
if __name__ == "__main__":
register()