Blender V5.3
editarmature_undo.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
9#include "MEM_guardedalloc.h"
10
11#include "CLG_log.h"
12
13#include "DNA_armature_types.h"
14#include "DNA_layer_types.h"
15#include "DNA_object_types.h"
16#include "DNA_scene_types.h"
17
18#include "BLI_array_utils_c.hh"
19#include "BLI_listbase.hh"
20#include "BLI_map.hh"
21#include "BLI_string.hh"
22
23#include "BKE_armature.hh"
24#include "BKE_context.hh"
25#include "BKE_layer.hh"
26#include "BKE_main.hh"
27#include "BKE_object.hh"
28#include "BKE_undo_system.hh"
29
30#include "DEG_depsgraph.hh"
31
32#include "ED_armature.hh"
33#include "ED_object.hh"
34#include "ED_undo.hh"
35#include "ED_util.hh"
36
38
39#include "WM_api.hh"
40#include "WM_types.hh"
41
42namespace blender {
43
44using namespace blender::animrig;
45
47static CLG_LogRef LOG = {"undo.armature"};
48
49/* Utility functions. */
50
59 ListBaseT<EditBone> *edit_bones, const Map<BoneCollection *, BoneCollection *> &bcoll_map)
60{
61 for (EditBone &ebone : *edit_bones) {
62 for (BoneCollectionReference &bcoll_ref : ebone.bone_collections) {
63 bcoll_ref.bcoll = bcoll_map.lookup(bcoll_ref.bcoll);
64 }
65 }
66}
67
68/* -------------------------------------------------------------------- */
71
81
83{
84 /* Copy edit bones. */
87
88 /* Active bone. */
89 if (uarm->act_edbone) {
90 EditBone *ebone;
91 ebone = uarm->act_edbone;
92 arm->act_edbone = ebone->temp.ebone;
93 }
94 else {
95 arm->act_edbone = nullptr;
96 }
97
99
100 /* Before freeing the old bone collections, copy their 'expanded' flag. This
101 * flag is not supposed to be restored with any undo steps. */
103 arm->collections_span());
104
105 /* Copy bone collections. */
109 uarm->collection_array,
111 true);
113
114 /* Always do a lookup-by-name and assignment. Even when the name of the active collection is
115 * still the same, the order may have changed and thus the index needs to be updated. */
118 ANIM_armature_bonecoll_active_set(arm, active_bcoll);
119
121
123}
124
126{
128
129 /* Copy edit bones. */
130 ED_armature_ebone_listbase_copy(&uarm->ebones, arm->edbo, false);
131
132 /* Active bone. */
133 if (arm->act_edbone) {
134 EditBone *ebone = arm->act_edbone;
135 uarm->act_edbone = ebone->temp.ebone;
136 }
137
139
140 /* Copy bone collections. */
143 arm->collection_array,
145 false);
148
149 /* Point the new edit bones at the new collections. */
151
152 /* Undo size.
153 * TODO: include size of ID-properties. */
154 uarm->undo_size = 0;
155 for (EditBone &ebone : uarm->ebones) {
156 uarm->undo_size += sizeof(EditBone);
157 uarm->undo_size += sizeof(BoneCollectionReference) * ebone.bone_collections.count();
158 }
159 /* Size of the bone collections + the size of the pointers to those
160 * bone collections in the bone collection array. */
161 uarm->undo_size += (sizeof(BoneCollection) + sizeof(BoneCollection *)) *
163
164 return uarm;
165}
166
172
174{
175 const Main *bmain = CTX_data_main(C);
176 Scene *scene = CTX_data_scene(C);
177 ViewLayer *view_layer = CTX_data_view_layer(C);
178 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
179 Object *obedit = BKE_view_layer_edit_object_get(view_layer);
180 if (obedit && obedit->type == OB_ARMATURE) {
181 bArmature *arm = id_cast<bArmature *>(obedit->data);
182 if (arm->edbo != nullptr) {
183 return obedit;
184 }
185 }
186 return nullptr;
187}
188
190
191/* -------------------------------------------------------------------- */
196
202
210
212{
213 return editarm_object_from_context(C) != nullptr;
214}
215
217{
218 ArmatureUndoStep *us = reinterpret_cast<ArmatureUndoStep *>(us_p);
219
220 /* Important not to use the 3D view when getting objects because all objects
221 * outside of this list will be moved out of edit-mode when reading back undo steps. */
222 Scene *scene = CTX_data_scene(C);
223 ViewLayer *view_layer = CTX_data_view_layer(C);
224 Vector<Object *> objects = ED_undo_editmode_objects_from_view_layer(*bmain, scene, view_layer);
225
226 us->scene_ref.ptr = scene;
227 us->elems = MEM_new_array_zeroed<ArmatureUndoStep_Elem>(objects.size(), __func__);
228 us->elems_len = objects.size();
229
230 for (uint i = 0; i < objects.size(); i++) {
231 Object *ob = objects[i];
232 ArmatureUndoStep_Elem *elem = &us->elems[i];
233
234 elem->obedit_ref.ptr = ob;
235 bArmature *arm = id_cast<bArmature *>(elem->obedit_ref.ptr->data);
236 undoarm_from_editarm(&elem->data, arm);
237 arm->needs_flush_to_id = 1;
238 us->step.data_size += elem->data.undo_size;
239 }
240
241 bmain->is_memfile_undo_flush_needed = true;
242
243 return true;
244}
245
247 bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir /*dir*/, bool /*is_final*/)
248{
249 ArmatureUndoStep *us = reinterpret_cast<ArmatureUndoStep *>(us_p);
250 Scene *scene = CTX_data_scene(C);
251 ViewLayer *view_layer = CTX_data_view_layer(C);
252
254 CTX_wm_manager(C), us->scene_ref.ptr, &scene, &view_layer);
256 scene, view_layer, &us->elems[0].obedit_ref.ptr, us->elems_len, sizeof(*us->elems));
257
259
260 for (uint i = 0; i < us->elems_len; i++) {
261 ArmatureUndoStep_Elem *elem = &us->elems[i];
262 Object *obedit = elem->obedit_ref.ptr;
263 bArmature *arm = id_cast<bArmature *>(obedit->data);
264 if (arm->edbo == nullptr) {
265 /* Should never fail, may not crash but can give odd behavior. */
267 "name='%s', failed to enter edit-mode for object '%s', undo state invalid",
268 us_p->name,
269 obedit->id.name);
270 continue;
271 }
272 undoarm_to_editarm(&elem->data, arm);
273 arm->needs_flush_to_id = 1;
275 }
276
277 /* The first element is always active */
279 *bmain, scene, view_layer, us->elems[0].obedit_ref.ptr, us_p->name, &LOG);
280
281 /* Check after setting active (unless undoing into another scene). */
283
284 bmain->is_memfile_undo_flush_needed = true;
285
287}
288
290{
291 ArmatureUndoStep *us = reinterpret_cast<ArmatureUndoStep *>(us_p);
292
293 for (uint i = 0; i < us->elems_len; i++) {
294 ArmatureUndoStep_Elem *elem = &us->elems[i];
295 undoarm_free_data(&elem->data);
296 }
297 MEM_delete(us->elems);
298}
299
301 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
302 void *user_data)
303{
304 ArmatureUndoStep *us = reinterpret_cast<ArmatureUndoStep *>(us_p);
305
306 foreach_ID_ref_fn(user_data, reinterpret_cast<UndoRefID *>(&us->scene_ref));
307 for (uint i = 0; i < us->elems_len; i++) {
308 ArmatureUndoStep_Elem *elem = &us->elems[i];
309 foreach_ID_ref_fn(user_data, reinterpret_cast<UndoRefID *>(&elem->obedit_ref));
310 }
311}
312
327
329
330} // namespace blender
C++ functions to deal with Armature collections (i.e. the successor of bone layers).
General operations, lookup, etc. for blender objects.
Generic array manipulation API.
#define BLI_array_is_zeroed(arr, arr_len)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define MAX_NAME
Definition DNA_defs.h:63
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
constexpr Span()=default
int64_t size() const
void bonecolls_copy_expanded_flag(Span< BoneCollection * > bcolls_dest, Span< const BoneCollection * > bcolls_source)
Map< BoneCollection *, BoneCollection * > ANIM_bonecoll_array_copy_no_membership(BoneCollection ***bcoll_array_dst, int *bcoll_array_dst_num, BoneCollection **bcoll_array_src, int bcoll_array_src_num, bool do_id_user)
void ANIM_bonecoll_array_free(BoneCollection ***bcoll_array, int *bcoll_array_num, bool do_id_user)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void * undoarm_from_editarm(UndoArmature *uarm, bArmature *arm)
static void remap_ebone_bone_collection_references(ListBaseT< EditBone > *edit_bones, const Map< BoneCollection *, BoneCollection * > &bcoll_map)
char * STRNCPY(char(&dst)[N], const char *src)
unsigned int uint
Definition BKE_text.h:55
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
@ UNDOTYPE_FLAG_ENCODE_PRE_MEMFILE_SUPPORTED
static void undoarm_to_editarm(UndoArmature *uarm, bArmature *arm)
void ED_undo_object_set_active_or_warn(const Main &bmain, Scene *scene, ViewLayer *view_layer, Object *ob, const char *info, CLG_LogRef *log)
Definition ed_undo.cc:1024
void ED_armature_ebone_listbase_copy(ListBaseT< EditBone > *lb_dst, ListBaseT< EditBone > *lb_src, const bool do_id_user)
Vector< Object * > ED_undo_editmode_objects_from_view_layer(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
Definition ed_undo.cc:1109
static Object * editarm_object_from_context(bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
void ED_armature_ebone_listbase_temp_clear(ListBaseT< EditBone > *lb)
static bool armature_undosys_step_encode(bContext *C, Main *bmain, UndoStep *us_p)
Object * BKE_view_layer_edit_object_get(const ViewLayer *view_layer)
void DEG_id_tag_update(ID *id, unsigned int flags)
wmWindowManager * CTX_wm_manager(const bContext *C)
BoneCollection * ANIM_armature_bonecoll_get_by_name(bArmature *armature, const char *name) ATTR_WARN_UNUSED_RESULT
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
static void armature_undosys_step_decode(bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir, bool)
void ED_armature_undosys_type(UndoType *ut)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
bool BKE_object_is_in_editmode(const Object *ob)
Definition object.cc:1898
Main * CTX_data_main(const bContext *C)
void ANIM_armature_bonecoll_active_set(bArmature *armature, BoneCollection *bcoll)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
static CLG_LogRef LOG
Definition logging.cpp:12
void ED_undo_object_editmode_restore_helper(Scene *scene, ViewLayer *view_layer, Object **object_array, uint object_array_len, uint object_array_stride)
Definition ed_undo.cc:1064
static void undoarm_free_data(UndoArmature *uarm)
void(*)(void *user_data, UndoRefID *id_ref) UndoTypeForEachIDRefFn
Scene * CTX_data_scene(const bContext *C)
void ANIM_armature_runtime_refresh(bArmature *armature)
void ED_armature_ebone_listbase_free(ListBaseT< EditBone > *lb, const bool do_id_user)
static void armature_undosys_foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
static void armature_undosys_step_free(UndoStep *us_p)
static bool armature_undosys_poll(bContext *C)
void ED_undo_object_editmode_validate_scene_from_windows(wmWindowManager *wm, const Scene *scene_ref, Scene **scene_p, ViewLayer **view_layer_p)
Definition ed_undo.cc:1047
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
ArmatureUndoStep_Elem * elems
union blender::EditBone::@252003325204232231327014100071366025164067232113 temp
ListBaseT< BoneCollectionReference > bone_collections
char name[258]
Definition DNA_ID.h:433
bool is_memfile_undo_flush_needed
Definition BKE_main.hh:302
char active_collection_name[MAX_NAME]
ListBaseT< EditBone > ebones
BoneCollection ** collection_array
bool(*) poll(bContext *C)
void(*) step_free(UndoStep *us)
void(*) step_decode(bContext *C, Main *bmain, UndoStep *us, eUndoStepDir dir, bool is_final)
void(*) step_foreach_ID_ref(UndoStep *us, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
bool(*) step_encode(bContext *C, Main *bmain, UndoStep *us)
const char * identifier
ListBaseT< EditBone > * edbo
struct EditBone * act_edbone
struct BoneCollection ** collection_array