Blender  V3.3
anim_visualization.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
7 #include "MEM_guardedalloc.h"
8 
9 #include "DNA_action_types.h"
10 #include "DNA_anim_types.h"
11 #include "DNA_object_types.h"
12 #include "DNA_scene_types.h"
13 
14 #include "BLT_translation.h"
15 
16 #include "BKE_anim_visualization.h"
17 #include "BKE_report.h"
18 
19 #include "GPU_batch.h"
20 
21 #include "BLO_read_write.h"
22 
23 /* ******************************************************************** */
24 /* Animation Visualization */
25 
27 {
28  /* sanity check */
29  if (avs == NULL) {
30  return;
31  }
32 
33  /* path settings */
34  avs->path_bc = avs->path_ac = 10;
35 
36  avs->path_sf = 1; /* XXX: Take from scene instead? */
37  avs->path_ef = 250; /* XXX: Take from scene instead? */
38 
40 
41  avs->path_step = 1;
42 
44 }
45 
46 /* ------------------- */
47 
49 {
50  /* sanity check */
51  if (mpath == NULL) {
52  return;
53  }
54 
55  /* free the path if necessary */
56  if (mpath->points) {
57  MEM_freeN(mpath->points);
58  }
59 
63 
64  /* reset the relevant parameters */
65  mpath->points = NULL;
66  mpath->length = 0;
67 }
68 
70 {
71  /* sanity check */
72  if (mpath == NULL) {
73  return;
74  }
75 
76  /* free the cache first */
78 
79  /* now the instance itself */
80  MEM_freeN(mpath);
81 }
82 
83 /* ------------------- */
84 
86 {
87  bMotionPath *mpath_dst;
88 
89  if (mpath_src == NULL) {
90  return NULL;
91  }
92 
93  mpath_dst = MEM_dupallocN(mpath_src);
94  mpath_dst->points = MEM_dupallocN(mpath_src->points);
95 
96  /* should get recreated on draw... */
97  mpath_dst->points_vbo = NULL;
98  mpath_dst->batch_line = NULL;
99  mpath_dst->batch_points = NULL;
100 
101  return mpath_dst;
102 }
103 
104 /* ------------------- */
105 
107  Scene *scene,
108  Object *ob,
109  bPoseChannel *pchan)
110 {
111  bAnimVizSettings *avs;
112  bMotionPath *mpath, **dst;
113 
114  /* sanity checks */
115  if (ELEM(NULL, scene, ob)) {
116  return NULL;
117  }
118 
119  /* get destination data */
120  if (pchan) {
121  /* paths for posechannel - assume that posechannel belongs to the object */
122  avs = &ob->pose->avs;
123  dst = &pchan->mpath;
124  }
125  else {
126  /* paths for object */
127  avs = &ob->avs;
128  dst = &ob->mpath;
129  }
130 
131  /* avoid 0 size allocs */
132  if (avs->path_sf >= avs->path_ef) {
133  BKE_reportf(reports,
134  RPT_ERROR,
135  "Motion path frame extents invalid for %s (%d to %d)%s",
136  (pchan) ? pchan->name : ob->id.name,
137  avs->path_sf,
138  avs->path_ef,
139  (avs->path_sf == avs->path_ef) ? TIP_(", cannot have single-frame paths") : "");
140  return NULL;
141  }
142 
143  /* if there is already a motionpath, just return that,
144  * provided its settings are ok (saves extra free+alloc)
145  */
146  if (*dst != NULL) {
147  int expected_length = avs->path_ef - avs->path_sf;
148 
149  mpath = *dst;
150 
151  /* Path is "valid" if length is valid,
152  * but must also be of the same length as is being requested. */
153  if ((mpath->start_frame != mpath->end_frame) && (mpath->length > 0)) {
154  /* outer check ensures that we have some curve data for this path */
155  if (mpath->length == expected_length) {
156  mpath->start_frame = avs->path_sf;
157  mpath->end_frame = avs->path_ef;
158  /* return/use this as it is already valid length */
159  return mpath;
160  }
161  /* clear the existing path (as the range has changed), and reallocate below */
163  }
164  }
165  else {
166  /* create a new motionpath, and assign it */
167  mpath = MEM_callocN(sizeof(bMotionPath), "bMotionPath");
168  *dst = mpath;
169  }
170 
171  /* set settings from the viz settings */
172  mpath->start_frame = avs->path_sf;
173  mpath->end_frame = avs->path_ef;
174 
175  mpath->length = mpath->end_frame - mpath->start_frame;
176 
178  mpath->flag |= MOTIONPATH_FLAG_BHEAD;
179  }
180  else {
181  mpath->flag &= ~MOTIONPATH_FLAG_BHEAD;
182  }
183 
184  /* set default custom values */
185  mpath->color[0] = 1.0; /* Red */
186  mpath->color[1] = 0.0;
187  mpath->color[2] = 0.0;
188 
189  mpath->line_thickness = 2;
190  mpath->flag |= MOTIONPATH_FLAG_LINES; /* draw lines by default */
191 
192  /* allocate a cache */
193  mpath->points = MEM_callocN(sizeof(bMotionPathVert) * mpath->length, "bMotionPathVerts");
194 
195  /* tag viz settings as currently having some path(s) which use it */
197 
198  /* return it */
199  return mpath;
200 }
201 
203 {
204  /* sanity checks */
205  if (mpath == NULL) {
206  return;
207  }
208 
209  /* firstly, just write the motionpath struct */
210  BLO_write_struct(writer, bMotionPath, mpath);
211 
212  /* now write the array of data */
213  BLO_write_struct_array(writer, bMotionPathVert, mpath->length, mpath->points);
214 }
215 
217 {
218  /* sanity check */
219  if (mpath == NULL) {
220  return;
221  }
222 
223  /* relink points cache */
224  BLO_read_data_address(reader, &mpath->points);
225 
226  mpath->points_vbo = NULL;
227  mpath->batch_line = NULL;
228  mpath->batch_points = NULL;
229 }
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
#define ELEM(...)
#define BLO_read_data_address(reader, ptr_p)
#define BLO_write_struct(writer, struct_name, data_ptr)
#define BLO_write_struct_array(writer, struct_name, array_size, data_ptr)
#define TIP_(msgid)
@ MOTIONPATH_BAKE_HEADS
@ MOTIONPATH_BAKE_HAS_PATHS
@ MOTIONPATH_VIEW_KFNOS
@ MOTIONPATH_VIEW_KFRAS
@ MOTIONPATH_FLAG_LINES
@ MOTIONPATH_FLAG_BHEAD
Object is a sort of wrapper for general info.
#define GPU_BATCH_DISCARD_SAFE(batch)
Definition: GPU_batch.h:216
#define GPU_VERTBUF_DISCARD_SAFE(verts)
Read Guarded memory(de)allocation.
void animviz_settings_init(bAnimVizSettings *avs)
void animviz_free_motionpath_cache(bMotionPath *mpath)
bMotionPath * animviz_copy_motionpath(const bMotionPath *mpath_src)
void animviz_motionpath_blend_read_data(BlendDataReader *reader, bMotionPath *mpath)
bMotionPath * animviz_verify_motionpaths(ReportList *reports, Scene *scene, Object *ob, bPoseChannel *pchan)
void animviz_motionpath_blend_write(BlendWriter *writer, bMotionPath *mpath)
void animviz_free_motionpath(bMotionPath *mpath)
Scene scene
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:28
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
char name[66]
Definition: DNA_ID.h:378
struct bPose * pose
bMotionPath * mpath
bAnimVizSettings avs
struct GPUVertBuf * points_vbo
bMotionPathVert * points
struct GPUBatch * batch_points
struct GPUBatch * batch_line
float color[3]
bMotionPath * mpath
bAnimVizSettings avs