Blender V5.3
object_multires_modifier.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "object_intern.hh"
10
11#include "DNA_mesh_types.h"
12#include "DNA_modifier_types.h"
13#include "DNA_object_types.h"
14#include "DNA_space_types.h"
16
17#include "BKE_attribute.hh"
18#include "BKE_context.hh"
19#include "BKE_customdata.hh"
20#include "BKE_main.hh"
21#include "BKE_multires.hh"
22#include "BKE_paint.hh"
23#include "BKE_report.hh"
24
25#include "BLI_path_utils.hh"
26#include "BLI_string_utf8.hh"
27
28#include "DEG_depsgraph.hh"
29
30#include "ED_object.hh"
31#include "ED_sculpt.hh"
32
33#include "RNA_access.hh"
34#include "RNA_define.hh"
35#include "RNA_prototypes.hh"
36
37#include "WM_api.hh"
38#include "WM_types.hh"
39
40namespace blender::ed::object {
41
42/* ------------------------------------------------------------------- */
45
47{
48 return edit_modifier_poll_generic(C, RNA_MultiresModifier, (1 << OB_MESH), true, false);
49}
50
70
80
82{
83 ot->name = "Delete Higher Levels";
84 ot->description = "Deletes the higher resolution mesh, potential loss of detail";
85 ot->idname = "OBJECT_OT_multires_higher_levels_delete";
86
87 ot->poll = multires_poll;
90
91 /* flags */
94}
95
97
98/* ------------------------------------------------------------------- */
101
104 "CATMULL_CLARK",
105 0,
106 "Catmull-Clark",
107 "Create a new level using Catmull-Clark subdivisions"},
109 "SIMPLE",
110 0,
111 "Simple",
112 "Create a new level using simple subdivisions"},
114 "LINEAR",
115 0,
116 "Linear",
117 "Create a new level using linear interpolation of the sculpted displacement"},
118 {0, nullptr, 0, nullptr, nullptr},
119};
120
122{
123 Object *object = context_active_object(C);
124 MultiresModifierData *mmd = reinterpret_cast<MultiresModifierData *>(
126
127 if (!mmd) {
128 return OPERATOR_CANCELLED;
129 }
130
132 RNA_enum_get(op->ptr, "mode"));
133 multiresModifier_subdivide(object, mmd, subdivide_mode);
134
135 Mesh *mesh = id_cast<Mesh *>(object->data);
136 if (mesh->attributes().contains(".sculpt_mask") &&
138 {
141 }
142
144
147
148 return OPERATOR_FINISHED;
149}
150
152 wmOperator *op,
153 const wmEvent * /*event*/)
154{
156 return multires_subdivide_exec(C, op);
157 }
158 return OPERATOR_CANCELLED;
159}
160
162{
163 ot->name = "Multires Subdivide";
164 ot->description = "Add a new level of subdivision";
165 ot->idname = "OBJECT_OT_multires_subdivide";
166
167 ot->poll = multires_poll;
170
171 /* flags */
174 RNA_def_enum(ot->srna,
175 "mode",
178 "Subdivision Mode",
179 "How the mesh is going to be subdivided to create a new level");
180}
181
183
184/* ------------------------------------------------------------------- */
187
189{
190 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
191 Object *ob = context_active_object(C), *secondob = nullptr;
192 MultiresModifierData *mmd = reinterpret_cast<MultiresModifierData *>(
194
195 if (!mmd) {
196 return OPERATOR_CANCELLED;
197 }
198
199 if (mmd->lvl == 0) {
200 BKE_report(op->reports, RPT_ERROR, "Reshape can work only with higher levels of subdivisions");
201 return OPERATOR_CANCELLED;
202 }
203
204 CTX_DATA_BEGIN (C, Object *, selob, selected_editable_objects) {
205 if (selob->type == OB_MESH && selob != ob) {
206 secondob = selob;
207 break;
208 }
209 }
211
212 if (!secondob) {
213 BKE_report(op->reports, RPT_ERROR, "Second selected mesh object required to copy shape from");
214 return OPERATOR_CANCELLED;
215 }
216
217 if (!multiresModifier_reshapeFromObject(depsgraph, mmd, ob, secondob)) {
218 BKE_report(op->reports, RPT_ERROR, "Objects do not have the same number of vertices");
219 return OPERATOR_CANCELLED;
220 }
221
224
225 return OPERATOR_FINISHED;
226}
227
229 wmOperator *op,
230 const wmEvent * /*event*/)
231{
233 return multires_reshape_exec(C, op);
234 }
235 return OPERATOR_CANCELLED;
236}
237
239{
240 ot->name = "Multires Reshape";
241 ot->description = "Copy vertex coordinates from other object";
242 ot->idname = "OBJECT_OT_multires_reshape";
243
244 ot->poll = multires_poll;
247
248 /* flags */
251}
252
254
255/* ------------------------------------------------------------------- */
258
260{
261 Main *bmain = CTX_data_main(C);
263 Mesh *mesh = (ob) ? id_cast<Mesh *>(ob->data) : static_cast<Mesh *>(op->customdata);
264 char filepath[FILE_MAX];
265 const bool relative = RNA_boolean_get(op->ptr, "relative_path");
266
267 if (!mesh) {
268 return OPERATOR_CANCELLED;
269 }
270
271 if (CustomData_external_test(&mesh->corner_data, CD_MDISPS)) {
272 return OPERATOR_CANCELLED;
273 }
274
275 RNA_string_get(op->ptr, "filepath", filepath);
276
277 if (relative) {
278 BLI_path_rel(filepath, BKE_main_blendfile_path(bmain));
279 }
280
281 CustomData_external_add(&mesh->corner_data, &mesh->id, CD_MDISPS, mesh->corners_num, filepath);
283 &mesh->corner_data, "", &mesh->id, CD_MASK_MESH.lmask, mesh->corners_num, 0);
284
285 return OPERATOR_FINISHED;
286}
287
289 wmOperator *op,
290 const wmEvent * /*event*/)
291{
293 Mesh *mesh = id_cast<Mesh *>(ob->data);
294 char filepath[FILE_MAX];
295
297 return OPERATOR_CANCELLED;
298 }
299
300 MultiresModifierData *mmd = reinterpret_cast<MultiresModifierData *>(
302
303 if (!mmd) {
304 return OPERATOR_CANCELLED;
305 }
306
307 if (CustomData_external_test(&mesh->corner_data, CD_MDISPS)) {
308 return OPERATOR_CANCELLED;
309 }
310
311 if (RNA_struct_property_is_set(op->ptr, "filepath")) {
312 return multires_external_save_exec(C, op);
313 }
314
315 op->customdata = mesh;
316
317 /* While a filename need not be UTF8, at this point the constructed name should be UTF8. */
318 SNPRINTF_UTF8(filepath, "//%s.btx", mesh->id.name + 2);
319 RNA_string_set(op->ptr, "filepath", filepath);
320
322
324}
325
327{
328 ot->name = "Multires Save External";
329 ot->description = "Save displacements to an external file";
330 ot->idname = "OBJECT_OT_multires_external_save";
331
332 /* XXX modifier no longer in context after file browser: `ot->poll = multires_poll;`. */
335 ot->poll = multires_poll;
336
337 /* flags */
339
343 FILE_SAVE,
348}
349
351
352/* ------------------------------------------------------------------- */
355
357{
359 Mesh *mesh = id_cast<Mesh *>(ob->data);
360
361 if (!CustomData_external_test(&mesh->corner_data, CD_MDISPS)) {
362 return OPERATOR_CANCELLED;
363 }
364
365 /* XXX don't remove. */
366 CustomData_external_remove(&mesh->corner_data, &mesh->id, CD_MDISPS, mesh->corners_num);
367
368 return OPERATOR_FINISHED;
369}
370
372{
373 ot->name = "Multires Pack External";
374 ot->description = "Pack displacements from an external file";
375 ot->idname = "OBJECT_OT_multires_external_pack";
376
377 ot->poll = multires_poll;
379
380 /* flags */
382}
383
385
386/* ------------------------------------------------------------------- */
389
391{
392 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
393 Object *object = context_active_object(C);
394 MultiresModifierData *mmd = reinterpret_cast<MultiresModifierData *>(
396
397 if (!mmd) {
398 return OPERATOR_CANCELLED;
399 }
400
401 const ApplyBaseMode mode = RNA_boolean_get(op->ptr, "apply_heuristic") ?
404
406
407 multiresModifier_base_apply(depsgraph, object, mmd, mode);
408
410
413
414 return OPERATOR_FINISHED;
415}
416
418 wmOperator *op,
419 const wmEvent * /*event*/)
420{
422 return multires_base_apply_exec(C, op);
423 }
424 return OPERATOR_CANCELLED;
425}
426
428{
429 ot->name = "Multires Apply Base";
430 ot->description = "Modify the base mesh to conform to the displaced mesh";
431 ot->idname = "OBJECT_OT_multires_base_apply";
432
433 ot->poll = multires_poll;
436
437 /* flags */
441 ot->srna,
442 "apply_heuristic",
443 true,
444 "Apply Subdivision Heuristic",
445 "Whether or not the final base mesh positions will be slightly altered to account for a new "
446 "subdivision modifier being added");
448}
449
451
452/* ------------------------------------------------------------------- */
455
457{
458 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
459 Object *object = context_active_object(C);
460 MultiresModifierData *mmd = reinterpret_cast<MultiresModifierData *>(
462
463 if (!mmd) {
464 return OPERATOR_CANCELLED;
465 }
466
467 MultiresUnsubdivideInfo info = {};
468 int new_levels = multiresModifier_rebuild_subdiv(depsgraph, object, mmd, 1, true, info);
469 if (new_levels == 0) {
470 BKE_report(op->reports, RPT_ERROR, "No valid subdivisions found to rebuild a lower level");
471 return OPERATOR_CANCELLED;
472 }
474
477
478 return OPERATOR_FINISHED;
479}
480
482 wmOperator *op,
483 const wmEvent * /*event*/)
484{
486 return multires_unsubdivide_exec(C, op);
487 }
488 return OPERATOR_CANCELLED;
489}
490
492{
493 ot->name = "Unsubdivide";
494 ot->description = "Rebuild a lower subdivision level of the current base mesh";
495 ot->idname = "OBJECT_OT_multires_unsubdivide";
496
497 ot->poll = multires_poll;
500
501 /* flags */
504}
505
507
508/* ------------------------------------------------------------------- */
511
513{
514 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
515 Object *object = context_active_object(C);
516 MultiresModifierData *mmd = reinterpret_cast<MultiresModifierData *>(
518
519 if (!mmd) {
520 return OPERATOR_CANCELLED;
521 }
522
523 MultiresUnsubdivideInfo info = {};
524 int new_levels = multiresModifier_rebuild_subdiv(depsgraph, object, mmd, INT_MAX, false, info);
525 if (new_levels == 0) {
526 BKE_report(op->reports, RPT_ERROR, "No valid subdivisions found to rebuild lower levels");
527 return OPERATOR_CANCELLED;
528 }
530
531 BKE_reportf(op->reports, RPT_INFO, "%d new levels rebuilt", new_levels);
532
535
536 return OPERATOR_FINISHED;
537}
538
540 wmOperator *op,
541 const wmEvent * /*event*/)
542{
545 }
546 return OPERATOR_CANCELLED;
547}
548
550{
551 ot->name = "Rebuild Lower Subdivisions";
552 ot->description =
553 "Rebuilds all possible subdivisions levels to generate a lower resolution base mesh";
554 ot->idname = "OBJECT_OT_multires_rebuild_subdiv";
555
556 ot->poll = multires_poll;
559
560 /* flags */
563}
564
566
567} // namespace blender::ed::object
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
CustomData interface, see also DNA_customdata_types.h.
#define FILE_MAX
#define SNPRINTF_UTF8(dst, format,...)
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define ND_MODIFIER
Definition WM_types.hh:503
#define NC_OBJECT
Definition WM_types.hh:392
#define INT_MAX
void OBJECT_OT_multires_base_apply(wmOperatorType *ot)
bool edit_modifier_invoke_properties(bContext *C, wmOperator *op)
void OBJECT_OT_multires_rebuild_subdiv(wmOperatorType *ot)
void OBJECT_OT_multires_reshape(wmOperatorType *ot)
bool iter_other(Main *bmain, Object *orig_ob, bool include_orig, bool(*callback)(Object *ob, void *callback_data), void *callback_data)
static wmOperatorStatus multires_external_save_exec(bContext *C, wmOperator *op)
static wmOperatorStatus multires_higher_levels_delete_exec(bContext *C, wmOperator *op)
void OBJECT_OT_multires_unsubdivide(wmOperatorType *ot)
bool multires_update_totlevels(Object *ob, void *totlevel_v)
static wmOperatorStatus multires_unsubdivide_invoke(bContext *C, wmOperator *op, const wmEvent *)
void OBJECT_OT_multires_subdivide(wmOperatorType *ot)
static wmOperatorStatus multires_base_apply_exec(bContext *C, wmOperator *op)
bool edit_modifier_poll_generic(bContext *C, StructRNA *rna_type, int obtype_flag, bool is_editmode_allowed, bool is_liboverride_allowed)
ModifierData * edit_modifier_property_get(wmOperator *op, Object *ob, int type)
static wmOperatorStatus multires_reshape_invoke(bContext *C, wmOperator *op, const wmEvent *)
static wmOperatorStatus multires_base_apply_invoke(bContext *C, wmOperator *op, const wmEvent *)
Object * context_active_object(const bContext *C)
void OBJECT_OT_multires_external_save(wmOperatorType *ot)
void OBJECT_OT_multires_higher_levels_delete(wmOperatorType *ot)
static wmOperatorStatus multires_reshape_exec(bContext *C, wmOperator *op)
static wmOperatorStatus multires_rebuild_subdiv_invoke(bContext *C, wmOperator *op, const wmEvent *)
static EnumPropertyItem prop_multires_subdivide_mode_type[]
static wmOperatorStatus multires_unsubdivide_exec(bContext *C, wmOperator *op)
static wmOperatorStatus multires_subdivide_invoke(bContext *C, wmOperator *op, const wmEvent *)
static wmOperatorStatus multires_subdivide_exec(bContext *C, wmOperator *op)
static wmOperatorStatus multires_external_save_invoke(bContext *C, wmOperator *op, const wmEvent *)
void OBJECT_OT_multires_external_pack(wmOperatorType *ot)
static wmOperatorStatus multires_external_pack_exec(bContext *C, wmOperator *)
static wmOperatorStatus multires_rebuild_subdiv_exec(bContext *C, wmOperator *op)
static bool multires_poll(bContext *C)
static wmOperatorStatus multires_higher_levels_delete_invoke(bContext *C, wmOperator *op, const wmEvent *)
void edit_modifier_properties(wmOperatorType *ot)
void push_multires_mesh_begin(bContext *C, const char *str)
void push_multires_mesh_end(bContext *C, const char *str)
bool CustomData_external_test(CustomData *data, eCustomDataType type)
int RNA_enum_get(PointerRNA *ptr, const char *name)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
void CustomData_external_add(CustomData *data, ID *id, eCustomDataType type, int totelem, const char *filepath)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
void multiresModifier_unsubdivide_report_if_needed(const MultiresUnsubdivideInfo &info, ReportList *reports)
void multiresModifier_subdivide(Object *object, MultiresModifierData *mmd, MultiresSubdivideModeType mode)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void WM_operator_properties_filesel(wmOperatorType *ot, const int filter, const short type, const eFileSel_Action action, const eFileSel_Flag flag, const short display, const short sort)
void multiresModifier_del_levels(MultiresModifierData *mmd, Scene *scene, Object *object, int direction)
Definition multires.cc:528
void BKE_sculpt_mask_layers_ensure(Depsgraph *depsgraph, Main *bmain, Object *ob, MultiresModifierData *mmd)
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
void multiresModifier_base_apply(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd, ApplyBaseMode mode)
bool multiresModifier_reshapeFromObject(Depsgraph *depsgraph, MultiresModifierData *mmd, Object *dst, Object *src)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
@ WM_FILESEL_RELPATH
Definition WM_api.hh:1329
@ WM_FILESEL_FILEPATH
Definition WM_api.hh:1332
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
MultiresSubdivideModeType
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void CustomData_external_remove(CustomData *data, ID *id, eCustomDataType type, int totelem)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
@ FILE_SAVE
Definition WM_api.hh:1342
Main * CTX_data_main(const bContext *C)
const CustomData_MeshMasks CD_MASK_MESH
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_INTERNAL
Definition WM_types.hh:210
std::string RNA_string_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
int multiresModifier_rebuild_subdiv(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd, int rebuild_limit, bool switch_view_to_lower_level, MultiresUnsubdivideInfo &info)
void WM_event_add_fileselect(bContext *C, wmOperator *op)
signed char int8_t
Definition BKE_text.h:76
void CustomData_external_write(CustomData *data, StringRefNull basepath, ID *id, eCustomDataMask mask, int totelem, int free)
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
struct wmOperatorType * type