Blender  V2.93
anim_visualization.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
23 #include "MEM_guardedalloc.h"
24 
25 #include "DNA_action_types.h"
26 #include "DNA_anim_types.h"
27 #include "DNA_object_types.h"
28 #include "DNA_scene_types.h"
29 
30 #include "BLT_translation.h"
31 
32 #include "BKE_anim_visualization.h"
33 #include "BKE_report.h"
34 
35 #include "GPU_batch.h"
36 
37 #include "BLO_read_write.h"
38 
39 /* ******************************************************************** */
40 /* Animation Visualization */
41 
42 /* Initialize the default settings for animation visualization */
44 {
45  /* sanity check */
46  if (avs == NULL) {
47  return;
48  }
49 
50  /* path settings */
51  avs->path_bc = avs->path_ac = 10;
52 
53  avs->path_sf = 1; /* xxx - take from scene instead? */
54  avs->path_ef = 250; /* xxx - take from scene instead? */
55 
57 
58  avs->path_step = 1;
59 
61 }
62 
63 /* ------------------- */
64 
65 /* Free the given motion path's cache */
67 {
68  /* sanity check */
69  if (mpath == NULL) {
70  return;
71  }
72 
73  /* free the path if necessary */
74  if (mpath->points) {
75  MEM_freeN(mpath->points);
76  }
77 
81 
82  /* reset the relevant parameters */
83  mpath->points = NULL;
84  mpath->length = 0;
85 }
86 
87 /* Free the given motion path instance and its data
88  * NOTE: this frees the motion path given!
89  */
91 {
92  /* sanity check */
93  if (mpath == NULL) {
94  return;
95  }
96 
97  /* free the cache first */
99 
100  /* now the instance itself */
101  MEM_freeN(mpath);
102 }
103 
104 /* ------------------- */
105 
106 /* Make a copy of motionpath data, so that viewing with copy on write works */
108 {
109  bMotionPath *mpath_dst;
110 
111  if (mpath_src == NULL) {
112  return NULL;
113  }
114 
115  mpath_dst = MEM_dupallocN(mpath_src);
116  mpath_dst->points = MEM_dupallocN(mpath_src->points);
117 
118  /* should get recreated on draw... */
119  mpath_dst->points_vbo = NULL;
120  mpath_dst->batch_line = NULL;
121  mpath_dst->batch_points = NULL;
122 
123  return mpath_dst;
124 }
125 
126 /* ------------------- */
127 
137  Scene *scene,
138  Object *ob,
139  bPoseChannel *pchan)
140 {
141  bAnimVizSettings *avs;
142  bMotionPath *mpath, **dst;
143 
144  /* sanity checks */
145  if (ELEM(NULL, scene, ob)) {
146  return NULL;
147  }
148 
149  /* get destination data */
150  if (pchan) {
151  /* paths for posechannel - assume that posechannel belongs to the object */
152  avs = &ob->pose->avs;
153  dst = &pchan->mpath;
154  }
155  else {
156  /* paths for object */
157  avs = &ob->avs;
158  dst = &ob->mpath;
159  }
160 
161  /* avoid 0 size allocs */
162  if (avs->path_sf >= avs->path_ef) {
163  BKE_reportf(reports,
164  RPT_ERROR,
165  "Motion path frame extents invalid for %s (%d to %d)%s",
166  (pchan) ? pchan->name : ob->id.name,
167  avs->path_sf,
168  avs->path_ef,
169  (avs->path_sf == avs->path_ef) ? TIP_(", cannot have single-frame paths") : "");
170  return NULL;
171  }
172 
173  /* if there is already a motionpath, just return that,
174  * provided its settings are ok (saves extra free+alloc)
175  */
176  if (*dst != NULL) {
177  int expected_length = avs->path_ef - avs->path_sf;
178 
179  mpath = *dst;
180 
181  /* Path is "valid" if length is valid,
182  * but must also be of the same length as is being requested. */
183  if ((mpath->start_frame != mpath->end_frame) && (mpath->length > 0)) {
184  /* outer check ensures that we have some curve data for this path */
185  if (mpath->length == expected_length) {
186  /* return/use this as it is already valid length */
187  return mpath;
188  }
189  /* clear the existing path (as the range has changed), and reallocate below */
191  }
192  }
193  else {
194  /* create a new motionpath, and assign it */
195  mpath = MEM_callocN(sizeof(bMotionPath), "bMotionPath");
196  *dst = mpath;
197  }
198 
199  /* set settings from the viz settings */
200  mpath->start_frame = avs->path_sf;
201  mpath->end_frame = avs->path_ef;
202 
203  mpath->length = mpath->end_frame - mpath->start_frame;
204 
206  mpath->flag |= MOTIONPATH_FLAG_BHEAD;
207  }
208  else {
209  mpath->flag &= ~MOTIONPATH_FLAG_BHEAD;
210  }
211 
212  /* set default custom values */
213  mpath->color[0] = 1.0; /* Red */
214  mpath->color[1] = 0.0;
215  mpath->color[2] = 0.0;
216 
217  mpath->line_thickness = 2;
218  mpath->flag |= MOTIONPATH_FLAG_LINES; /* draw lines by default */
219 
220  /* allocate a cache */
221  mpath->points = MEM_callocN(sizeof(bMotionPathVert) * mpath->length, "bMotionPathVerts");
222 
223  /* tag viz settings as currently having some path(s) which use it */
225 
226  /* return it */
227  return mpath;
228 }
229 
231 {
232  /* sanity checks */
233  if (mpath == NULL) {
234  return;
235  }
236 
237  /* firstly, just write the motionpath struct */
238  BLO_write_struct(writer, bMotionPath, mpath);
239 
240  /* now write the array of data */
241  BLO_write_struct_array(writer, bMotionPathVert, mpath->length, mpath->points);
242 }
243 
245 {
246  /* sanity check */
247  if (mpath == NULL) {
248  return;
249  }
250 
251  /* relink points cache */
252  BLO_read_data_address(reader, &mpath->points);
253 
254  mpath->points_vbo = NULL;
255  mpath->batch_line = NULL;
256  mpath->batch_points = NULL;
257 }
void BKE_reportf(ReportList *reports, ReportType 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:199
#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:41
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:42
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
char name[66]
Definition: DNA_ID.h:283
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