2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2006 by Nicholas Bishop
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
27 * Implements the Sculpt Mode tools
31 /** \file blender/editors/sculpt_paint/sculpt_undo.c
37 #include "MEM_guardedalloc.h"
40 #include "BLI_utildefines.h"
41 #include "BLI_string.h"
42 #include "BLI_listbase.h"
43 #include "BLI_ghash.h"
44 #include "BLI_threads.h"
46 #include "DNA_meshdata_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_mesh_types.h"
52 #include "BKE_context.h"
53 #include "BKE_depsgraph.h"
54 #include "BKE_multires.h"
55 #include "BKE_paint.h"
58 #include "BKE_subsurf.h"
63 #include "GPU_buffers.h"
65 #include "ED_sculpt.h"
67 #include "paint_intern.h"
68 #include "sculpt_intern.h"
70 /************************** Undo *************************/
72 static void update_cb(PBVHNode *node, void *rebuild)
74 BKE_pbvh_node_mark_update(node);
75 if (*((bool *)rebuild))
76 BKE_pbvh_node_mark_rebuild_draw(node);
77 BKE_pbvh_node_fully_hidden_set(node, 0);
80 static void sculpt_undo_restore_deformed(const SculptSession *ss,
81 SculptUndoNode *unode,
82 int uindex, int oindex,
86 swap_v3_v3(coord, unode->orig_co[uindex]);
87 copy_v3_v3(unode->co[uindex], ss->deform_cos[oindex]);
90 swap_v3_v3(coord, unode->co[uindex]);
94 static int sculpt_undo_restore_coords(bContext *C, DerivedMesh *dm, SculptUndoNode *unode)
96 Scene *scene = CTX_data_scene(C);
97 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
98 Object *ob = CTX_data_active_object(C);
99 SculptSession *ss = ob->sculpt;
103 if (unode->maxvert) {
104 /* regular mesh restore */
106 if (ss->kb && strcmp(ss->kb->name, unode->shapeName)) {
107 /* shape key has been changed before calling undo operator */
109 Key *key = BKE_key_from_object(ob);
110 KeyBlock *kb = key ? BKE_keyblock_find_name(key, unode->shapeName) : NULL;
113 ob->shapenr = BLI_findindex(&key->block, kb) + 1;
115 BKE_sculpt_update_mesh_elements(scene, sd, ob, 0, false);
116 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, ob);
119 /* key has been removed -- skip this undo node */
124 index = unode->index;
129 vertCos = BKE_key_convert_to_vertcos(ob, ss->kb);
131 for (i = 0; i < unode->totvert; i++) {
132 if (ss->modifiers_active) {
133 sculpt_undo_restore_deformed(ss, unode, i, index[i], vertCos[index[i]]);
136 if (unode->orig_co) swap_v3_v3(vertCos[index[i]], unode->orig_co[i]);
137 else swap_v3_v3(vertCos[index[i]], unode->co[i]);
141 /* propagate new coords to keyblock */
142 sculpt_vertcos_to_key(ob, ss->kb, vertCos);
144 /* pbvh uses it's own mvert array, so coords should be */
145 /* propagated to pbvh here */
146 BKE_pbvh_apply_vertCos(ss->pbvh, vertCos);
151 for (i = 0; i < unode->totvert; i++) {
152 if (ss->modifiers_active) {
153 sculpt_undo_restore_deformed(ss, unode, i, index[i], mvert[index[i]].co);
156 if (unode->orig_co) swap_v3_v3(mvert[index[i]].co, unode->orig_co[i]);
157 else swap_v3_v3(mvert[index[i]].co, unode->co[i]);
159 mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
163 else if (unode->maxgrid && dm->getGridData) {
164 /* multires restore */
165 CCGElem **grids, *grid;
170 grids = dm->getGridData(dm);
171 gridsize = dm->getGridSize(dm);
172 dm->getGridKey(dm, &key);
175 for (j = 0; j < unode->totgrid; j++) {
176 grid = grids[unode->grids[j]];
178 for (i = 0; i < gridsize * gridsize; i++, co++)
179 swap_v3_v3(CCG_elem_offset_co(&key, grid, i), co[0]);
186 static int sculpt_undo_restore_hidden(bContext *C, DerivedMesh *dm,
187 SculptUndoNode *unode)
189 Object *ob = CTX_data_active_object(C);
190 SculptSession *ss = ob->sculpt;
193 if (unode->maxvert) {
194 MVert *mvert = ss->mvert;
196 for (i = 0; i < unode->totvert; i++) {
197 MVert *v = &mvert[unode->index[i]];
198 int uval = BLI_BITMAP_GET(unode->vert_hidden, i);
200 BLI_BITMAP_MODIFY(unode->vert_hidden, i,
207 v->flag |= ME_VERT_PBVH_UPDATE;
210 else if (unode->maxgrid && dm->getGridData) {
211 BLI_bitmap **grid_hidden = dm->getGridHidden(dm);
213 for (i = 0; i < unode->totgrid; i++) {
215 unode->grid_hidden[i],
216 grid_hidden[unode->grids[i]]);
224 static int sculpt_undo_restore_mask(bContext *C, DerivedMesh *dm, SculptUndoNode *unode)
226 Object *ob = CTX_data_active_object(C);
227 SculptSession *ss = ob->sculpt;
232 if (unode->maxvert) {
233 /* regular mesh restore */
235 index = unode->index;
239 for (i = 0; i < unode->totvert; i++) {
240 SWAP(float, vmask[index[i]], unode->mask[i]);
241 mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
244 else if (unode->maxgrid && dm->getGridData) {
245 /* multires restore */
246 CCGElem **grids, *grid;
251 grids = dm->getGridData(dm);
252 gridsize = dm->getGridSize(dm);
253 dm->getGridKey(dm, &key);
256 for (j = 0; j < unode->totgrid; j++) {
257 grid = grids[unode->grids[j]];
259 for (i = 0; i < gridsize * gridsize; i++, mask++)
260 SWAP(float, *CCG_elem_offset_mask(&key, grid, i), *mask);
267 static void sculpt_undo_bmesh_restore_generic(bContext *C,
268 SculptUndoNode *unode,
272 if (unode->applied) {
273 BM_log_undo(ss->bm, ss->bm_log);
274 unode->applied = false;
277 BM_log_redo(ss->bm, ss->bm_log);
278 unode->applied = true;
281 if (ELEM(unode->type, SCULPT_UNDO_MASK, SCULPT_UNDO_MASK)) {
286 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
291 BKE_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
293 #pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
294 for (i = 0; i < totnode; i++) {
295 BKE_pbvh_node_mark_redraw(nodes[i]);
302 sculpt_pbvh_clear(ob);
306 /* Create empty sculpt BMesh and enable logging */
307 static void sculpt_undo_bmesh_enable(Object *ob,
308 SculptUndoNode *unode)
310 SculptSession *ss = ob->sculpt;
313 sculpt_pbvh_clear(ob);
315 /* Create empty BMesh and enable logging */
316 ss->bm = BM_mesh_create(&bm_mesh_allocsize_default);
317 BM_data_layer_add(ss->bm, &ss->bm->vdata, CD_PAINT_MASK);
318 sculpt_dyntopo_node_layers_add(ss);
319 me->flag |= ME_SCULPT_DYNAMIC_TOPOLOGY;
321 /* Restore the BMLog using saved entries */
322 ss->bm_log = BM_log_from_existing_entries_create(ss->bm,
326 static void sculpt_undo_bmesh_restore_begin(bContext *C,
327 SculptUndoNode *unode,
331 if (unode->applied) {
332 sculpt_dynamic_topology_disable(C, unode);
333 unode->applied = false;
336 sculpt_undo_bmesh_enable(ob, unode);
338 /* Restore the mesh from the first log entry */
339 BM_log_redo(ss->bm, ss->bm_log);
341 unode->applied = true;
345 static void sculpt_undo_bmesh_restore_end(bContext *C,
346 SculptUndoNode *unode,
350 if (unode->applied) {
351 sculpt_undo_bmesh_enable(ob, unode);
353 /* Restore the mesh from the last log entry */
354 BM_log_undo(ss->bm, ss->bm_log);
356 unode->applied = false;
359 /* Disable dynamic topology sculpting */
360 sculpt_dynamic_topology_disable(C, NULL);
361 unode->applied = true;
365 /* Handle all dynamic-topology updates
367 * Returns true if this was a dynamic-topology undo step, otherwise
368 * returns false to indicate the non-dyntopo code should run. */
369 static int sculpt_undo_bmesh_restore(bContext *C,
370 SculptUndoNode *unode,
374 switch (unode->type) {
375 case SCULPT_UNDO_DYNTOPO_BEGIN:
376 sculpt_undo_bmesh_restore_begin(C, unode, ob, ss);
379 case SCULPT_UNDO_DYNTOPO_END:
380 sculpt_undo_bmesh_restore_end(C, unode, ob, ss);
385 sculpt_undo_bmesh_restore_generic(C, unode, ob, ss);
394 static void sculpt_undo_restore(bContext *C, ListBase *lb)
396 Scene *scene = CTX_data_scene(C);
397 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
398 Object *ob = CTX_data_active_object(C);
400 SculptSession *ss = ob->sculpt;
401 SculptUndoNode *unode;
402 bool update = false, rebuild = false;
403 bool need_mask = false;
405 for (unode = lb->first; unode; unode = unode->next) {
406 if (strcmp(unode->idname, ob->id.name) == 0) {
407 if (unode->type == SCULPT_UNDO_MASK) {
408 /* is possible that we can't do the mask undo (below)
409 * because of the vertex count */
416 BKE_sculpt_update_mesh_elements(scene, sd, ob, 0, need_mask);
418 /* call _after_ sculpt_update_mesh_elements() which may update 'ob->derivedFinal' */
419 dm = mesh_get_derived_final(scene, ob, 0);
421 if (lb->first && sculpt_undo_bmesh_restore(C, lb->first, ob, ss))
424 for (unode = lb->first; unode; unode = unode->next) {
425 if (!(strcmp(unode->idname, ob->id.name) == 0))
428 /* check if undo data matches current data well enough to
430 if (unode->maxvert) {
431 if (ss->totvert != unode->maxvert)
434 else if (unode->maxgrid && dm->getGridData) {
435 if ((dm->getNumGrids(dm) != unode->maxgrid) ||
436 (dm->getGridSize(dm) != unode->gridsize))
442 switch (unode->type) {
443 case SCULPT_UNDO_COORDS:
444 if (sculpt_undo_restore_coords(C, dm, unode))
447 case SCULPT_UNDO_HIDDEN:
448 if (sculpt_undo_restore_hidden(C, dm, unode))
451 case SCULPT_UNDO_MASK:
452 if (sculpt_undo_restore_mask(C, dm, unode))
456 case SCULPT_UNDO_DYNTOPO_BEGIN:
457 case SCULPT_UNDO_DYNTOPO_END:
458 case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
459 BLI_assert(!"Dynamic topology should've already been handled");
464 if (update || rebuild) {
465 bool tag_update = false;
466 /* we update all nodes still, should be more clever, but also
467 * needs to work correct when exiting/entering sculpt mode and
468 * the nodes get recreated, though in that case it could do all */
469 BKE_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb, &rebuild);
470 BKE_pbvh_update(ss->pbvh, PBVH_UpdateBB | PBVH_UpdateOriginalBB | PBVH_UpdateRedraw, NULL);
472 if (BKE_sculpt_multires_active(scene, ob)) {
474 multires_mark_as_modified(ob, MULTIRES_HIDDEN_MODIFIED);
476 multires_mark_as_modified(ob, MULTIRES_COORDS_MODIFIED);
479 tag_update |= ((Mesh *)ob->data)->id.us > 1;
481 if (ss->kb || ss->modifiers_active) {
482 Mesh *mesh = ob->data;
483 BKE_mesh_calc_normals_tessface(mesh->mvert, mesh->totvert,
484 mesh->mface, mesh->totface, NULL);
486 BKE_free_sculptsession_deformMats(ss);
491 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
494 sculpt_update_object_bounding_box(ob);
497 /* for non-PBVH drawing, need to recreate VBOs */
498 GPU_drawobject_free(ob->derivedFinal);
502 static void sculpt_undo_free(ListBase *lb)
504 SculptUndoNode *unode;
507 for (unode = lb->first; unode; unode = unode->next) {
509 MEM_freeN(unode->co);
511 MEM_freeN(unode->no);
513 MEM_freeN(unode->index);
515 MEM_freeN(unode->grids);
517 MEM_freeN(unode->orig_co);
518 if (unode->vert_hidden)
519 MEM_freeN(unode->vert_hidden);
520 if (unode->grid_hidden) {
521 for (i = 0; i < unode->totgrid; i++) {
522 if (unode->grid_hidden[i])
523 MEM_freeN(unode->grid_hidden[i]);
525 MEM_freeN(unode->grid_hidden);
528 MEM_freeN(unode->mask);
530 if (unode->bm_entry) {
531 BM_log_entry_drop(unode->bm_entry);
534 if (unode->bm_enter_totvert)
535 CustomData_free(&unode->bm_enter_vdata, unode->bm_enter_totvert);
536 if (unode->bm_enter_totedge)
537 CustomData_free(&unode->bm_enter_edata, unode->bm_enter_totedge);
538 if (unode->bm_enter_totloop)
539 CustomData_free(&unode->bm_enter_ldata, unode->bm_enter_totloop);
540 if (unode->bm_enter_totpoly)
541 CustomData_free(&unode->bm_enter_pdata, unode->bm_enter_totpoly);
545 bool sculpt_undo_cleanup(bContext *C, ListBase *lb) {
546 Object *ob = CTX_data_active_object(C);
547 SculptUndoNode *unode;
551 if (strcmp(unode->idname, ob->id.name) != 0) {
552 for (unode = lb->first; unode; unode = unode->next) {
554 BM_log_cleanup_entry(unode->bm_entry);
563 SculptUndoNode *sculpt_undo_get_node(PBVHNode *node)
565 ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
571 return BLI_findptr(lb, node, offsetof(SculptUndoNode, node));
574 static void sculpt_undo_alloc_and_store_hidden(PBVH *pbvh,
575 SculptUndoNode *unode)
577 PBVHNode *node = unode->node;
578 BLI_bitmap **grid_hidden;
579 int i, *grid_indices, totgrid;
581 grid_hidden = BKE_pbvh_grid_hidden(pbvh);
583 BKE_pbvh_node_get_grids(pbvh, node, &grid_indices, &totgrid,
584 NULL, NULL, NULL, NULL);
586 unode->grid_hidden = MEM_mapallocN(sizeof(*unode->grid_hidden) * totgrid,
587 "unode->grid_hidden");
589 for (i = 0; i < totgrid; i++) {
590 if (grid_hidden[grid_indices[i]])
591 unode->grid_hidden[i] = MEM_dupallocN(grid_hidden[grid_indices[i]]);
593 unode->grid_hidden[i] = NULL;
597 static SculptUndoNode *sculpt_undo_alloc_node(Object *ob, PBVHNode *node,
600 ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
601 SculptUndoNode *unode;
602 SculptSession *ss = ob->sculpt;
603 int totvert, allvert, totgrid, maxgrid, gridsize, *grids;
605 unode = MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
606 BLI_strncpy(unode->idname, ob->id.name, sizeof(unode->idname));
611 BKE_pbvh_node_num_verts(ss->pbvh, node, &totvert, &allvert);
612 BKE_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
613 &maxgrid, &gridsize, NULL, NULL);
615 unode->totvert = totvert;
620 /* we will use this while sculpting, is mapalloc slow to access then? */
622 /* general TODO, fix count_alloc */
624 case SCULPT_UNDO_COORDS:
625 unode->co = MEM_mapallocN(sizeof(float) * 3 * allvert, "SculptUndoNode.co");
626 unode->no = MEM_mapallocN(sizeof(short) * 3 * allvert, "SculptUndoNode.no");
627 undo_paint_push_count_alloc(UNDO_PAINT_MESH,
630 sizeof(int)) * allvert);
632 case SCULPT_UNDO_HIDDEN:
634 sculpt_undo_alloc_and_store_hidden(ss->pbvh, unode);
636 unode->vert_hidden = BLI_BITMAP_NEW(allvert, "SculptUndoNode.vert_hidden");
639 case SCULPT_UNDO_MASK:
640 unode->mask = MEM_mapallocN(sizeof(float) * allvert, "SculptUndoNode.mask");
641 undo_paint_push_count_alloc(UNDO_PAINT_MESH, (sizeof(float) * sizeof(int)) * allvert);
643 case SCULPT_UNDO_DYNTOPO_BEGIN:
644 case SCULPT_UNDO_DYNTOPO_END:
645 case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
646 BLI_assert(!"Dynamic topology should've already been handled");
650 BLI_addtail(lb, unode);
654 unode->maxgrid = maxgrid;
655 unode->totgrid = totgrid;
656 unode->gridsize = gridsize;
657 unode->grids = MEM_mapallocN(sizeof(int) * totgrid, "SculptUndoNode.grids");
661 unode->maxvert = ss->totvert;
662 unode->index = MEM_mapallocN(sizeof(int) * allvert, "SculptUndoNode.index");
665 if (ss->modifiers_active)
666 unode->orig_co = MEM_callocN(allvert * sizeof(*unode->orig_co), "undoSculpt orig_cos");
671 static void sculpt_undo_store_coords(Object *ob, SculptUndoNode *unode)
673 SculptSession *ss = ob->sculpt;
676 BKE_pbvh_vertex_iter_begin(ss->pbvh, unode->node, vd, PBVH_ITER_ALL)
678 copy_v3_v3(unode->co[vd.i], vd.co);
679 if (vd.no) copy_v3_v3_short(unode->no[vd.i], vd.no);
680 else normal_float_to_short_v3(unode->no[vd.i], vd.fno);
682 if (ss->modifiers_active)
683 copy_v3_v3(unode->orig_co[vd.i], ss->orig_cos[unode->index[vd.i]]);
685 BKE_pbvh_vertex_iter_end;
688 static void sculpt_undo_store_hidden(Object *ob, SculptUndoNode *unode)
690 PBVH *pbvh = ob->sculpt->pbvh;
691 PBVHNode *node = unode->node;
694 /* already stored during allocation */
698 int *vert_indices, allvert;
701 BKE_pbvh_node_num_verts(pbvh, node, NULL, &allvert);
702 BKE_pbvh_node_get_verts(pbvh, node, &vert_indices, &mvert);
703 for (i = 0; i < allvert; i++) {
704 BLI_BITMAP_MODIFY(unode->vert_hidden, i,
705 mvert[vert_indices[i]].flag & ME_HIDE);
710 static void sculpt_undo_store_mask(Object *ob, SculptUndoNode *unode)
712 SculptSession *ss = ob->sculpt;
715 BKE_pbvh_vertex_iter_begin(ss->pbvh, unode->node, vd, PBVH_ITER_ALL)
717 unode->mask[vd.i] = *vd.mask;
719 BKE_pbvh_vertex_iter_end;
722 static SculptUndoNode *sculpt_undo_bmesh_push(Object *ob,
726 ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
727 SculptUndoNode *unode = lb->first;
728 SculptSession *ss = ob->sculpt;
732 unode = MEM_callocN(sizeof(*unode), __func__);
734 BLI_strncpy(unode->idname, ob->id.name, sizeof(unode->idname));
736 unode->applied = true;
738 if (type == SCULPT_UNDO_DYNTOPO_END) {
739 unode->bm_entry = BM_log_entry_add(ss->bm_log);
740 BM_log_before_all_removed(ss->bm, ss->bm_log);
742 else if (type == SCULPT_UNDO_DYNTOPO_BEGIN) {
745 /* Store a copy of the mesh's current vertices, loops, and
746 * polys. A full copy like this is needed because entering
747 * dynamic-topology immediately does topological edits
748 * (converting polys to triangles) that the BMLog can't
749 * fully restore from */
750 CustomData_copy(&me->vdata, &unode->bm_enter_vdata, CD_MASK_MESH,
751 CD_DUPLICATE, me->totvert);
752 CustomData_copy(&me->edata, &unode->bm_enter_edata, CD_MASK_MESH,
753 CD_DUPLICATE, me->totedge);
754 CustomData_copy(&me->ldata, &unode->bm_enter_ldata, CD_MASK_MESH,
755 CD_DUPLICATE, me->totloop);
756 CustomData_copy(&me->pdata, &unode->bm_enter_pdata, CD_MASK_MESH,
757 CD_DUPLICATE, me->totpoly);
758 unode->bm_enter_totvert = me->totvert;
759 unode->bm_enter_totedge = me->totedge;
760 unode->bm_enter_totloop = me->totloop;
761 unode->bm_enter_totpoly = me->totpoly;
763 unode->bm_entry = BM_log_entry_add(ss->bm_log);
764 BM_log_all_added(ss->bm, ss->bm_log);
767 unode->bm_entry = BM_log_entry_add(ss->bm_log);
770 BLI_addtail(lb, unode);
775 case SCULPT_UNDO_COORDS:
776 case SCULPT_UNDO_MASK:
777 /* Before any vertex values get modified, ensure their
778 * original positions are logged */
779 BKE_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL) {
780 BM_log_vert_before_modified(ss->bm_log, vd.bm_vert, vd.cd_vert_mask_offset);
782 BKE_pbvh_vertex_iter_end;
785 case SCULPT_UNDO_HIDDEN:
787 GSetIterator gs_iter;
788 GSet *faces = BKE_pbvh_bmesh_node_faces(node);
789 BKE_pbvh_vertex_iter_begin(ss->pbvh, node, vd, PBVH_ITER_ALL) {
790 BM_log_vert_before_modified(ss->bm_log, vd.bm_vert, vd.cd_vert_mask_offset);
792 BKE_pbvh_vertex_iter_end;
794 GSET_ITER (gs_iter, faces) {
795 BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
796 BM_log_face_modified(ss->bm_log, f);
801 case SCULPT_UNDO_DYNTOPO_BEGIN:
802 case SCULPT_UNDO_DYNTOPO_END:
803 case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
811 SculptUndoNode *sculpt_undo_push_node(Object *ob, PBVHNode *node,
814 SculptSession *ss = ob->sculpt;
815 SculptUndoNode *unode;
817 /* list is manipulated by multiple threads, so we lock */
818 BLI_lock_thread(LOCK_CUSTOM1);
822 SCULPT_UNDO_DYNTOPO_BEGIN,
823 SCULPT_UNDO_DYNTOPO_END))
825 /* Dynamic topology stores only one undo node per stroke,
826 * regardless of the number of PBVH nodes modified */
827 unode = sculpt_undo_bmesh_push(ob, node, type);
828 BLI_unlock_thread(LOCK_CUSTOM1);
831 else if ((unode = sculpt_undo_get_node(node))) {
832 BLI_unlock_thread(LOCK_CUSTOM1);
836 unode = sculpt_undo_alloc_node(ob, node, type);
838 BLI_unlock_thread(LOCK_CUSTOM1);
840 /* copy threaded, hopefully this is the performance critical part */
844 BKE_pbvh_node_get_grids(ss->pbvh, node, &grids, &totgrid,
845 NULL, NULL, NULL, NULL);
846 memcpy(unode->grids, grids, sizeof(int) * totgrid);
849 int *vert_indices, allvert;
850 BKE_pbvh_node_num_verts(ss->pbvh, node, NULL, &allvert);
851 BKE_pbvh_node_get_verts(ss->pbvh, node, &vert_indices, NULL);
852 memcpy(unode->index, vert_indices, sizeof(int) * unode->totvert);
856 case SCULPT_UNDO_COORDS:
857 sculpt_undo_store_coords(ob, unode);
859 case SCULPT_UNDO_HIDDEN:
860 sculpt_undo_store_hidden(ob, unode);
862 case SCULPT_UNDO_MASK:
863 sculpt_undo_store_mask(ob, unode);
865 case SCULPT_UNDO_DYNTOPO_BEGIN:
866 case SCULPT_UNDO_DYNTOPO_END:
867 case SCULPT_UNDO_DYNTOPO_SYMMETRIZE:
868 BLI_assert(!"Dynamic topology should've already been handled");
872 /* store active shape key */
873 if (ss->kb) BLI_strncpy(unode->shapeName, ss->kb->name, sizeof(ss->kb->name));
874 else unode->shapeName[0] = '\0';
879 void sculpt_undo_push_begin(const char *name)
881 ED_undo_paint_push_begin(UNDO_PAINT_MESH, name,
882 sculpt_undo_restore, sculpt_undo_free);
885 void sculpt_undo_push_end(void)
887 ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
888 SculptUndoNode *unode;
890 /* we don't need normals in the undo stack */
891 for (unode = lb->first; unode; unode = unode->next) {
893 MEM_freeN(unode->no);
898 BKE_pbvh_node_layer_disp_free(unode->node);
901 ED_undo_paint_push_end(UNDO_PAINT_MESH);