Blender V4.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#include "MEM_guardedalloc.h"
9
10#include "DNA_action_types.h"
11#include "DNA_object_types.h"
12
13#include "BLT_translation.hh"
14
16#include "BKE_report.hh"
17
18#include "GPU_batch.hh"
19
20#include "BLO_read_write.hh"
21
22/* ******************************************************************** */
23/* Animation Visualization */
24
26{
27 /* sanity check */
28 if (avs == nullptr) {
29 return;
30 }
31
32 /* path settings */
33 avs->path_bc = avs->path_ac = 10;
34
35 avs->path_sf = 1; /* XXX: Take from scene instead? */
36 avs->path_ef = 250; /* XXX: Take from scene instead? */
37
39
40 avs->path_step = 1;
41
43}
44
45/* ------------------- */
46
48{
49 /* sanity check */
50 if (mpath == nullptr) {
51 return;
52 }
53
54 /* free the path if necessary */
55 if (mpath->points) {
56 MEM_freeN(mpath->points);
57 }
58
62
63 /* reset the relevant parameters */
64 mpath->points = nullptr;
65 mpath->length = 0;
66}
67
69{
70 /* sanity check */
71 if (mpath == nullptr) {
72 return;
73 }
74
75 /* free the cache first */
77
78 /* now the instance itself */
79 MEM_freeN(mpath);
80}
81
82/* ------------------- */
83
85{
86 bMotionPath *mpath_dst;
87
88 if (mpath_src == nullptr) {
89 return nullptr;
90 }
91
92 mpath_dst = static_cast<bMotionPath *>(MEM_dupallocN(mpath_src));
93 mpath_dst->points = static_cast<bMotionPathVert *>(MEM_dupallocN(mpath_src->points));
94
95 /* should get recreated on draw... */
96 mpath_dst->points_vbo = nullptr;
97 mpath_dst->batch_line = nullptr;
98 mpath_dst->batch_points = nullptr;
99
100 return mpath_dst;
101}
102
103/* ------------------- */
104
106 Scene *scene,
107 Object *ob,
108 bPoseChannel *pchan)
109{
110 bAnimVizSettings *avs;
111 bMotionPath *mpath, **dst;
112
113 /* sanity checks */
114 if (ELEM(nullptr, scene, ob)) {
115 return nullptr;
116 }
117
118 /* get destination data */
119 if (pchan) {
120 /* paths for posechannel - assume that posechannel belongs to the object */
121 avs = &ob->pose->avs;
122 dst = &pchan->mpath;
123 }
124 else {
125 /* paths for object */
126 avs = &ob->avs;
127 dst = &ob->mpath;
128 }
129
130 /* Avoid 0 size allocations. */
131 if (avs->path_sf >= avs->path_ef) {
132 BKE_reportf(reports,
133 RPT_ERROR,
134 "Motion path frame extents invalid for %s (%d to %d)%s",
135 (pchan) ? pchan->name : ob->id.name,
136 avs->path_sf,
137 avs->path_ef,
138 (avs->path_sf == avs->path_ef) ? RPT_(", cannot have single-frame paths") : "");
139 return nullptr;
140 }
141
142 /* Adding 1 because the avs range is inclusive on both ends. */
143 const int expected_length = (avs->path_ef - avs->path_sf) + 1;
144 BLI_assert(expected_length > 1); /* Because the `if` above. */
145
146 /* If there is already a motionpath, just return that, provided its settings
147 * are ok (saves extra free+alloc). */
148 if (*dst != nullptr) {
149 mpath = *dst;
150
153 }
154 else {
155 mpath->flag &= ~MOTIONPATH_FLAG_BAKE_CAMERA;
156 }
157
158 /* Only reuse a path if it was already a valid path, and of the expected length. */
159 if (mpath->start_frame != mpath->end_frame && mpath->length == expected_length) {
160 mpath->start_frame = avs->path_sf;
161 mpath->end_frame = avs->path_ef + 1;
162 return mpath;
163 }
164
165 /* Clear the existing cache, to allocate a new one below. */
167 }
168 else {
169 mpath = static_cast<bMotionPath *>(MEM_callocN(sizeof(bMotionPath), "bMotionPath"));
170 *dst = mpath;
171 }
172
173 /* Copy mpath settings from the viz settings. */
174 mpath->start_frame = avs->path_sf;
175 mpath->end_frame = avs->path_ef + 1;
176 mpath->length = expected_length;
177
179 mpath->flag |= MOTIONPATH_FLAG_BHEAD;
180 }
181 else {
182 mpath->flag &= ~MOTIONPATH_FLAG_BHEAD;
183 }
184
187 }
188 else {
189 mpath->flag &= ~MOTIONPATH_FLAG_BAKE_CAMERA;
190 }
191
192 /* Set default custom values (RGB). */
193 mpath->color[0] = 1.0;
194 mpath->color[1] = 0.0;
195 mpath->color[2] = 0.0;
196
197 mpath->color_post[0] = 0.1;
198 mpath->color_post[1] = 1.0;
199 mpath->color_post[2] = 0.1;
200
201 mpath->line_thickness = 2;
202 mpath->flag |= MOTIONPATH_FLAG_LINES;
203
204 /* Allocate a cache. */
205 mpath->points = static_cast<bMotionPathVert *>(
206 MEM_callocN(sizeof(bMotionPathVert) * mpath->length, "bMotionPathVerts"));
207
208 /* Tag viz settings as currently having some path(s) which use it. */
210
211 return mpath;
212}
213
215{
216 /* sanity checks */
217 if (mpath == nullptr) {
218 return;
219 }
220
221 /* firstly, just write the motionpath struct */
222 BLO_write_struct(writer, bMotionPath, mpath);
223
224 /* now write the array of data */
225 BLO_write_struct_array(writer, bMotionPathVert, mpath->length, mpath->points);
226}
227
229{
230 /* sanity check */
231 if (mpath == nullptr) {
232 return;
233 }
234
235 /* relink points cache */
236 BLO_read_struct_array(reader, bMotionPathVert, mpath->length, &mpath->points);
237
238 mpath->points_vbo = nullptr;
239 mpath->batch_line = nullptr;
240 mpath->batch_points = nullptr;
241}
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
#define BLI_assert(a)
Definition BLI_assert.h:50
#define ELEM(...)
#define BLO_write_struct(writer, struct_name, data_ptr)
#define BLO_write_struct_array(writer, struct_name, array_size, data_ptr)
#define BLO_read_struct_array(reader, struct_name, array_size, ptr_p)
#define RPT_(msgid)
@ MOTIONPATH_BAKE_CAMERA_SPACE
@ MOTIONPATH_BAKE_HEADS
@ MOTIONPATH_BAKE_HAS_PATHS
@ MOTIONPATH_VIEW_KFNOS
@ MOTIONPATH_VIEW_KFRAS
@ MOTIONPATH_FLAG_LINES
@ MOTIONPATH_FLAG_BAKE_CAMERA
@ MOTIONPATH_FLAG_BHEAD
Object is a sort of wrapper for general info.
#define GPU_BATCH_DISCARD_SAFE(batch)
Definition GPU_batch.hh:205
#define GPU_VERTBUF_DISCARD_SAFE(verts)
Read Guarded memory(de)allocation.
void animviz_settings_init(bAnimVizSettings *avs)
void animviz_free_motionpath_cache(bMotionPath *mpath)
void animviz_motionpath_blend_read_data(BlendDataReader *reader, bMotionPath *mpath)
bMotionPath * animviz_verify_motionpaths(ReportList *reports, Scene *scene, Object *ob, bPoseChannel *pchan)
bMotionPath * animviz_copy_motionpath(const bMotionPath *mpath_src)
void animviz_motionpath_blend_write(BlendWriter *writer, bMotionPath *mpath)
void animviz_free_motionpath(bMotionPath *mpath)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_callocN)(size_t len, const char *str)
Definition mallocn.cc:42
void *(* MEM_dupallocN)(const void *vmemh)
Definition mallocn.cc:39
char name[66]
Definition DNA_ID.h:425
struct bPose * pose
bMotionPath * mpath
bAnimVizSettings avs
GPUBatchHandle * batch_line
bMotionPathVert * points
float color_post[3]
GPUVertBufHandle * points_vbo
GPUBatchHandle * batch_points
bMotionPath * mpath
bAnimVizSettings avs