Blender  V2.93
editarmature_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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
24 #include "MEM_guardedalloc.h"
25 
26 #include "CLG_log.h"
27 
28 #include "DNA_armature_types.h"
29 #include "DNA_layer_types.h"
30 #include "DNA_object_types.h"
31 #include "DNA_scene_types.h"
32 
33 #include "BLI_array_utils.h"
34 #include "BLI_listbase.h"
35 
36 #include "BKE_armature.h"
37 #include "BKE_context.h"
38 #include "BKE_layer.h"
39 #include "BKE_main.h"
40 #include "BKE_object.h"
41 #include "BKE_undo_system.h"
42 
43 #include "DEG_depsgraph.h"
44 
45 #include "ED_armature.h"
46 #include "ED_object.h"
47 #include "ED_undo.h"
48 #include "ED_util.h"
49 
50 #include "WM_api.h"
51 #include "WM_types.h"
52 
54 static CLG_LogRef LOG = {"ed.undo.armature"};
55 
56 /* -------------------------------------------------------------------- */
60 typedef struct UndoArmature {
63  size_t undo_size;
65 
66 static void undoarm_to_editarm(UndoArmature *uarm, bArmature *arm)
67 {
68  EditBone *ebone;
69 
71  ED_armature_ebone_listbase_copy(arm->edbo, &uarm->lb, true);
72 
73  /* active bone */
74  if (uarm->act_edbone) {
75  ebone = uarm->act_edbone;
76  arm->act_edbone = ebone->temp.ebone;
77  }
78  else {
79  arm->act_edbone = NULL;
80  }
81 
83 }
84 
85 static void *undoarm_from_editarm(UndoArmature *uarm, bArmature *arm)
86 {
88 
89  /* TODO: include size of ID-properties. */
90  uarm->undo_size = 0;
91 
92  ED_armature_ebone_listbase_copy(&uarm->lb, arm->edbo, false);
93 
94  /* active bone */
95  if (arm->act_edbone) {
96  EditBone *ebone = arm->act_edbone;
97  uarm->act_edbone = ebone->temp.ebone;
98  }
99 
101 
102  LISTBASE_FOREACH (EditBone *, ebone, &uarm->lb) {
103  uarm->undo_size += sizeof(EditBone);
104  }
105 
106  return uarm;
107 }
108 
109 static void undoarm_free_data(UndoArmature *uarm)
110 {
111  ED_armature_ebone_listbase_free(&uarm->lb, false);
112 }
113 
115 {
116  ViewLayer *view_layer = CTX_data_view_layer(C);
117  Object *obedit = OBEDIT_FROM_VIEW_LAYER(view_layer);
118  if (obedit && obedit->type == OB_ARMATURE) {
119  bArmature *arm = obedit->data;
120  if (arm->edbo != NULL) {
121  return obedit;
122  }
123  }
124  return NULL;
125 }
126 
129 /* -------------------------------------------------------------------- */
135 typedef struct ArmatureUndoStep_Elem {
137  UndoRefID_Object obedit_ref;
140 
141 typedef struct ArmatureUndoStep {
146 
148 {
150 }
151 
152 static bool armature_undosys_step_encode(struct bContext *C, struct Main *bmain, UndoStep *us_p)
153 {
154  ArmatureUndoStep *us = (ArmatureUndoStep *)us_p;
155 
156  /* Important not to use the 3D view when getting objects because all objects
157  * outside of this list will be moved out of edit-mode when reading back undo steps. */
158  ViewLayer *view_layer = CTX_data_view_layer(C);
159  uint objects_len = 0;
160  Object **objects = ED_undo_editmode_objects_from_view_layer(view_layer, &objects_len);
161 
162  us->elems = MEM_callocN(sizeof(*us->elems) * objects_len, __func__);
163  us->elems_len = objects_len;
164 
165  for (uint i = 0; i < objects_len; i++) {
166  Object *ob = objects[i];
167  ArmatureUndoStep_Elem *elem = &us->elems[i];
168 
169  elem->obedit_ref.ptr = ob;
170  bArmature *arm = elem->obedit_ref.ptr->data;
171  undoarm_from_editarm(&elem->data, arm);
172  arm->needs_flush_to_id = 1;
173  us->step.data_size += elem->data.undo_size;
174  }
175  MEM_freeN(objects);
176 
177  bmain->is_memfile_undo_flush_needed = true;
178 
179  return true;
180 }
181 
183  struct Main *bmain,
184  UndoStep *us_p,
185  const eUndoStepDir UNUSED(dir),
186  bool UNUSED(is_final))
187 {
188  ArmatureUndoStep *us = (ArmatureUndoStep *)us_p;
189 
191  C, &us->elems[0].obedit_ref.ptr, us->elems_len, sizeof(*us->elems));
192 
194 
195  for (uint i = 0; i < us->elems_len; i++) {
196  ArmatureUndoStep_Elem *elem = &us->elems[i];
197  Object *obedit = elem->obedit_ref.ptr;
198  bArmature *arm = obedit->data;
199  if (arm->edbo == NULL) {
200  /* Should never fail, may not crash but can give odd behavior. */
201  CLOG_ERROR(&LOG,
202  "name='%s', failed to enter edit-mode for object '%s', undo state invalid",
203  us_p->name,
204  obedit->id.name);
205  continue;
206  }
207  undoarm_to_editarm(&elem->data, arm);
208  arm->needs_flush_to_id = 1;
210  }
211 
212  /* The first element is always active */
214  CTX_data_scene(C), CTX_data_view_layer(C), us->elems[0].obedit_ref.ptr, us_p->name, &LOG);
215 
216  /* Check after setting active. */
218 
219  bmain->is_memfile_undo_flush_needed = true;
220 
222 }
223 
225 {
226  ArmatureUndoStep *us = (ArmatureUndoStep *)us_p;
227 
228  for (uint i = 0; i < us->elems_len; i++) {
229  ArmatureUndoStep_Elem *elem = &us->elems[i];
230  undoarm_free_data(&elem->data);
231  }
232  MEM_freeN(us->elems);
233 }
234 
236  UndoTypeForEachIDRefFn foreach_ID_ref_fn,
237  void *user_data)
238 {
239  ArmatureUndoStep *us = (ArmatureUndoStep *)us_p;
240 
241  for (uint i = 0; i < us->elems_len; i++) {
242  ArmatureUndoStep_Elem *elem = &us->elems[i];
243  foreach_ID_ref_fn(user_data, ((UndoRefID *)&elem->obedit_ref));
244  }
245 }
246 
247 /* Export for ED_undo_sys. */
249 {
250  ut->name = "Edit Armature";
255 
257 
259 
260  ut->step_size = sizeof(ArmatureUndoStep);
261 }
262 
struct EditBone EditBone
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
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
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNUSED(x)
#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
Object is a sort of wrapper for general info.
@ OB_ARMATURE
#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
void ED_armature_ebone_listbase_temp_clear(ListBase *lb)
void ED_armature_ebone_listbase_free(ListBase *lb, const bool do_id_user)
void ED_armature_ebone_listbase_copy(ListBase *lb_dst, ListBase *lb_src, const bool do_id_user)
void * user_data
static void armature_undosys_foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
static bool armature_undosys_poll(bContext *C)
static void armature_undosys_step_decode(struct bContext *C, struct Main *bmain, UndoStep *us_p, const eUndoStepDir UNUSED(dir), bool UNUSED(is_final))
struct ArmatureUndoStep_Elem ArmatureUndoStep_Elem
static bool armature_undosys_step_encode(struct bContext *C, struct Main *bmain, UndoStep *us_p)
static void * undoarm_from_editarm(UndoArmature *uarm, bArmature *arm)
static Object * editarm_object_from_context(bContext *C)
static void armature_undosys_step_free(UndoStep *us_p)
void ED_armature_undosys_type(UndoType *ut)
static void undoarm_free_data(UndoArmature *uarm)
struct ArmatureUndoStep ArmatureUndoStep
static void undoarm_to_editarm(UndoArmature *uarm, bArmature *arm)
static CLG_LogRef LOG
struct UndoArmature UndoArmature
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
struct ArmatureUndoStep_Elem * next
UndoRefID_Object obedit_ref
struct ArmatureUndoStep_Elem * prev
ArmatureUndoStep_Elem * elems
struct EditBone * ebone
Definition: BKE_armature.h:112
union EditBone::@2 temp
char name[66]
Definition: DNA_ID.h:283
Definition: BKE_main.h:116
char is_memfile_undo_flush_needed
Definition: BKE_main.h:130
void * data
EditBone * act_edbone
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)
struct EditBone * act_edbone
ListBase * edbo
void WM_event_add_notifier(const bContext *C, uint type, void *reference)