Blender V5.3
sculpt_dyntopo.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8#include "sculpt_dyntopo.hh"
9
10#include <cstdlib>
11
12#include "BLT_translation.hh"
13
14#include "DNA_modifier_types.h"
15
16#include "BKE_context.hh"
17#include "BKE_global.hh"
18#include "BKE_mesh.hh"
19#include "BKE_modifier.hh"
20#include "BKE_object.hh"
21#include "BKE_object_types.hh"
22#include "BKE_paint.hh"
23#include "BKE_paint_bvh.hh"
24#include "BKE_particle.h"
25#include "BKE_pointcache.h"
26#include "BKE_scene.hh"
27
28#include "DEG_depsgraph.hh"
29
30#include "WM_api.hh"
31#include "WM_types.hh"
32
33#include "ED_undo.hh"
34
35#include "sculpt_intern.hh"
36#include "sculpt_undo.hh"
37
38#include "UI_interface.hh"
40#include "UI_resources.hh"
41
42#include "bmesh.hh"
43#include "bmesh_tools.hh"
44
46
48{
49 if (bm->totloop != bm->totface * 3) {
53 4,
54 false,
55 nullptr,
56 nullptr,
57 nullptr);
58 }
59}
60
61void enable_ex(Main &bmain, Depsgraph &depsgraph, Object &ob)
62{
64 Mesh *mesh = id_cast<Mesh *>(ob.data);
66
69
70 /* Dynamic topology doesn't ensure selection state is valid, so remove #36280. */
72
73 /* Create triangles-only BMesh. */
74 BMeshCreateParams create_params{};
75 create_params.use_toolflags = false;
76 ss.bm = BM_mesh_create(&allocsize, &create_params);
77
78 BMeshFromMeshParams convert_params{};
79 convert_params.calc_face_normal = true;
80 convert_params.calc_vert_normal = true;
81 convert_params.use_shapekey = true;
82 convert_params.active_shapekey = ob.shapenr;
83 BM_mesh_bm_from_me(ss.bm, mesh, &convert_params);
84 triangulate(ss.bm);
85
86 BM_data_layer_ensure_named(ss.bm, &ss.bm->vdata, CD_PROP_FLOAT, ".sculpt_mask");
87
88 /* Make sure the data for existing faces are initialized. */
89 if (mesh->faces_num != ss.bm->totface) {
91 }
92
93 /* Enable dynamic topology. */
95
96 /* Enable logging for undo/redo. */
97 ss.bm_log = BM_log_create(ss.bm);
98
99 /* Update dependency graph, so modifiers that depend on dyntopo being enabled
100 * are re-evaluated and the #bke::pbvh::Tree is re-created. */
102 BKE_scene_graph_update_tagged(&depsgraph, &bmain);
103}
104
111static void disable(
112 Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob, undo::StepData *undo_step)
113{
115 Mesh *mesh = id_cast<Mesh *>(ob.data);
116
117 if (BMesh *bm = ss.bm) {
118 BM_data_layer_free_named(bm, &bm->vdata, ".sculpt_dyntopo_node_id_vertex");
119 BM_data_layer_free_named(bm, &bm->pdata, ".sculpt_dyntopo_node_id_face");
120 }
121
123
124 if (undo_step) {
126 }
127 else {
129 }
130
131 /* Clear data. */
133
134 /* Typically valid but with global-undo they can be nullptr, see: #36234. */
135 if (ss.bm) {
136 BM_mesh_free(ss.bm);
137 ss.bm = nullptr;
138 }
139 if (ss.bm_log) {
141 ss.bm_log = nullptr;
142 }
143
146
147 /* Update dependency graph, so modifiers that depend on dyntopo being enabled
148 * are re-evaluated and the #bke::pbvh::Tree is re-created. */
150 BKE_scene_graph_update_tagged(&depsgraph, &bmain);
151}
152
154{
155 Main *bmain = CTX_data_main(C);
156 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
157 Scene *scene = CTX_data_scene(C);
159 disable(*bmain, *depsgraph, *scene, *ob, undo_step);
160}
161
162void disable_with_undo(Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob)
163{
164 /* This is an unlikely situation to happen in normal usage, though with application handlers
165 * it is possible that a user is attempting to exit the current object mode. See #146398 */
167 /* May be false in background mode. */
168 const bool use_undo = G.background ? (ED_undo_stack_get() != nullptr) : true;
169 if (use_undo) {
170 undo::push_begin_ex(scene, ob, "Dynamic topology disable");
171 undo::push_node(depsgraph, ob, nullptr, undo::Type::DyntopoEnd);
172 }
173 disable(bmain, depsgraph, scene, ob, nullptr);
174 if (use_undo) {
175 undo::push_end_ex(ob, true);
176 }
177 }
178}
179
180static void enable_with_undo(Main &bmain, Depsgraph &depsgraph, const Scene &scene, Object &ob)
181{
183 if (ss.bm == nullptr) {
184 /* May be false in background mode. */
185 const bool use_undo = G.background ? (ED_undo_stack_get() != nullptr) : true;
186 if (use_undo) {
187 undo::push_begin_ex(scene, ob, "Dynamic topology enable");
188 }
189 enable_ex(bmain, depsgraph, ob);
190 if (use_undo) {
191 undo::push_node(depsgraph, ob, nullptr, undo::Type::DyntopoBegin);
192 undo::push_end_ex(ob, true);
193 }
194 }
195}
196
198{
199 Main &bmain = *CTX_data_main(C);
200 Depsgraph &depsgraph = *CTX_data_ensure_evaluated_depsgraph(C);
201 Scene &scene = *CTX_data_scene(C);
204
205 WM_cursor_wait(true);
206
207 if (ss.bm) {
208 disable_with_undo(bmain, depsgraph, scene, ob);
209 }
210 else {
211 enable_with_undo(bmain, depsgraph, scene, ob);
212 }
213
214 WM_cursor_wait(false);
216
217 return OPERATOR_FINISHED;
218}
219
221{
222 if (iter.data_type == bke::AttrType::Float && iter.name == ".sculpt_mask") {
223 return true;
224 }
226}
227
229{
230 Mesh *mesh = id_cast<Mesh *>(ob.data);
232
234
235 BLI_assert(ss.bm == nullptr);
237
238 const bke::AttributeAccessor attributes = mesh->attributes();
239 attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
240 if (!dyntopo_supports_layer(iter)) {
241 flag |= ATTRIBUTES;
242 }
243 });
244
245 {
246 VirtualModifierData virtual_modifier_data;
247 ModifierData *md = BKE_modifiers_get_virtual_modifierlist(&ob, &virtual_modifier_data);
248
249 /* Exception for shape keys because we can edit those. */
250 for (; md; md = md->next) {
253 continue;
254 }
255
257 flag |= MODIFIER;
258 break;
259 }
260 }
261 }
262
263 return flag;
264}
265
267 wmOperator *op,
268 const wmEvent * /*event*/)
269{
272
273 if (!ss.bm) {
274 Scene &scene = *CTX_data_scene(C);
275 const WarnFlag flag = check_attribute_warning(scene, ob);
276
277 if (flag & ATTRIBUTES) {
280 "Dyntopo will not preserve face sets, colors, UVs, or other attributes");
281 }
282
283 if (flag & MODIFIER) {
285 C,
286 op,
287 RPT_("Generative Modifiers Detected!"),
288 RPT_("Keeping the modifiers will increase polycount when returning to object mode"),
289 IFACE_("Enable"),
291 false);
292 }
293 }
294
296}
297
299{
300 /* Identifiers. */
301 ot->name = "Dynamic Topology Toggle";
302 ot->idname = "SCULPT_OT_dynamic_topology_toggle";
303 ot->description = "Dynamic topology alters the mesh topology while sculpting";
304
305 /* API callbacks. */
309
310 ot->flag = OPTYPE_REGISTER;
311}
312
313} // namespace blender::ed::sculpt_paint::dyntopo
General operations, lookup, etc. for blender objects.
A BVH for high poly meshes.
#define PTCACHE_RESET_OUTDATED
#define RPT_(msgid)
#define IFACE_(msgid)
#define C
Definition RandGen.cpp:29
#define NC_SCENE
Definition WM_types.hh:391
#define ND_TOOLSETTINGS
Definition WM_types.hh:468
#define UNUSED_VARS_NDEBUG(...)
#define BMALLOC_TEMPLATE_FROM_ME(...)
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
void enable_ex(Main &bmain, Depsgraph &depsgraph, Object &ob)
static bool dyntopo_supports_layer(const bke::AttributeIter &iter)
void disable_with_undo(Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob)
static void disable(Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob, undo::StepData *undo_step)
WarnFlag check_attribute_warning(Scene &scene, Object &ob)
static wmOperatorStatus sculpt_dynamic_topology_toggle_exec(bContext *C, wmOperator *)
void SCULPT_OT_dynamic_topology_toggle(wmOperatorType *ot)
static wmOperatorStatus sculpt_dynamic_topology_toggle_invoke(bContext *C, wmOperator *op, const wmEvent *)
static void enable_with_undo(Main &bmain, Depsgraph &depsgraph, const Scene &scene, Object &ob)
void push_begin_ex(const Scene &, Object &ob, const char *name)
void push_node(const Depsgraph &depsgraph, const Object &object, const bke::pbvh::Node *node, const Type type)
void restore_from_bmesh_enter_geometry(const StepData &step_data, Mesh &mesh)
void push_end_ex(Object &ob, const bool use_nested_undo)
bool sculpt_mode_poll(bContext *C)
Definition sculpt.cc:3800
void WM_main_add_notifier(uint type, void *reference)
bool BM_data_layer_free_named(BMesh *bm, CustomData *data, StringRef name)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ RPT_WARNING
Definition BKE_report.hh:40
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
int BKE_ptcache_object_reset(struct Scene *scene, struct Object *ob, int mode)
UndoStack * ED_undo_stack_get()
Definition ed_undo.cc:690
void BKE_mesh_mselect_clear(Mesh *mesh)
void BM_log_free(BMLog *log)
Definition bmesh_log.cc:501
void BKE_sculptsession_bm_to_me(Object *ob)
void BKE_sculptsession_free_pbvh(Object &object)
Global G
Definition blender.cc:57
void BM_mesh_bm_from_me(BMesh *bm, const Mesh *mesh, const BMeshFromMeshParams *params)
Mesh -> BMesh.
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
ModifierData * BKE_modifiers_get_virtual_modifierlist(const Object *ob, VirtualModifierData *data)
void BKE_scene_graph_update_tagged(Depsgraph *depsgraph, Main *bmain)
Definition scene.cc:2867
BMLog * BM_log_create(BMesh *bm)
Definition bmesh_log.cc:396
void BKE_particlesystem_reset_all(struct Object *object)
wmOperatorStatus WM_operator_confirm_ex(bContext *C, wmOperator *op, const char *title, const char *message, const char *confirm_text, ui::AlertIcon icon, bool cancel_default)
@ ME_SCULPT_DYNAMIC_TOPOLOGY
void BKE_sculptsession_free_deformMats(SculptSession *ss)
void BM_data_layer_ensure_named(BMesh *bm, CustomData *data, int type, const StringRef name)
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
void BM_mesh_triangulate(BMesh *bm, const int quad_method, const int ngon_method, const int min_vertices, const bool tag_only, BMOperator *op, BMOpSlot *slot_facemap_out, BMOpSlot *slot_facemap_double_out)
bool BKE_modifier_is_enabled(const Scene *scene, ModifierData *md, int required_mode)
void WM_cursor_wait(bool val)
BMesh * BM_mesh_create(const BMAllocTemplate *allocsize, const BMeshCreateParams *params)
BMesh Make Mesh.
Object * CTX_data_active_object(const bContext *C)
const ModifierTypeInfo * BKE_modifier_get_info(ModifierType type)
bool BM_attribute_stored_in_bmesh_builtin(const StringRef name)
void BM_mesh_normals_update(BMesh *bm)
CustomData pdata
CustomData vdata
struct ModifierData * next
bke::ObjectRuntime * runtime
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports