Blender V5.3
anim_visualization.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "MEM_guardedalloc.h"
12
13#include "DNA_action_types.h"
14#include "DNA_object_types.h"
15
16#include "BLT_translation.hh"
17
19#include "BKE_report.hh"
20
21#include "GPU_batch.hh"
22
23#include "BLO_read_write.hh"
24
25namespace blender::bke {
26
27/* ******************************************************************** */
28/* Animation Visualization */
29namespace animviz {
30
32{
33 if (avs == nullptr) {
34 return;
35 }
36
37 /* Path settings. */
38 avs->path_bc = avs->path_ac = 10;
39
40 avs->path_sf = 1; /* XXX: Take from scene instead? */
41 avs->path_ef = 250; /* XXX: Take from scene instead? */
42
44
45 avs->path_step = 1;
46
48}
49
50} // namespace animviz
51
52/* ******************************************************************** */
53/* Motion Path */
54
55namespace motionpath {
56
57void set_preview_location(bMotionPath &mpath, const float location[3])
58{
59 copy_v3_v3(mpath.runtime->preview_location, location);
60 mpath.runtime->show_preview = true;
63}
64
71
72/* ------------------- */
73
75{
76 if (mpath == nullptr) {
77 return;
78 }
79
80 /* Free the path if necessary. */
81 if (mpath->points) {
82 MEM_delete(mpath->points);
83 }
84
90
91 /* Reset the relevant parameters. */
92 mpath->points = nullptr;
93 mpath->length = 0;
94}
95
96void free(bMotionPath *mpath)
97{
98 if (mpath == nullptr) {
99 return;
100 }
101
102 free_cache(mpath);
103
104 MEM_delete(mpath->runtime);
105 MEM_delete(mpath);
106}
107
108/* ------------------- */
109
110bMotionPath *copy(const bMotionPath *mpath_src)
111{
112 bMotionPath *mpath_dst;
113
114 if (mpath_src == nullptr) {
115 return nullptr;
116 }
117
118 mpath_dst = MEM_dupalloc(mpath_src);
119 mpath_dst->points = MEM_dupalloc(mpath_src->points);
120
121 /* Should get recreated on draw... */
122 mpath_dst->points_vbo = nullptr;
123 mpath_dst->batch_line = nullptr;
124 mpath_dst->batch_points = nullptr;
125
126 /* Runtime is always created. */
127 BLI_assert(mpath_src->runtime != nullptr);
128 mpath_dst->runtime = MEM_new<bke::motionpath::Runtime>(__func__);
129 mpath_dst->runtime->show_preview = mpath_src->runtime->show_preview;
131
132 return mpath_dst;
133}
134
135/* ------------------- */
136
137bMotionPath *ensure(ReportList *reports, Scene *scene, Object *ob, bPoseChannel *pchan)
138{
139 if (ELEM(nullptr, scene, ob)) {
140 return nullptr;
141 }
142
143 bAnimVizSettings *avs;
144 bMotionPath *mpath, **dst;
145 /* Get destination data. */
146 if (pchan) {
147 /* Paths for pose-channel - assume that pose-channel belongs to the object. */
148 avs = &ob->pose->avs;
149 dst = &pchan->mpath;
150 }
151 else {
152 /* Paths for object. */
153 avs = &ob->avs;
154 dst = &ob->mpath;
155 }
156
157 /* Avoid 0 size allocations. */
158 if (avs->path_sf >= avs->path_ef) {
159 BKE_reportf(reports,
160 RPT_ERROR,
161 "Motion path frame extents invalid for %s (%d to %d)%s",
162 (pchan) ? pchan->name : ob->id.name,
163 avs->path_sf,
164 avs->path_ef,
165 (avs->path_sf == avs->path_ef) ? RPT_(", cannot have single-frame paths") : "");
166 return nullptr;
167 }
168
169 /* Adding 1 because the avs range is inclusive on both ends. */
170 const int expected_length = (avs->path_ef - avs->path_sf) + 1;
171 BLI_assert(expected_length > 1); /* Because the `if` above. */
172
173 /* If there is already a motionpath, just return that, provided its settings
174 * are ok (saves extra free+alloc). */
175 if (*dst != nullptr) {
176 mpath = *dst;
177
180 }
181 else {
183 }
184
185 /* Only reuse a path if it was already a valid path, and of the expected length. */
186 if (mpath->start_frame != mpath->end_frame && mpath->length == expected_length) {
187 mpath->start_frame = avs->path_sf;
188 mpath->end_frame = avs->path_ef + 1;
189 return mpath;
190 }
191
192 /* The path exists, but the allocated buffers are the wrong size. Clear the existing cache, to
193 * allocate a new one below. */
194 free_cache(mpath);
195 }
196 else {
197 mpath = MEM_new<bMotionPath>("bMotionPath");
198 mpath->runtime = MEM_new<bke::motionpath::Runtime>(__func__);
199 *dst = mpath;
200 }
201
202 /* Copy mpath settings from the viz settings. */
203 mpath->start_frame = avs->path_sf;
204 mpath->end_frame = avs->path_ef + 1;
205 mpath->length = expected_length;
206
208 mpath->flag |= MOTIONPATH_FLAG_BHEAD;
209 }
210 else {
212 }
213
216 }
217 else {
219 }
220
221 /* Set default custom values (RGB). */
222 mpath->color[0] = 1.0;
223 mpath->color[1] = 0.0;
224 mpath->color[2] = 0.0;
225
226 mpath->color_post[0] = 0.1;
227 mpath->color_post[1] = 1.0;
228 mpath->color_post[2] = 0.1;
229
230 mpath->line_thickness = 2;
231 mpath->flag |= MOTIONPATH_FLAG_LINES;
232
233 /* Allocate a cache. */
234 mpath->points = MEM_new_array<bMotionPathVert>(mpath->length, "bMotionPathVerts");
235
236 /* Tag viz settings as currently having some path(s) which use it. */
238
239 return mpath;
240}
241
243{
244 if (mpath == nullptr) {
245 return;
246 }
247
248 writer->write_struct(mpath, [](BlendStructWriter<bMotionPath> &struct_writer) {
249 struct_writer.shallow_data.runtime = {};
250 });
251 writer->write_struct_array(mpath->length, mpath->points);
252}
253
255{
256 if (mpath == nullptr) {
257 return;
258 }
259
260 /* Relink points cache. */
261 BLO_read_array_and_validate_size(reader, &mpath->points, &mpath->length);
262
263 mpath->points_vbo = nullptr;
264 mpath->batch_line = nullptr;
265 mpath->batch_points = nullptr;
266 mpath->runtime = MEM_new<bke::motionpath::Runtime>(__func__);
267}
268} // namespace motionpath
269
270} // namespace blender::bke
#define RPT_(msgid)
Object is a sort of wrapper for general info.
#define GPU_BATCH_DISCARD_SAFE(batch)
Definition GPU_batch.hh:199
#define GPU_VERTBUF_DISCARD_SAFE(verts)
Read Blender memory allocation and freeing.
#define ELEM(...)
void settings_init(struct bAnimVizSettings *avs)
void blend_read_data(struct BlendDataReader *reader, struct bMotionPath *mpath)
void free_cache(struct bMotionPath *mpath)
struct bMotionPath * copy(const struct bMotionPath *mpath_src)
void clear_preview_location(bMotionPath &mpath)
void set_preview_location(bMotionPath &mpath, const float location[3])
void free(struct bMotionPath *mpath)
void blend_write(struct BlendWriter *writer, struct bMotionPath *mpath)
struct bMotionPath * ensure(struct ReportList *reports, struct Scene *scene, struct Object *ob, struct bPoseChannel *pchan)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE void copy_v3_v3(float r[3], const float a[3])
@ MOTIONPATH_FLAG_BAKE_CAMERA
@ MOTIONPATH_FLAG_BHEAD
@ MOTIONPATH_FLAG_LINES
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
@ MOTIONPATH_BAKE_CAMERA_SPACE
@ MOTIONPATH_BAKE_HAS_PATHS
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct_array(const int64_t array_size, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
char name[258]
Definition DNA_ID.h:433
bAnimVizSettings avs
bMotionPath * mpath
struct bPose * pose
eMotionPath_BakeFlag path_bakeflag
eMotionPaths_ViewFlag path_viewflag
bke::motionpath::Runtime * runtime
eMotionPath_Flag flag
bMotionPathVert * points
gpu::VertBuf * points_vbo
bAnimVizSettings avs