Blender V5.3
grease_pencil_material.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "BKE_attribute.hh"
12#include "BKE_context.hh"
13#include "BKE_curves.hh"
14#include "BKE_grease_pencil.hh"
15#include "BKE_material.hh"
16
17#include "BLI_vector.hh"
18
19#include "DEG_depsgraph.hh"
20
21#include "ED_grease_pencil.hh"
22
23#include "RNA_access.hh"
24#include "RNA_define.hh"
25
26#include "WM_api.hh"
27
28namespace blender {
29
30namespace ed::greasepencil {
31
32/* -------------------------------------------------------------------- */
35
37{
39 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
40
41 bool changed = false;
42 for (const int i : IndexRange(object->totcol)) {
43 if (Material *ma = BKE_gpencil_material(object, i + 1)) {
44 MaterialGPencilStyle &gp_style = *ma->gp_style;
45 gp_style.flag &= ~GP_MATERIAL_HIDE;
47 changed = true;
48 }
49 }
50
51 if (changed) {
54 }
55
56 return OPERATOR_FINISHED;
57}
58
60{
61 /* Identifiers. */
62 ot->name = "Show All Materials";
63 ot->idname = "GREASE_PENCIL_OT_material_reveal";
64 ot->description = "Unhide all hidden Grease Pencil materials";
65
66 /* Callbacks. */
69
71}
72
74
75/* -------------------------------------------------------------------- */
78
80{
82 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
83 const bool invert = RNA_boolean_get(op->ptr, "invert");
84
85 bool changed = false;
86 const int material_index = object->actcol - 1;
87
88 for (const int i : IndexRange(object->totcol)) {
89 if (invert && i == material_index) {
90 continue;
91 }
92 if (!invert && i != material_index) {
93 continue;
94 }
95 if (Material *ma = BKE_object_material_get(object, i + 1)) {
96 MaterialGPencilStyle &gp_style = *ma->gp_style;
97 gp_style.flag |= GP_MATERIAL_HIDE;
99 changed = true;
100 }
101 }
102
103 if (changed) {
106 }
107
108 return OPERATOR_FINISHED;
109}
110
112{
113 /* Identifiers. */
114 ot->name = "Hide Materials";
115 ot->idname = "GREASE_PENCIL_OT_material_hide";
116 ot->description = "Hide active/inactive Grease Pencil material(s)";
117
118 /* Callbacks. */
121
123
124 /* props */
126 ot->srna, "invert", false, "Invert", "Hide inactive materials instead of the active one");
127}
128
130
131/* -------------------------------------------------------------------- */
134
136{
138 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
139
140 bool changed = false;
141 for (const int i : IndexRange(object->totcol)) {
142 if (Material *ma = BKE_object_material_get(object, i + 1)) {
143 MaterialGPencilStyle &gp_style = *ma->gp_style;
144 gp_style.flag |= GP_MATERIAL_LOCKED;
146 changed = true;
147 }
148 }
149
150 if (changed) {
153 }
154
155 return OPERATOR_FINISHED;
156}
157
159{
160 /* Identifiers. */
161 ot->name = "Lock All Materials";
162 ot->idname = "GREASE_PENCIL_OT_material_lock_all";
163 ot->description =
164 "Lock all Grease Pencil materials to prevent them from being accidentally modified";
165
166 /* Callbacks. */
169
171}
172
174
175/* -------------------------------------------------------------------- */
178
180{
182 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
183
184 bool changed = false;
185 for (const int i : IndexRange(object->totcol)) {
186 if (Material *ma = BKE_object_material_get(object, i + 1)) {
187 MaterialGPencilStyle &gp_style = *ma->gp_style;
188 gp_style.flag &= ~GP_MATERIAL_LOCKED;
190 changed = true;
191 }
192 }
193
194 if (changed) {
197 }
198
199 return OPERATOR_FINISHED;
200}
201
203{
204 /* Identifiers. */
205 ot->name = "Unlock All Materials";
206 ot->idname = "GREASE_PENCIL_OT_material_unlock_all";
207 ot->description = "Unlock all Grease Pencil materials so that they can be edited";
208
209 /* Callbacks. */
212
214}
215
217
218/* -------------------------------------------------------------------- */
221
223{
225 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
226
227 bool changed = false;
228 for (const int material_index : IndexRange(object->totcol)) {
229 if (!BKE_object_material_slot_used(object, material_index + 1)) {
230 if (Material *ma = BKE_object_material_get(object, material_index + 1)) {
231 MaterialGPencilStyle &gp_style = *ma->gp_style;
234 changed = true;
235 }
236 }
237 }
238 if (changed) {
241 }
242
243 return OPERATOR_FINISHED;
244}
245
247{
248 /* Identifiers. */
249 ot->name = "Lock Unused Materials";
250 ot->idname = "GREASE_PENCIL_OT_material_lock_unused";
251 ot->description = "Lock and hide any material not used";
252
253 /* Callbacks. */
256
258}
259
261
262/* -------------------------------------------------------------------- */
265
267 wmOperator * /*op*/)
268{
269 using namespace blender::bke;
270
271 const Scene *scene = CTX_data_scene(C);
273 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object->data);
274
275 bool changed = false;
277
278 Set<int> materials_used;
279
280 for (const MutableDrawingInfo &info : drawings) {
281 IndexMaskMemory memory;
283 *object, info.drawing, info.layer_index, memory);
284 if (strokes.is_empty()) {
285 return OPERATOR_CANCELLED;
286 }
287
288 AttributeAccessor attributes = info.drawing.strokes().attributes();
289 const VArray<int> material_indices = *attributes.lookup_or_default<int>(
290 "material_index", AttrDomain::Curve, 0);
291
292 if (const std::optional<int> single = material_indices.get_if_single()) {
293 materials_used.add(*single);
294 }
295 else {
296 strokes.foreach_index([&](const int i) { materials_used.add(material_indices[i]); });
297 }
298 };
299
300 /* The material lock must be done outside of the drawing loop to prevent
301 * 'retrieve_editable_and_selected_strokes' from returning an incorrect IndexMask.
302 */
303 for (const int i : IndexRange(object->totcol)) {
304 if (!materials_used.contains(i)) {
305 if (Material *ma = BKE_object_material_get(object, i + 1)) {
306 MaterialGPencilStyle &gp_style = *ma->gp_style;
307 gp_style.flag |= GP_MATERIAL_LOCKED;
309 changed = true;
310 }
311 }
312 }
313
314 if (changed) {
317 }
318
319 return OPERATOR_FINISHED;
320}
321
323{
324 /* Identifiers. */
325 ot->name = "Lock Unselected Materials";
326 ot->idname = "GREASE_PENCIL_OT_material_lock_unselected";
327 ot->description = "Lock any material not used in any selected stroke";
328
329 /* Callbacks. */
332
334}
335
337
338/* -------------------------------------------------------------------- */
341
343{
344 using namespace blender::bke;
345
346 Main *bmain = CTX_data_main(C);
347 const bool only_active = RNA_boolean_get(op->ptr, "only_active");
349 Material *ma_active = BKE_object_material_get(ob_src, ob_src->actcol);
350 if (ma_active == nullptr) {
351 return OPERATOR_CANCELLED;
352 }
353
354 CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
355 if ((ob == ob_src) || (ob->type != OB_GREASE_PENCIL)) {
356 continue;
357 }
358 /* Duplicate materials. */
359 for (const int i : IndexRange(ob_src->totcol)) {
360 Material *ma_src = BKE_object_material_get(ob_src, i + 1);
361 if (only_active && ma_src != ma_active) {
362 continue;
363 }
364
365 if (ma_src == nullptr) {
366 continue;
367 }
368
369 BKE_object_material_ensure(bmain, ob, ma_src);
370 }
371
373 }
375
377
378 return OPERATOR_FINISHED;
379}
380
382{
383 PropertyRNA *prop;
384
385 /* Identifiers. */
386 ot->name = "Copy Materials to Selected Object";
387 ot->idname = "GREASE_PENCIL_OT_material_copy_to_object";
388 ot->description = "Append Materials of the active Grease Pencil to other object";
389
390 /* Callbacks. */
393
395
396 prop = RNA_def_boolean(ot->srna,
397 "only_active",
398 true,
399 "Only Active",
400 "Append only active material, uncheck to append all materials");
401
403}
404
406{
408 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(ob->data);
409 Material *active_ma = BKE_gpencil_material(ob, ob->actcol);
411 MaterialGPencilStyle *gp_style;
412
414 bool isolate = false;
415
416 if (RNA_boolean_get(op->ptr, "affect_visibility")) {
417 flags |= GP_MATERIAL_HIDE;
418 }
419
420 if (active_material == nullptr) {
421 return OPERATOR_CANCELLED;
422 }
423
424 /* Test whether to isolate or clear all flags */
425 Material *ma = nullptr;
426 short *totcol = BKE_object_material_len_p(ob);
427 for (short i = 0; i < *totcol; i++) {
428 ma = BKE_gpencil_material(ob, i + 1);
429 /* Skip if this is the active one */
430 if (ELEM(ma, nullptr, active_ma)) {
431 continue;
432 }
433
434 /* If the flags aren't set, that means that the color is
435 * not alone, so we have some colors to isolate still
436 */
437 gp_style = ma->gp_style;
438 if ((gp_style->flag & flags) == 0) {
439 isolate = true;
440 break;
441 }
442 }
443
444 /* Set/Clear flags as appropriate */
445 if (isolate) {
446 /* Set flags on all "other" colors */
447 for (short i = 0; i < *totcol; i++) {
448 ma = BKE_gpencil_material(ob, i + 1);
449 if (ma == nullptr) {
450 continue;
451 }
452 gp_style = ma->gp_style;
453 if (gp_style == active_material) {
454 continue;
455 }
456 gp_style->flag |= flags;
458 }
459 }
460 else {
461 /* Clear flags - Restore everything else */
462 for (short i = 0; i < *totcol; i++) {
463 ma = BKE_gpencil_material(ob, i + 1);
464 if (ma == nullptr) {
465 continue;
466 }
467 gp_style = ma->gp_style;
468 gp_style->flag &= ~flags;
470 }
471 }
472
475
476 return OPERATOR_FINISHED;
477}
478
480{
481 /* identifiers */
482 ot->name = "Isolate Material";
483 ot->idname = "GREASE_PENCIL_OT_material_isolate";
484 ot->description =
485 "Toggle whether the active material is the only one that is editable and/or visible";
486
487 /* callbacks */
490
491 /* flags */
493
494 /* properties */
496 "affect_visibility",
497 false,
498 "Affect Visibility",
499 "In addition to toggling "
500 "the editability, also affect the visibility");
501}
502
504
505} // namespace ed::greasepencil
506
519
520} // namespace blender
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
Low-level operations for curves.
Low-level operations for grease pencil.
General operations, lookup, etc. for materials.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define NA_EDITED
Definition WM_types.hh:634
#define NC_GPENCIL
Definition WM_types.hh:412
#define ELEM(...)
constexpr IndexRange()=default
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
std::optional< T > get_if_single() const
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
float2x2 invert(float2x2 mat)
bool active_grease_pencil_poll(bContext *C)
IndexMask retrieve_editable_and_selected_strokes(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
static void GREASE_PENCIL_OT_material_lock_unused(wmOperatorType *ot)
static void GREASE_PENCIL_OT_material_copy_to_object(wmOperatorType *ot)
static void GREASE_PENCIL_OT_material_unlock_all(wmOperatorType *ot)
static void GREASE_PENCIL_OT_material_lock_unselected(wmOperatorType *ot)
static void GREASE_PENCIL_OT_material_isolate(wmOperatorType *ot)
static void GREASE_PENCIL_OT_material_lock_all(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_material_lock_unused_exec(bContext *C, wmOperator *)
static wmOperatorStatus material_isolate_exec(bContext *C, wmOperator *op)
static void GREASE_PENCIL_OT_material_hide(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_material_unlock_all_exec(bContext *C, wmOperator *)
static wmOperatorStatus grease_pencil_material_reveal_exec(bContext *C, wmOperator *)
static wmOperatorStatus grease_pencil_material_lock_unselected_exec(bContext *C, wmOperator *)
static wmOperatorStatus grease_pencil_material_copy_to_object_exec(bContext *C, wmOperator *op)
static wmOperatorStatus grease_pencil_material_lock_all_exec(bContext *C, wmOperator *)
bool active_grease_pencil_material_poll(bContext *C)
Vector< MutableDrawingInfo > retrieve_editable_drawings(const Scene &scene, GreasePencil &grease_pencil)
static void GREASE_PENCIL_OT_material_reveal(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_material_hide_exec(bContext *C, wmOperator *op)
BMesh Iterators.
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
Material * BKE_gpencil_material(Object *ob, short act)
short * BKE_object_material_len_p(Object *ob)
Material * BKE_object_material_get(Object *ob, short act)
int BKE_object_material_ensure(Main *bmain, Object *ob, Material *material)
MaterialGPencilStyle * BKE_gpencil_material_settings(Object *ob, short act)
bool BKE_object_material_slot_used(Object *object, short actcol)
void DEG_id_tag_update(ID *id, unsigned int flags)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
Main * CTX_data_main(const bContext *C)
void ED_operatortypes_grease_pencil_material()
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ 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
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
Object * CTX_data_active_object(const bContext *C)
eMaterialGPencilStyle_Flag flag
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