Blender  V2.93
editcurve_undo.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 
21 #include "MEM_guardedalloc.h"
22 
23 #include "CLG_log.h"
24 
25 #include "DNA_anim_types.h"
26 #include "DNA_object_types.h"
27 #include "DNA_scene_types.h"
28 
29 #include "BLI_array_utils.h"
30 #include "BLI_blenlib.h"
31 #include "BLI_ghash.h"
32 
33 #include "BKE_anim_data.h"
34 #include "BKE_context.h"
35 #include "BKE_curve.h"
36 #include "BKE_fcurve.h"
37 #include "BKE_layer.h"
38 #include "BKE_main.h"
39 #include "BKE_object.h"
40 #include "BKE_undo_system.h"
41 
42 #include "DEG_depsgraph.h"
43 
44 #include "ED_curve.h"
45 #include "ED_undo.h"
46 
47 #include "WM_api.h"
48 #include "WM_types.h"
49 
50 #include "curve_intern.h"
51 
53 static CLG_LogRef LOG = {"ed.undo.curve"};
54 
55 /* -------------------------------------------------------------------- */
59 typedef struct {
61  int actvert;
63  ListBase fcurves, drivers;
64  int actnu;
65  int flag;
66 
67  /* Stored in the object, needed since users may change the active key while in edit-mode. */
68  struct {
69  short shapenr;
70  } obedit;
71 
72  size_t undo_size;
73 } UndoCurve;
74 
75 static void undocurve_to_editcurve(Main *bmain, UndoCurve *ucu, Curve *cu, short *r_shapenr)
76 {
77  ListBase *undobase = &ucu->nubase;
78  ListBase *editbase = BKE_curve_editNurbs_get(cu);
79  Nurb *nu, *newnu;
80  EditNurb *editnurb = cu->editnurb;
81  AnimData *ad = BKE_animdata_from_id(&cu->id);
82 
83  BKE_nurbList_free(editbase);
84 
85  if (ucu->undoIndex) {
88  }
89 
90  if (ad) {
91  if (ad->action) {
93  BKE_fcurves_copy(&ad->action->curves, &ucu->fcurves);
94  }
95 
97  BKE_fcurves_copy(&ad->drivers, &ucu->drivers);
98  }
99 
100  /* copy */
101  for (nu = undobase->first; nu; nu = nu->next) {
102  newnu = BKE_nurb_duplicate(nu);
103 
104  if (editnurb->keyindex) {
105  ED_curve_keyindex_update_nurb(editnurb, nu, newnu);
106  }
107 
108  BLI_addtail(editbase, newnu);
109  }
110 
111  cu->actvert = ucu->actvert;
112  cu->actnu = ucu->actnu;
113  cu->flag = ucu->flag;
114  *r_shapenr = ucu->obedit.shapenr;
115  ED_curve_updateAnimPaths(bmain, cu);
116 }
117 
118 static void undocurve_from_editcurve(UndoCurve *ucu, Curve *cu, const short shapenr)
119 {
121  ListBase *nubase = BKE_curve_editNurbs_get(cu);
122  EditNurb *editnurb = cu->editnurb, tmpEditnurb;
123  Nurb *nu, *newnu;
124  AnimData *ad = BKE_animdata_from_id(&cu->id);
125 
126  /* TODO: include size of fcurve & undoIndex */
127  // ucu->undo_size = 0;
128 
129  if (editnurb->keyindex) {
131  tmpEditnurb.keyindex = ucu->undoIndex;
132  }
133 
134  if (ad) {
135  if (ad->action) {
136  BKE_fcurves_copy(&ucu->fcurves, &ad->action->curves);
137  }
138 
139  BKE_fcurves_copy(&ucu->drivers, &ad->drivers);
140  }
141 
142  /* copy */
143  for (nu = nubase->first; nu; nu = nu->next) {
144  newnu = BKE_nurb_duplicate(nu);
145 
146  if (ucu->undoIndex) {
147  ED_curve_keyindex_update_nurb(&tmpEditnurb, nu, newnu);
148  }
149 
150  BLI_addtail(&ucu->nubase, newnu);
151 
152  ucu->undo_size += ((nu->bezt ? (sizeof(BezTriple) * nu->pntsu) : 0) +
153  (nu->bp ? (sizeof(BPoint) * (nu->pntsu * nu->pntsv)) : 0) +
154  (nu->knotsu ? (sizeof(float) * KNOTSU(nu)) : 0) +
155  (nu->knotsv ? (sizeof(float) * KNOTSV(nu)) : 0) + sizeof(Nurb));
156  }
157 
158  ucu->actvert = cu->actvert;
159  ucu->actnu = cu->actnu;
160  ucu->flag = cu->flag;
161 
162  ucu->obedit.shapenr = shapenr;
163 }
164 
166 {
168 
170 
173 }
174 
176 {
177  ViewLayer *view_layer = CTX_data_view_layer(C);
178  Object *obedit = OBEDIT_FROM_VIEW_LAYER(view_layer);
179  if (obedit && ELEM(obedit->type, OB_CURVE, OB_SURF)) {
180  Curve *cu = obedit->data;
181  if (BKE_curve_editNurbs_get(cu) != NULL) {
182  return obedit;
183  }
184  }
185  return NULL;
186 }
187 
190 /* -------------------------------------------------------------------- */
196 typedef struct CurveUndoStep_Elem {
197  UndoRefID_Object obedit_ref;
200 
201 typedef struct CurveUndoStep {
206 
208 {
210  return (obedit != NULL);
211 }
212 
213 static bool curve_undosys_step_encode(struct bContext *C, struct Main *bmain, UndoStep *us_p)
214 {
215  CurveUndoStep *us = (CurveUndoStep *)us_p;
216 
217  /* Important not to use the 3D view when getting objects because all objects
218  * outside of this list will be moved out of edit-mode when reading back undo steps. */
219  ViewLayer *view_layer = CTX_data_view_layer(C);
220  uint objects_len = 0;
221  Object **objects = ED_undo_editmode_objects_from_view_layer(view_layer, &objects_len);
222 
223  us->elems = MEM_callocN(sizeof(*us->elems) * objects_len, __func__);
224  us->elems_len = objects_len;
225 
226  for (uint i = 0; i < objects_len; i++) {
227  Object *ob = objects[i];
228  Curve *cu = ob->data;
229  CurveUndoStep_Elem *elem = &us->elems[i];
230 
231  elem->obedit_ref.ptr = ob;
232  undocurve_from_editcurve(&elem->data, ob->data, ob->shapenr);
233  cu->editnurb->needs_flush_to_id = 1;
234  us->step.data_size += elem->data.undo_size;
235  }
236  MEM_freeN(objects);
237 
238  bmain->is_memfile_undo_flush_needed = true;
239 
240  return true;
241 }
242 
244  struct Main *bmain,
245  UndoStep *us_p,
246  const eUndoStepDir UNUSED(dir),
247  bool UNUSED(is_final))
248 {
249  CurveUndoStep *us = (CurveUndoStep *)us_p;
250 
252  C, &us->elems[0].obedit_ref.ptr, us->elems_len, sizeof(*us->elems));
253 
255 
256  for (uint i = 0; i < us->elems_len; i++) {
257  CurveUndoStep_Elem *elem = &us->elems[i];
258  Object *obedit = elem->obedit_ref.ptr;
259  Curve *cu = obedit->data;
260  if (cu->editnurb == NULL) {
261  /* Should never fail, may not crash but can give odd behavior. */
262  CLOG_ERROR(&LOG,
263  "name='%s', failed to enter edit-mode for object '%s', undo state invalid",
264  us_p->name,
265  obedit->id.name);
266  continue;
267  }
268  undocurve_to_editcurve(bmain, &elem->data, obedit->data, &obedit->shapenr);
269  cu->editnurb->needs_flush_to_id = 1;
271  }
272 
273  /* The first element is always active */
275  CTX_data_scene(C), CTX_data_view_layer(C), us->elems[0].obedit_ref.ptr, us_p->name, &LOG);
276 
277  /* Check after setting active. */
279 
280  bmain->is_memfile_undo_flush_needed = true;
281 
283 }
284 
286 {
287  CurveUndoStep *us = (CurveUndoStep *)us_p;
288 
289  for (uint i = 0; i < us->elems_len; i++) {
290  CurveUndoStep_Elem *elem = &us->elems[i];
291  undocurve_free_data(&elem->data);
292  }
293  MEM_freeN(us->elems);
294 }
295 
297  UndoTypeForEachIDRefFn foreach_ID_ref_fn,
298  void *user_data)
299 {
300  CurveUndoStep *us = (CurveUndoStep *)us_p;
301 
302  for (uint i = 0; i < us->elems_len; i++) {
303  CurveUndoStep_Elem *elem = &us->elems[i];
304  foreach_ID_ref_fn(user_data, ((UndoRefID *)&elem->obedit_ref));
305  }
306 }
307 
308 /* Export for ED_undo_sys. */
310 {
311  ut->name = "Edit Curve";
312  ut->poll = curve_undosys_poll;
316 
318 
320 
321  ut->step_size = sizeof(CurveUndoStep);
322 }
323 
typedef float(TangentPoint)[2]
struct AnimData * BKE_animdata_from_id(struct ID *id)
Definition: anim_data.c:96
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1044
#define KNOTSU(nu)
Definition: BKE_curve.h:68
struct ListBase * BKE_curve_editNurbs_get(struct Curve *cu)
Definition: curve.c:437
struct Nurb * BKE_nurb_duplicate(const struct Nurb *nu)
#define KNOTSV(nu)
Definition: BKE_curve.h:70
void BKE_nurbList_free(struct ListBase *lb)
Definition: curve.c:660
void BKE_curve_editNurb_keyIndex_free(struct GHash **keyindex)
Definition: curve.c:379
void BKE_fcurves_free(ListBase *list)
Definition: fcurve.c:103
void BKE_fcurves_copy(ListBase *dst, ListBase *src)
Definition: fcurve.c:165
General operations, lookup, etc. for blender objects.
bool BKE_object_is_in_editmode(const struct Object *ob)
eUndoStepDir
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
void(* UndoTypeForEachIDRefFn)(void *user_data, struct UndoRefID *id_ref)
Generic array manipulation API.
#define BLI_array_is_zeroed(arr, arr_len)
#define BLI_assert(a)
Definition: BLI_assert.h:58
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNUSED(x)
#define ELEM(...)
#define CLOG_ERROR(clg_ref,...)
Definition: CLG_log.h:204
void DEG_id_tag_update(struct ID *id, int flag)
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:611
struct Nurb Nurb
struct BezTriple BezTriple
Object is a sort of wrapper for general info.
@ OB_SURF
@ OB_CURVE
#define OBEDIT_FROM_VIEW_LAYER(view_layer)
void ED_undo_object_editmode_restore_helper(struct bContext *C, struct Object **object_array, uint object_array_len, uint object_array_stride)
Definition: ed_undo.c:897
void ED_undo_object_set_active_or_warn(struct Scene *scene, struct ViewLayer *view_layer, struct Object *ob, const char *info, struct CLG_LogRef *log)
Definition: ed_undo.c:877
struct Object ** ED_undo_editmode_objects_from_view_layer(struct ViewLayer *view_layer, uint *r_len)
Definition: ed_undo.c:968
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
#define NC_GEOM
Definition: WM_types.h:294
#define ND_DATA
Definition: WM_types.h:408
struct GHash * ED_curve_keyindex_hash_duplicate(struct GHash *keyindex)
Definition: editcurve.c:536
void ED_curve_keyindex_update_nurb(struct EditNurb *editnurb, struct Nurb *nu, struct Nurb *newnu)
Definition: editcurve.c:347
void * user_data
int ED_curve_updateAnimPaths(Main *bmain, Curve *cu)
Definition: editcurve.c:1078
static void curve_undosys_foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
static Object * editcurve_object_from_context(bContext *C)
static void curve_undosys_step_free(UndoStep *us_p)
static bool curve_undosys_poll(bContext *C)
static bool curve_undosys_step_encode(struct bContext *C, struct Main *bmain, UndoStep *us_p)
void ED_curve_undosys_type(UndoType *ut)
static CLG_LogRef LOG
static void undocurve_to_editcurve(Main *bmain, UndoCurve *ucu, Curve *cu, short *r_shapenr)
struct CurveUndoStep_Elem CurveUndoStep_Elem
static void undocurve_free_data(UndoCurve *uc)
static void undocurve_from_editcurve(UndoCurve *ucu, Curve *cu, const short shapenr)
struct CurveUndoStep CurveUndoStep
static void curve_undosys_step_decode(struct bContext *C, struct Main *bmain, UndoStep *us_p, const eUndoStepDir UNUSED(dir), bool UNUSED(is_final))
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
struct UndoCurve UndoCurve
bAction * action
ListBase drivers
UndoRefID_Object obedit_ref
CurveUndoStep_Elem * elems
EditNurb * editnurb
char needs_flush_to_id
struct GHash * keyindex
char name[66]
Definition: DNA_ID.h:283
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
char is_memfile_undo_flush_needed
Definition: BKE_main.h:130
struct Nurb * next
float * knotsu
float * knotsv
BezTriple * bezt
BPoint * bp
short shapenr
void * data
short shapenr
struct UndoCurve::@317 obedit
size_t undo_size
ListBase drivers
GHash * undoIndex
ListBase fcurves
ListBase nubase
size_t data_size
char name[64]
size_t step_size
void(* step_decode)(struct bContext *C, struct Main *bmain, UndoStep *us, const eUndoStepDir dir, bool is_final)
bool(* step_encode)(struct bContext *C, struct Main *bmain, UndoStep *us)
void(* step_foreach_ID_ref)(UndoStep *us, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
const char * name
void(* step_free)(UndoStep *us)
bool(* poll)(struct bContext *C)
ListBase curves
void WM_event_add_notifier(const bContext *C, uint type, void *reference)