Blender V5.3
node_composite_track_position.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_index_range.hh"
7#include "BLI_string_utf8.hh"
8
10#include "DNA_tracking_types.h"
11
12#include "BKE_context.hh"
13#include "BKE_lib_id.hh"
14#include "BKE_movieclip.hh"
15#include "BKE_tracking.hh"
16
17#include "RNA_access.hh"
18#include "RNA_prototypes.hh"
19
20#include "UI_interface.hh"
22#include "UI_resources.hh"
23
24#include "COM_node_operation.hh"
25
27
29
31
32static const EnumPropertyItem mode_items[] = {
34 "ABSOLUTE",
35 0,
36 N_("Absolute"),
37 N_("Returns the position and speed of the marker at the current scene frame relative to the "
38 "zero origin of the tracking space")},
40 "RELATIVE_START",
41 0,
42 N_("Relative Start"),
43 N_("Returns the position and speed of the marker at the current scene frame relative to the "
44 "position of the first non-disabled marker in the track")},
46 "RELATIVE_FRAME",
47 0,
48 N_("Relative Frame"),
49 N_("Returns the position and speed of the marker at the current scene frame relative to the "
50 "position of the marker at the current scene frame plus the user given relative frame")},
52 "ABSOLUTE_FRAME",
53 0,
54 N_("Absolute Frame"),
55 N_("Returns the position and speed of the marker at the given absolute frame")},
56 {0, nullptr, 0, nullptr, nullptr},
57};
58
60{
61 b.add_input<decl::Menu>("Mode"_ustr)
63 .static_items(mode_items)
65 b.add_input<decl::Int>("Frame"_ustr)
66 .usage_by_menu(
67 "Mode"_ustr,
69
70 b.add_output<decl::Float>("X"_ustr);
71 b.add_output<decl::Float>("Y"_ustr);
72 b.add_output<decl::Vector>("Speed"_ustr).subtype(PROP_VELOCITY).dimensions(4);
73}
74
75static void node_init(bNodeTree * /*node_tree*/, bNode *node)
76{
77 NodeTrackPosData *data = MEM_new<NodeTrackPosData>(__func__);
78 node->storage = data;
79}
80
81static void node_init_api(const bContext *C, PointerRNA *node_ptr)
82{
83 const Scene *scene = CTX_data_scene(C);
84 MovieClip *movie_clip = scene->clip;
85 if (!movie_clip) {
86 return;
87 }
88
89 bNode *node = node_ptr->data_as<bNode>();
90 node->id = &movie_clip->id;
91 id_us_plus(&movie_clip->id);
92
93 NodeTrackPosData &data = node_storage(*node);
95 &movie_clip->tracking);
96 STRNCPY_UTF8(data.tracking_object, tracking_object->name);
97
98 if (tracking_object->active_track) {
99 STRNCPY_UTF8(data.track_name, tracking_object->active_track->name);
100 }
101}
102
104{
105 bNode *node = static_cast<bNode *>(ptr->data);
106
107 template_id(&layout, C, ptr, "clip", nullptr, "CLIP_OT_open", nullptr);
108
109 if (node->id) {
110 MovieClip *clip = id_cast<MovieClip *>(node->id);
111 MovieTracking *tracking = &clip->tracking;
112 MovieTrackingObject *tracking_object;
114 PointerRNA tracking_ptr = RNA_pointer_create_discrete(&clip->id, RNA_MovieTracking, tracking);
115
116 ui::Layout &col = layout.column(false);
117 col.prop_search(ptr, "tracking_object", &tracking_ptr, "objects", "", ICON_OBJECT_DATA);
118
119 tracking_object = BKE_tracking_object_get_named(tracking, data->tracking_object);
120 if (tracking_object) {
122 &clip->id, RNA_MovieTrackingObject, tracking_object);
123
124 col.prop_search(ptr, "track_name", &object_ptr, "tracks", "", ICON_ANIM_DATA);
125 }
126 else {
127 layout.prop(ptr, "track_name", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_ANIM_DATA);
128 }
129 }
130}
131
132using namespace blender::compositor;
133
135 public:
137
138 void execute() override
139 {
141
142 if (!track) {
144 return;
145 }
146
147 const float2 current_marker_position = compute_marker_position_at_frame(track, get_frame());
148 const int2 size = get_size();
149
150 execute_position(track, current_marker_position, size);
151 execute_speed(track, current_marker_position, size);
152 }
153
154 void execute_position(MovieTrackingTrack *track, float2 current_marker_position, int2 size)
155 {
156 Result &x_result = this->get_result("X");
157 Result &y_result = this->get_result("Y");
158 if (!x_result.should_compute() && !y_result.should_compute()) {
159 return;
160 }
161
162 /* Compute the position relative to the reference marker position. Multiply by the size to get
163 * the position in pixel space. */
164 const float2 reference_marker_position = compute_reference_marker_position(track);
165 const float2 position = (current_marker_position - reference_marker_position) * float2(size);
166
167 if (x_result.should_compute()) {
168 x_result.allocate_single_value();
169 x_result.set_single_value(position.x);
170 }
171
172 if (y_result.should_compute()) {
173 y_result.allocate_single_value();
174 y_result.set_single_value(position.y);
175 }
176 }
177
178 void execute_speed(MovieTrackingTrack *track, float2 current_marker_position, int2 size)
179 {
180 Result &result = this->get_result("Speed");
181 if (!result.should_compute()) {
182 return;
183 }
184
185 /* Compute the speed as the difference between the previous marker position and the current
186 * marker position. Notice that we compute the speed from the current to the previous position,
187 * not the other way around. */
188 const float2 previous_marker_position = compute_temporally_neighboring_marker_position(
189 track, current_marker_position, -1);
190 const float2 speed_toward_previous = previous_marker_position - current_marker_position;
191
192 /* Compute the speed as the difference between the current marker position and the next marker
193 * position. */
194 const float2 next_marker_position = compute_temporally_neighboring_marker_position(
195 track, current_marker_position, 1);
196 const float2 speed_toward_next = current_marker_position - next_marker_position;
197
198 /* Encode both speeds in a 4D vector. Multiply by the size to get the speed in pixel space. */
199 const float4 speed = float4(speed_toward_previous * float2(size),
200 speed_toward_next * float2(size));
201
202 result.allocate_single_value();
203 result.set_single_value(speed);
204 }
205
207 {
208 Result &x_result = this->get_result("X");
209 if (x_result.should_compute()) {
210 x_result.allocate_single_value();
211 x_result.set_single_value(0.0f);
212 }
213 Result &y_result = this->get_result("Y");
214 if (y_result.should_compute()) {
215 y_result.allocate_single_value();
216 y_result.set_single_value(0.0f);
217 }
218 Result &speed_result = this->get_result("Speed");
219 if (speed_result.should_compute()) {
220 speed_result.allocate_single_value();
221 speed_result.set_single_value(float4(0.0f));
222 }
223 }
224
225 /* Compute the position of the marker that is delta time away from the evaluation frame. If no
226 * marker exist for that particular frame or is disabled, the current marker position is
227 * returned. This is useful for computing the speed by providing small negative and positive
228 * delta times. */
230 float2 current_marker_position,
231 int time_delta)
232 {
233 const int local_frame_number = BKE_movieclip_remap_scene_to_clip_frame(
234 get_movie_clip(), get_frame() + time_delta);
235 MovieTrackingMarker *marker = BKE_tracking_marker_get_exact(track, local_frame_number);
236
237 if (marker == nullptr || marker->flag & MARKER_DISABLED) {
238 return current_marker_position;
239 }
240
241 return float2(marker->pos);
242 }
243
244 /* Compute the position of the reference marker which the output position will be computed
245 * relative to. For non-relative modes, this is just the zero origin or the tracking space. See
246 * the get_mode() method for more information. */
261
262 /* Compute the position of the first non-disabled marker in the track. */
264 {
265 for (const int i : IndexRange(track->markersnr)) {
266 MovieTrackingMarker &marker = track->markers[i];
267
268 if (marker.flag & MARKER_DISABLED) {
269 continue;
270 }
271
272 return float2(marker.pos);
273 }
274
275 return float2(0.0f);
276 }
277
278 /* Compute the marker position at the given frame, if no such marker exist, return the position
279 * of the temporally nearest marker before it, if no such marker exist, return the position of
280 * the temporally nearest marker after it. */
282 {
283 const int local_frame_number = BKE_movieclip_remap_scene_to_clip_frame(get_movie_clip(),
284 frame);
285 MovieTrackingMarker *marker = BKE_tracking_marker_get(track, local_frame_number);
286 return float2(marker->pos);
287 }
288
289 /* Get the movie tracking track corresponding to the given object and track names. If no such
290 * track exist, return nullptr. */
292 {
293 MovieClip *movie_clip = get_movie_clip();
294 if (!movie_clip) {
295 return nullptr;
296 }
297
298 MovieTracking *movie_tracking = &movie_clip->tracking;
299
300 MovieTrackingObject *movie_tracking_object = BKE_tracking_object_get_named(
301 movie_tracking, node_storage(node()).tracking_object);
302 if (!movie_tracking_object) {
303 return nullptr;
304 }
305
306 return BKE_tracking_object_find_track_with_name(movie_tracking_object,
307 node_storage(node()).track_name);
308 }
309
310 /* Get the size of the movie clip at the evaluation frame. This is constant for all frames in
311 * most cases. */
313 {
314 MovieClipUser user = {};
316
317 int2 size;
318 BKE_movieclip_get_size(get_movie_clip(), &user, &size.x, &size.y);
319
320 return size;
321 }
322
323 /* In the CMP_NODE_TRACK_POSITION_RELATIVE_FRAME mode, this represents the offset that will be
324 * added to the current scene frame. See the get_mode() method for more information. */
326 {
327 return this->get_input("Frame").get_single_value_default<int>();
328 }
329
330 /* Get the frame where the marker will be retrieved. This is the absolute frame for the absolute
331 * mode and the current scene frame otherwise. */
333 {
335 return get_absolute_frame();
336 }
337
338 return context().get_frame_number();
339 }
340
341 /* In the CMP_NODE_TRACK_POSITION_ABSOLUTE_FRAME mode, this represents the frame where the marker
342 * will be retrieved. See the get_mode() method for more information. */
344 {
345 return this->get_input("Frame").get_single_value_default<int>();
346 }
347
349 {
351 this->get_input("Mode").get_single_value_default<MenuValue>().value);
352 }
353
355 {
356 return id_cast<MovieClip *>(node().id);
357 }
358};
359
361{
362 return new TrackPositionOperation(context, node);
363}
364
365static void node_register()
366{
367 static bke::bNodeType ntype;
368
369 cmp_node_type_base(&ntype, "CompositorNodeTrackPos"_ustr, CMP_NODE_TRACKPOS);
370 ntype.ui_name = "Track Position";
371 ntype.ui_description =
372 "Provide information about motion tracking points, such as x and y values";
373 ntype.enum_name_legacy = "TRACKPOS";
374 ntype.nclass = NODE_CLASS_INPUT;
375 ntype.declare = node_declare;
377 ntype.initfunc = node_init;
380 ntype, "NodeTrackPosData", node_free_standard_storage, node_copy_standard_storage);
382
384}
386
387} // namespace blender::nodes::node_composite_track_position_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define CMP_NODE_TRACKPOS
#define STRNCPY_UTF8(dst, src)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
constexpr IndexRange()=default
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
bool should_compute() const
Definition result.cc:1124
void set_single_value(const T &value)
float2 compute_temporally_neighboring_marker_position(MovieTrackingTrack *track, float2 current_marker_position, int time_delta)
void execute_position(MovieTrackingTrack *track, float2 current_marker_position, int2 size)
void execute_speed(MovieTrackingTrack *track, float2 current_marker_position, int2 size)
#define position
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
static void node_init_api(const bContext *C, PointerRNA *node_ptr)
static void node_draw_buttons(ui::Layout &layout, bContext *C, PointerRNA *ptr)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
MovieTrackingMarker * BKE_tracking_marker_get_exact(MovieTrackingTrack *track, int framenr)
Definition tracking.cc:1396
void BKE_movieclip_get_size(MovieClip *clip, const MovieClipUser *user, int *r_width, int *r_height)
void id_us_plus(ID *id)
Definition lib_id.cc:433
MovieTrackingObject * BKE_tracking_object_get_active(const MovieTracking *tracking)
Definition tracking.cc:1982
BMesh const char void * data
MovieTrackingTrack * BKE_tracking_object_find_track_with_name(MovieTrackingObject *tracking_object, const char *name)
Definition tracking.cc:1998
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
@ CMP_NODE_TRACK_POSITION_RELATIVE_FRAME
@ CMP_NODE_TRACK_POSITION_RELATIVE_START
@ CMP_NODE_TRACK_POSITION_ABSOLUTE
@ CMP_NODE_TRACK_POSITION_ABSOLUTE_FRAME
void BKE_movieclip_user_set_frame(MovieClipUser *user, int framenr)
float BKE_movieclip_remap_scene_to_clip_frame(const MovieClip *clip, float framenr)
MovieTrackingObject * BKE_tracking_object_get_named(MovieTracking *tracking, const char *name)
Definition tracking.cc:1971
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
Scene * CTX_data_scene(const bContext *C)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
@ PROP_VELOCITY
Definition RNA_types.hh:320
MovieTrackingMarker * BKE_tracking_marker_get(MovieTrackingTrack *track, int framenr)
Definition tracking.cc:1363
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
struct MovieTracking tracking
MovieTrackingTrack * active_track
MovieTrackingMarker * markers
T * data_as() const
Definition RNA_types.hh:139
struct MovieClip * clip
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
void(*) initfunc_api(const bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:333
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
void prop_search(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop, PropertyRNA *item_searchpropname, std::optional< StringRefNull > name, int icon, bool results_are_suggestions)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & column(bool align)
#define N_(msgid)