1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
/* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup modifiers
*/
#include "BLI_listbase.h"
#include "BLI_string.h"
#include "MEM_guardedalloc.h"
#include "BKE_context.h"
#include "BKE_modifier.h"
#include "BKE_object.h"
#include "BKE_screen.h"
#include "DNA_object_force_types.h"
#include "DNA_object_types.h"
#include "DNA_particle_types.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "ED_object.h"
#include "BLT_translation.h"
#include "UI_interface.h"
#include "UI_resources.h"
#include "RNA_access.h"
#include "WM_api.h"
#include "WM_types.h"
#include "MOD_modifiertypes.h"
#include "MOD_ui_common.h" /* Self include */
/**
* Poll function so these modifier panels don't show for other object types with modifiers (only
* grease pencil currently).
*/
static bool modifier_ui_poll(const bContext *C, PanelType *UNUSED(pt))
{
Object *ob = ED_object_active_context(C);
return (ob != NULL) && (ob->type != OB_GPENCIL);
}
/* -------------------------------------------------------------------- */
/** \name Panel Drag and Drop, Expansion Saving
* \{ */
/**
* Move a modifier to the index it's moved to after a drag and drop.
*/
static void modifier_reorder(bContext *C, Panel *panel, int new_index)
{
PointerRNA *md_ptr = UI_panel_custom_data_get(panel);
ModifierData *md = (ModifierData *)md_ptr->data;
PointerRNA props_ptr;
wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_modifier_move_to_index", false);
WM_operator_properties_create_ptr(&props_ptr, ot);
RNA_string_set(&props_ptr, "modifier", md->name);
RNA_int_set(&props_ptr, "index", new_index);
WM_operator_name_call_ptr(C, ot, WM_OP_INVOKE_DEFAULT, &props_ptr);
WM_operator_properties_free(&props_ptr);
}
static short get_modifier_expand_flag(const bContext *UNUSED(C), Panel *panel)
{
PointerRNA *md_ptr = UI_panel_custom_data_get(panel);
ModifierData *md = (ModifierData *)md_ptr->data;
return md->ui_expand_flag;
}
static void set_modifier_expand_flag(const bContext *UNUSED(C), Panel *panel, short expand_flag)
{
PointerRNA *md_ptr = UI_panel_custom_data_get(panel);
ModifierData *md = (ModifierData *)md_ptr->data;
md->ui_expand_flag = expand_flag;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Modifier Panel Layouts
* \{ */
/**
* Draw modifier error message.
*/
void modifier_panel_end(uiLayout *layout, PointerRNA *ptr)
{
ModifierData *md = ptr->data;
if (md->error) {
uiLayout *row = uiLayoutRow(layout, false);
uiItemL(row, IFACE_(md->error), ICON_ERROR);
}
}
/**
* Gets RNA pointers for the active object and the panel's modifier data. Also locks
* the layout if the modifier is from a linked object, and sets the context pointer.
*
* \note The modifier #PointerRNA is owned by the panel so we only need a pointer to it.
*/
#define ERROR_LIBDATA_MESSAGE TIP_("External library data")
PointerRNA *modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
{
PointerRNA *ptr = UI_panel_custom_data_get(panel);
BLI_assert(!RNA_pointer_is_null(ptr));
BLI_assert(RNA_struct_is_a(ptr->type, &RNA_Modifier));
if (r_ob_ptr != NULL) {
RNA_pointer_create(ptr->owner_id, &RNA_Object, ptr->owner_id, r_ob_ptr);
}
uiBlock *block = uiLayoutGetBlock(panel->layout);
UI_block_lock_set(block, ID_IS_LINKED((Object *)ptr->owner_id), ERROR_LIBDATA_MESSAGE);
uiLayoutSetContextPointer(panel->layout, "modifier", ptr);
return ptr;
}
/**
* Helper function for modifier layouts to draw vertex group settings.
*/
void modifier_vgroup_ui(uiLayout *layout,
PointerRNA *ptr,
PointerRNA *ob_ptr,
const char *vgroup_prop,
const char *invert_vgroup_prop,
const char *text)
{
bool has_vertex_group = RNA_string_length(ptr, vgroup_prop) != 0;
uiLayout *row = uiLayoutRow(layout, true);
uiItemPointerR(row, ptr, vgroup_prop, ob_ptr, "vertex_groups", text, ICON_NONE);
if (invert_vgroup_prop != NULL) {
uiLayout *sub = uiLayoutRow(row, true);
uiLayoutSetActive(sub, has_vertex_group);
uiLayoutSetPropDecorate(sub, false);
uiItemR(sub, ptr, invert_vgroup_prop, 0, "", ICON_ARROW_LEFTRIGHT);
}
}
/**
* Check whether Modifier is a simulation or not. Used for switching to the
* physics/particles context tab.
*/
static int modifier_is_simulation(ModifierData *md)
{
/* Physic Tab */
if (ELEM(md->type,
eModifierType_Cloth,
eModifierType_Collision,
eModifierType_Fluidsim,
eModifierType_Fluid,
eModifierType_Softbody,
eModifierType_Surface,
eModifierType_DynamicPaint)) {
return 1;
}
/* Particle Tab */
if (md->type == eModifierType_ParticleSystem) {
return 2;
}
return 0;
}
static bool modifier_can_delete(ModifierData *md)
{
/* fluid particle modifier can't be deleted here */
if (md->type == eModifierType_ParticleSystem) {
short particle_type = ((ParticleSystemModifierData *)md)->psys->part->type;
if (ELEM(particle_type,
PART_FLUID,
PART_FLUID_FLIP,
PART_FLUID_FOAM,
PART_FLUID_SPRAY,
PART_FLUID_BUBBLE,
PART_FLUID_TRACER,
PART_FLUID_SPRAYFOAM,
PART_FLUID_SPRAYBUBBLE,
PART_FLUID_FOAMBUBBLE,
PART_FLUID_SPRAYFOAMBUBBLE)) {
return false;
}
}
return true;
}
static void modifier_ops_extra_draw(bContext *C, uiLayout *layout, void *md_v)
{
PointerRNA op_ptr;
uiLayout *row;
ModifierData *md = (ModifierData *)md_v;
PointerRNA ptr;
Object *ob = ED_object_active_context(C);
RNA_pointer_create(&ob->id, &RNA_Modifier, md, &ptr);
uiLayoutSetContextPointer(layout, "modifier", &ptr);
uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_DEFAULT);
uiLayoutSetUnitsX(layout, 4.0f);
/* Apply. */
uiItemO(layout,
CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Apply"),
ICON_CHECKMARK,
"OBJECT_OT_modifier_apply");
/* Apply as shapekey. */
if (BKE_modifier_is_same_topology(md) && !BKE_modifier_is_non_geometrical(md)) {
uiItemBooleanO(layout,
CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Apply as Shape Key"),
ICON_SHAPEKEY_DATA,
"OBJECT_OT_modifier_apply_as_shapekey",
"keep_modifier",
false);
uiItemBooleanO(layout,
CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Save as Shape Key"),
ICON_SHAPEKEY_DATA,
"OBJECT_OT_modifier_apply_as_shapekey",
"keep_modifier",
true);
}
/* Duplicate. */
if (!ELEM(md->type,
eModifierType_Fluidsim,
eModifierType_Softbody,
eModifierType_ParticleSystem,
eModifierType_Cloth,
eModifierType_Fluid)) {
uiItemO(layout,
CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Duplicate"),
ICON_DUPLICATE,
"OBJECT_OT_modifier_copy");
}
uiItemS(layout);
/* Move to first. */
row = uiLayoutColumn(layout, false);
uiItemFullO(row,
"OBJECT_OT_modifier_move_to_index",
IFACE_("Move to First"),
ICON_TRIA_UP,
NULL,
WM_OP_INVOKE_DEFAULT,
0,
&op_ptr);
RNA_int_set(&op_ptr, "index", 0);
if (!md->prev) {
uiLayoutSetEnabled(row, false);
}
/* Move to last. */
row = uiLayoutColumn(layout, false);
uiItemFullO(row,
"OBJECT_OT_modifier_move_to_index",
IFACE_("Move to Last"),
ICON_TRIA_DOWN,
NULL,
WM_OP_INVOKE_DEFAULT,
0,
&op_ptr);
RNA_int_set(&op_ptr, "index", BLI_listbase_count(&ob->modifiers) - 1);
if (!md->next) {
uiLayoutSetEnabled(row, false);
}
}
static void modifier_panel_header(const bContext *C, Panel *panel)
{
uiLayout *row, *sub, *name_row;
uiLayout *layout = panel->layout;
/* Don't use #modifier_panel_get_property_pointers, we don't want to lock the header. */
PointerRNA *ptr = UI_panel_custom_data_get(panel);
ModifierData *md = (ModifierData *)ptr->data;
Object *ob = (Object *)ptr->owner_id;
uiLayoutSetContextPointer(panel->layout, "modifier", ptr);
const ModifierTypeInfo *mti = BKE_modifier_get_info(md->type);
Scene *scene = CTX_data_scene(C);
int index = BLI_findindex(&ob->modifiers, md);
/* Modifier Icon. */
sub = uiLayoutRow(layout, true);
uiLayoutSetEmboss(sub, UI_EMBOSS_NONE);
if (mti->isDisabled && mti->isDisabled(scene, md, 0)) {
uiLayoutSetRedAlert(sub, true);
}
uiItemStringO(sub,
"",
RNA_struct_ui_icon(ptr->type),
"OBJECT_OT_modifier_set_active",
"modifier",
md->name);
row = uiLayoutRow(layout, true);
/* Modifier Name.
* Count how many buttons are added to the header to check if there is enough space. */
int buttons_number = 0;
name_row = uiLayoutRow(row, true);
/* Display mode switching buttons. */
if (ob->type == OB_MESH) {
int last_cage_index;
int cage_index = BKE_modifiers_get_cage_index(scene, ob, &last_cage_index, 0);
if (BKE_modifier_supports_cage(scene, md) && (index <= last_cage_index)) {
sub = uiLayoutRow(row, true);
if (index < cage_index || !BKE_modifier_couldbe_cage(scene, md)) {
uiLayoutSetActive(sub, false);
}
uiItemR(sub, ptr, "show_on_cage", 0, "", ICON_NONE);
buttons_number++;
}
} /* Tessellation point for curve-typed objects. */
else if (ELEM(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
if (mti->type != eModifierTypeType_Constructive) {
/* Constructive modifiers tessellates curve before applying. */
uiItemR(row, ptr, "use_apply_on_spline", 0, "", ICON_NONE);
buttons_number++;
}
}
/* Collision and Surface are always enabled, hide buttons. */
if (((md->type != eModifierType_Collision) || !(ob->pd && ob->pd->deflect)) &&
(md->type != eModifierType_Surface)) {
if (mti->flags & eModifierTypeFlag_SupportsEditmode) {
sub = uiLayoutRow(row, true);
uiLayoutSetActive(sub, (md->mode & eModifierMode_Realtime));
uiItemR(sub, ptr, "show_in_editmode", 0, "", ICON_NONE);
buttons_number++;
}
uiItemR(row, ptr, "show_viewport", 0, "", ICON_NONE);
uiItemR(row, ptr, "show_render", 0, "", ICON_NONE);
buttons_number += 2;
}
/* Extra operators menu. */
uiItemMenuF(row, "", ICON_DOWNARROW_HLT, modifier_ops_extra_draw, md);
/* Delete button. */
if (modifier_can_delete(md) && !modifier_is_simulation(md)) {
sub = uiLayoutRow(row, false);
uiLayoutSetEmboss(sub, UI_EMBOSS_NONE);
uiItemO(sub, "", ICON_X, "OBJECT_OT_modifier_remove");
buttons_number++;
}
/* Switch context buttons. */
if (modifier_is_simulation(md) == 1) {
uiItemStringO(
row, "", ICON_PROPERTIES, "WM_OT_properties_context_change", "context", "PHYSICS");
buttons_number++;
}
else if (modifier_is_simulation(md) == 2) {
uiItemStringO(
row, "", ICON_PROPERTIES, "WM_OT_properties_context_change", "context", "PARTICLES");
buttons_number++;
}
bool display_name = (panel->sizex / UI_UNIT_X - buttons_number > 5) || (panel->sizex == 0);
if (display_name) {
uiItemR(name_row, ptr, "name", 0, "", ICON_NONE);
}
else {
uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_RIGHT);
}
/* Extra padding for delete button. */
uiItemS(layout);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Modifier Registration Helpers
* \{ */
/**
* Create a panel in the context's region
*/
PanelType *modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
{
/* Get the name for the modifier's panel. */
char panel_idname[BKE_ST_MAXNAME];
BKE_modifier_type_panel_id(type, panel_idname);
PanelType *panel_type = MEM_callocN(sizeof(PanelType), panel_idname);
BLI_strncpy(panel_type->idname, panel_idname, BKE_ST_MAXNAME);
BLI_strncpy(panel_type->label, "", BKE_ST_MAXNAME);
BLI_strncpy(panel_type->context, "modifier", BKE_ST_MAXNAME);
BLI_strncpy(panel_type->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA, BKE_ST_MAXNAME);
BLI_strncpy(panel_type->active_property, "is_active", BKE_ST_MAXNAME);
panel_type->draw_header = modifier_panel_header;
panel_type->draw = draw;
panel_type->poll = modifier_ui_poll;
/* Give the panel the special flag that says it was built here and corresponds to a
* modifier rather than a #PanelType. */
panel_type->flag = PANEL_TYPE_HEADER_EXPAND | PANEL_TYPE_DRAW_BOX | PANEL_TYPE_INSTANCED;
panel_type->reorder = modifier_reorder;
panel_type->get_list_data_expand_flag = get_modifier_expand_flag;
panel_type->set_list_data_expand_flag = set_modifier_expand_flag;
BLI_addtail(®ion_type->paneltypes, panel_type);
return panel_type;
}
/**
* Add a child panel to the parent.
*
* \note To create the panel type's idname, it appends the \a name argument to the \a parent's
* idname.
*/
PanelType *modifier_subpanel_register(ARegionType *region_type,
const char *name,
const char *label,
PanelDrawFn draw_header,
PanelDrawFn draw,
PanelType *parent)
{
/* Create the subpanel's ID name. */
char panel_idname[BKE_ST_MAXNAME];
BLI_snprintf(panel_idname, BKE_ST_MAXNAME, "%s_%s", parent->idname, name);
PanelType *panel_type = MEM_callocN(sizeof(PanelType), panel_idname);
BLI_strncpy(panel_type->idname, panel_idname, BKE_ST_MAXNAME);
BLI_strncpy(panel_type->label, label, BKE_ST_MAXNAME);
BLI_strncpy(panel_type->context, "modifier", BKE_ST_MAXNAME);
BLI_strncpy(panel_type->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA, BKE_ST_MAXNAME);
panel_type->draw_header = draw_header;
panel_type->draw = draw;
panel_type->poll = modifier_ui_poll;
panel_type->flag = (PANEL_TYPE_DEFAULT_CLOSED | PANEL_TYPE_DRAW_BOX);
BLI_assert(parent != NULL);
BLI_strncpy(panel_type->parent_id, parent->idname, BKE_ST_MAXNAME);
panel_type->parent = parent;
BLI_addtail(&parent->children, BLI_genericNodeN(panel_type));
BLI_addtail(®ion_type->paneltypes, panel_type);
return panel_type;
}
/** \} */
|