Welcome to mirror list, hosted at ThFree Co, Russian Federation.

properties_grease_pencil_common.py « bl_ui « startup « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 38522a1bf8444280d95d9e6a03b26ebd56ecb7cd (plain)
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
# SPDX-License-Identifier: GPL-2.0-or-later

import bpy
from bpy.types import Menu, UIList, Operator
from bpy.app.translations import pgettext_iface as iface_


# XXX: To be replaced with active tools
# Currently only used by the clip editor
class AnnotationDrawingToolsPanel:
    # subclass must set
    # bl_space_type = 'IMAGE_EDITOR'
    bl_label = "Annotation"
    bl_category = "Annotation"
    bl_region_type = 'TOOLS'

    def draw(self, context):
        layout = self.layout

        tool_settings = context.tool_settings

        col = layout.column(align=True)

        col.label(text="Draw:")
        row = col.row(align=True)
        row.operator("gpencil.annotate", icon='GREASEPENCIL', text="Draw").mode = 'DRAW'
        # XXX: Needs a dedicated icon
        row.operator("gpencil.annotate", icon='FORCE_CURVE', text="Erase").mode = 'ERASER'

        row = col.row(align=True)
        row.operator("gpencil.annotate", icon='LINE_DATA', text="Line").mode = 'DRAW_STRAIGHT'
        row.operator("gpencil.annotate", icon='MESH_DATA', text="Poly").mode = 'DRAW_POLY'

        col.separator()

        col.label(text="Stroke Placement:")
        row = col.row(align=True)
        row.prop_enum(tool_settings, "annotation_stroke_placement_view2d", 'VIEW')
        row.prop_enum(tool_settings, "annotation_stroke_placement_view2d", 'IMAGE', text="Image")


class GreasePencilSculptAdvancedPanel:

    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True
        layout.use_property_decorate = False

        tool_settings = context.scene.tool_settings
        brush = tool_settings.gpencil_sculpt_paint.brush
        tool = brush.gpencil_sculpt_tool
        gp_settings = brush.gpencil_settings

        col = layout.column(heading="Auto-Masking", align=True)
        col.prop(gp_settings, "use_automasking_stroke", text="Stroke")
        col.prop(gp_settings, "use_automasking_layer", text="Layer")
        col.prop(gp_settings, "use_automasking_material", text="Material")

        if tool in {'SMOOTH', 'RANDOMIZE'}:
            col = layout.column(heading="Affect", align=True)
            col.prop(gp_settings, "use_edit_position", text="Position")
            col.prop(gp_settings, "use_edit_strength", text="Strength")
            col.prop(gp_settings, "use_edit_thickness", text="Thickness")
            col.prop(gp_settings, "use_edit_uv", text="UV")


# GP Object Tool Settings
class GreasePencilDisplayPanel:
    bl_label = "Brush Tip"
    bl_options = {'DEFAULT_CLOSED'}

    @classmethod
    def poll(cls, context):
        ob = context.active_object
        brush = context.tool_settings.gpencil_paint.brush
        if ob and ob.type == 'GPENCIL' and brush:
            if context.mode == 'PAINT_GPENCIL':
                return brush.gpencil_tool != 'ERASE'
            else:
                # GP Sculpt, Vertex and Weight Paint always have Brush Tip panel.
                return True
        return False

    def draw_header(self, context):
        if self.is_popover:
            return

        tool_settings = context.tool_settings
        if context.mode == 'PAINT_GPENCIL':
            settings = tool_settings.gpencil_paint
        elif context.mode == 'SCULPT_GPENCIL':
            settings = tool_settings.gpencil_sculpt_paint
        elif context.mode == 'WEIGHT_GPENCIL':
            settings = tool_settings.gpencil_weight_paint
        elif context.mode == 'VERTEX_GPENCIL':
            settings = tool_settings.gpencil_vertex_paint
        brush = settings.brush
        if brush:
            self.layout.prop(settings, "show_brush", text="")

    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True
        layout.use_property_decorate = False

        tool_settings = context.tool_settings
        if context.mode == 'PAINT_GPENCIL':
            settings = tool_settings.gpencil_paint
        elif context.mode == 'SCULPT_GPENCIL':
            settings = tool_settings.gpencil_sculpt_paint
        elif context.mode == 'WEIGHT_GPENCIL':
            settings = tool_settings.gpencil_weight_paint
        elif context.mode == 'VERTEX_GPENCIL':
            settings = tool_settings.gpencil_vertex_paint
        brush = settings.brush
        gp_settings = brush.gpencil_settings

        ob = context.active_object
        if ob.mode == 'PAINT_GPENCIL':

            if self.is_popover:
                row = layout.row(align=True)
                row.prop(settings, "show_brush", text="Display Cursor")

            col = layout.column(align=True)
            col.active = settings.show_brush

            if brush.gpencil_tool == 'DRAW':
                col.prop(gp_settings, "show_lasso", text="Show Fill Color While Drawing")

        elif ob.mode == 'SCULPT_GPENCIL':
            col = layout.column(align=True)
            col.active = settings.show_brush

            col.prop(brush, "cursor_color_add", text="Cursor Color")
            if brush.gpencil_sculpt_tool in {'THICKNESS', 'STRENGTH', 'PINCH', 'TWIST'}:
                col.prop(brush, "cursor_color_subtract", text="Inverse Color")

        elif ob.mode == 'WEIGHT_GPENCIL':
            col = layout.column(align=True)
            col.active = settings.show_brush

            col.prop(brush, "cursor_color_add", text="Cursor Color")

        elif ob.mode == 'VERTEX_GPENCIL':
            row = layout.row(align=True)
            row.prop(settings, "show_brush", text="")
            row.label(text="Display Cursor")


class GreasePencilBrushFalloff:
    bl_label = "Falloff"
    bl_options = {'DEFAULT_CLOSED'}

    def draw(self, context):
        layout = self.layout
        ts = context.tool_settings
        settings = None
        if context.mode == 'PAINT_GPENCIL':
            settings = ts.gpencil_paint
        if context.mode == 'SCULPT_GPENCIL':
            settings = ts.gpencil_sculpt_paint
        elif context.mode == 'WEIGHT_GPENCIL':
            settings = ts.gpencil_weight_paint
        elif context.mode == 'VERTEX_GPENCIL':
            settings = ts.gpencil_vertex_paint

        if settings:
            brush = settings.brush

            col = layout.column(align=True)
            row = col.row(align=True)
            row.prop(brush, "curve_preset", text="")

            if brush.curve_preset == 'CUSTOM':
                layout.template_curve_mapping(brush, "curve", brush=True)

                col = layout.column(align=True)
                row = col.row(align=True)
                row.operator("brush.curve_preset", icon='SMOOTHCURVE', text="").shape = 'SMOOTH'
                row.operator("brush.curve_preset", icon='SPHERECURVE', text="").shape = 'ROUND'
                row.operator("brush.curve_preset", icon='ROOTCURVE', text="").shape = 'ROOT'
                row.operator("brush.curve_preset", icon='SHARPCURVE', text="").shape = 'SHARP'
                row.operator("brush.curve_preset", icon='LINCURVE', text="").shape = 'LINE'
                row.operator("brush.curve_preset", icon='NOCURVE', text="").shape = 'MAX'


class GPENCIL_MT_snap(Menu):
    bl_label = "Snap"

    def draw(self, _context):
        layout = self.layout

        layout.operator("gpencil.snap_to_grid", text="Selection to Grid")
        layout.operator("gpencil.snap_to_cursor", text="Selection to Cursor").use_offset = False
        layout.operator("gpencil.snap_to_cursor", text="Selection to Cursor (Keep Offset)").use_offset = True

        layout.separator()

        layout.operator("gpencil.snap_cursor_to_selected", text="Cursor to Selected")
        layout.operator("view3d.snap_cursor_to_center", text="Cursor to World Origin")
        layout.operator("view3d.snap_cursor_to_grid", text="Cursor to Grid")


class GPENCIL_MT_snap_pie(Menu):
    bl_label = "Snap"

    def draw(self, _context):
        layout = self.layout
        pie = layout.menu_pie()

        pie.operator("view3d.snap_cursor_to_grid", text="Cursor to Grid", icon='CURSOR')
        pie.operator("gpencil.snap_to_grid", text="Selection to Grid", icon='RESTRICT_SELECT_OFF')
        pie.operator("gpencil.snap_cursor_to_selected", text="Cursor to Selected", icon='CURSOR')
        pie.operator(
            "gpencil.snap_to_cursor",
            text="Selection to Cursor",
            icon='RESTRICT_SELECT_OFF'
        ).use_offset = False
        pie.operator(
            "gpencil.snap_to_cursor",
            text="Selection to Cursor (Keep Offset)",
            icon='RESTRICT_SELECT_OFF'
        ).use_offset = True
        pie.separator()
        pie.operator("view3d.snap_cursor_to_center", text="Cursor to World Origin", icon='CURSOR')
        pie.separator()


class GPENCIL_MT_move_to_layer(Menu):
    bl_label = "Move to Layer"

    def draw(self, context):
        layout = self.layout
        gpd = context.gpencil_data
        if gpd:
            layout.operator_context = 'INVOKE_REGION_WIN'
            layout.operator("gpencil.move_to_layer", text="New Layer", icon='ADD').layer = -1

            layout.separator()

            gpl_active = context.active_gpencil_layer
            tot_layers = len(gpd.layers)
            i = tot_layers - 1
            while i >= 0:
                gpl = gpd.layers[i]
                if gpl.info == gpl_active.info:
                    icon = 'GREASEPENCIL'
                else:
                    icon = 'NONE'
                layout.operator("gpencil.move_to_layer", text=gpl.info, icon=icon, translate=False).layer = i
                i -= 1


class GPENCIL_MT_layer_active(Menu):
    bl_label = "Change Active Layer"

    def draw(self, context):
        layout = self.layout
        layout.operator_context = 'INVOKE_REGION_WIN'

        gpd = context.gpencil_data
        if gpd:
            layout.operator("gpencil.layer_add", text="New Layer", icon='ADD')

            layout.separator()

            gpl_active = context.active_gpencil_layer
            tot_layers = len(gpd.layers)
            i = tot_layers - 1
            while i >= 0:
                gpl = gpd.layers[i]
                if gpl.info == gpl_active.info:
                    icon = 'GREASEPENCIL'
                else:
                    icon = 'NONE'
                layout.operator("gpencil.layer_active", text=gpl.info, icon=icon).layer = i
                i -= 1


class GPENCIL_MT_material_active(Menu):
    bl_label = "Change Active Material"

    @classmethod
    def poll(cls, context):
        ob = context.active_object
        if ob is None or len(ob.material_slots) == 0:
            return False

        return True

    def draw(self, context):
        layout = self.layout
        layout.operator_context = 'INVOKE_REGION_WIN'
        ob = context.active_object

        for slot in ob.material_slots:
            mat = slot.material
            if mat and mat.id_data and mat.id_data.preview:
                icon = mat.id_data.preview.icon_id
                layout.operator("gpencil.material_set", text=mat.name, icon_value=icon).slot = mat.name


class GPENCIL_MT_gpencil_draw_delete(Menu):
    bl_label = "Delete"

    def draw(self, _context):
        layout = self.layout

        layout.operator_context = 'INVOKE_REGION_WIN'

        layout.operator("gpencil.delete", text="Delete Active Keyframe (Active Layer)").type = 'FRAME'
        layout.operator("gpencil.active_frames_delete_all", text="Delete Active Keyframes (All Layers)")


class GPENCIL_MT_cleanup(Menu):
    bl_label = "Clean Up"

    def draw(self, context):

        ob = context.active_object

        layout = self.layout

        layout.operator("gpencil.frame_clean_fill", text="Boundary Strokes").mode = 'ACTIVE'
        layout.operator("gpencil.frame_clean_fill", text="Boundary Strokes all Frames").mode = 'ALL'

        layout.separator()

        layout.operator("gpencil.frame_clean_loose", text="Delete Loose Points")

        if ob.mode != 'PAINT_GPENCIL':
            layout.operator("gpencil.stroke_merge_by_distance", text="Merge by Distance")

        layout.separator()

        layout.operator("gpencil.frame_clean_duplicate", text="Delete Duplicated Frames")
        layout.operator("gpencil.recalc_geometry", text="Recalculate Geometry")
        if ob.mode != 'PAINT_GPENCIL':
            layout.operator("gpencil.reproject")


class GPENCIL_UL_annotation_layer(UIList):
    def draw_item(self, _context, layout, _data, item, icon, _active_data, _active_propname, _index):
        # assert(isinstance(item, bpy.types.GPencilLayer)
        gpl = item

        if self.layout_type in {'DEFAULT', 'COMPACT'}:
            if gpl.lock:
                layout.active = False

            split = layout.split(factor=0.2)
            split.prop(gpl, "color", text="", emboss=True)
            split.prop(gpl, "info", text="", emboss=False)

            row = layout.row(align=True)

            row.prop(gpl, "show_in_front", text="", icon='XRAY' if gpl.show_in_front else 'FACESEL', emboss=False)

            row.prop(gpl, "annotation_hide", text="", emboss=False)
        elif self.layout_type == 'GRID':
            layout.alignment = 'CENTER'
            layout.label(text="", icon_value=icon)


class AnnotationDataPanel:
    bl_label = "Annotations"
    bl_region_type = 'UI'
    bl_options = {'DEFAULT_CLOSED'}

    def draw_header(self, context):
        if context.space_data.type not in {'VIEW_3D', 'TOPBAR', 'SEQUENCE_EDITOR'}:
            self.layout.prop(context.space_data, "show_annotation", text="")

    def draw(self, context):
        layout = self.layout
        layout.use_property_decorate = False

        is_clip_editor = context.space_data.type == 'CLIP_EDITOR'

        # Grease Pencil owner.
        gpd_owner = context.annotation_data_owner
        gpd = context.annotation_data

        # Owner selector.
        if is_clip_editor:
            col = layout.column()
            col.label(text="Data Source:")
            row = col.row()
            row.prop(context.space_data, "annotation_source", expand=True)

        # Only allow adding annotation ID if its owner exist
        if context.annotation_data_owner is None:
            row = layout.row()
            row.active = False
            row.label(text="No annotation source")
            return

        row = layout.row()
        row.template_ID(gpd_owner, "grease_pencil", new="gpencil.annotation_add", unlink="gpencil.data_unlink")

        # List of layers/notes.
        if gpd and gpd.layers:
            self.draw_layers(context, layout, gpd)

    def draw_layers(self, context, layout, gpd):
        row = layout.row()

        col = row.column()
        if len(gpd.layers) >= 2:
            layer_rows = 5
        else:
            layer_rows = 3
        col.template_list("GPENCIL_UL_annotation_layer", "", gpd, "layers", gpd.layers, "active_index",
                          rows=layer_rows, sort_reverse=True, sort_lock=True)

        col = row.column()

        sub = col.column(align=True)
        sub.operator("gpencil.layer_annotation_add", icon='ADD', text="")
        sub.operator("gpencil.layer_annotation_remove", icon='REMOVE', text="")

        gpl = context.active_annotation_layer
        if gpl:
            if len(gpd.layers) > 1:
                col.separator()

                sub = col.column(align=True)
                sub.operator("gpencil.layer_annotation_move", icon='TRIA_UP', text="").type = 'UP'
                sub.operator("gpencil.layer_annotation_move", icon='TRIA_DOWN', text="").type = 'DOWN'

        tool_settings = context.tool_settings
        if gpd and gpl:
            layout.prop(gpl, "annotation_opacity", text="Opacity", slider=True)
            layout.prop(gpl, "thickness")
        else:
            layout.prop(tool_settings, "annotation_thickness", text="Thickness")

        if gpl:
            # Full-Row - Frame Locking (and Delete Frame)
            row = layout.row(align=True)
            row.active = not gpl.lock

            if gpl.active_frame:
                lock_status = iface_("Locked") if gpl.lock_frame else iface_("Unlocked")
                lock_label = iface_("Frame: %d (%s)") % (gpl.active_frame.frame_number, lock_status)
            else:
                lock_label = iface_("Lock Frame")
            row.prop(gpl, "lock_frame", text=lock_label, icon='UNLOCKED')
            row.operator("gpencil.annotation_active_frame_delete", text="", icon='X')


class AnnotationOnionSkin:
    bl_label = "Onion Skin"
    bl_region_type = 'UI'
    bl_options = {'DEFAULT_CLOSED'}

    @classmethod
    def poll(cls, context):
        # Show this panel as long as someone that might own this exists
        # AND the owner isn't an object (e.g. GP Object)
        if context.annotation_data_owner is None:
            return False
        elif type(context.annotation_data_owner) is bpy.types.Object:
            return False
        else:
            gpl = context.active_annotation_layer
            if gpl is None:
                return False

        return True

    def draw_header(self, context):
        gpl = context.active_annotation_layer
        self.layout.prop(gpl, "use_annotation_onion_skinning", text="")

    def draw(self, context):
        layout = self.layout
        layout.use_property_decorate = False

        gpl = context.active_annotation_layer
        col = layout.column()
        split = col.split(factor=0.5)
        split.active = gpl.use_annotation_onion_skinning

        # - Before Frames
        sub = split.column(align=True)
        row = sub.row(align=True)
        row.prop(gpl, "annotation_onion_before_color", text="")
        sub.prop(gpl, "annotation_onion_before_range", text="Before")

        # - After Frames
        sub = split.column(align=True)
        row = sub.row(align=True)
        row.prop(gpl, "annotation_onion_after_color", text="")
        sub.prop(gpl, "annotation_onion_after_range", text="After")


class GreasePencilMaterialsPanel:
    # Mix-in, use for properties editor and top-bar.
    def draw(self, context):
        layout = self.layout
        show_full_ui = (self.bl_space_type == 'PROPERTIES')

        is_view3d = (self.bl_space_type == 'VIEW_3D')
        tool_settings = context.scene.tool_settings
        gpencil_paint = tool_settings.gpencil_paint
        brush = gpencil_paint.brush if gpencil_paint else None

        ob = context.object
        row = layout.row()

        if ob:
            is_sortable = len(ob.material_slots) > 1
            rows = 7

            row.template_list("GPENCIL_UL_matslots", "", ob, "material_slots", ob, "active_material_index", rows=rows)

            # if topbar popover and brush pinned, disable
            if is_view3d and brush is not None:
                gp_settings = brush.gpencil_settings
                if gp_settings.use_material_pin:
                    row.enabled = False

            col = row.column(align=True)
            if show_full_ui:
                col.operator("object.material_slot_add", icon='ADD', text="")
                col.operator("object.material_slot_remove", icon='REMOVE', text="")

            col.separator()

            col.menu("GPENCIL_MT_material_context_menu", icon='DOWNARROW_HLT', text="")

            if is_sortable:
                col.separator()

                col.operator("object.material_slot_move", icon='TRIA_UP', text="").direction = 'UP'
                col.operator("object.material_slot_move", icon='TRIA_DOWN', text="").direction = 'DOWN'

                col.separator()

                sub = col.column(align=True)
                sub.operator("gpencil.material_isolate", icon='RESTRICT_VIEW_ON', text="").affect_visibility = True
                sub.operator("gpencil.material_isolate", icon='LOCKED', text="").affect_visibility = False

            if show_full_ui:
                row = layout.row()

                row.template_ID(ob, "active_material", new="material.new", live_icon=True)

                slot = context.material_slot
                if slot:
                    icon_link = 'MESH_DATA' if slot.link == 'DATA' else 'OBJECT_DATA'
                    row.prop(slot, "link", icon=icon_link, icon_only=True)

                if ob.data.use_stroke_edit_mode:
                    row = layout.row(align=True)
                    row.operator("gpencil.stroke_change_color", text="Assign")
                    row.operator("gpencil.material_select", text="Select").deselect = False
                    row.operator("gpencil.material_select", text="Deselect").deselect = True
        # stroke color
            ma = None
            if is_view3d and brush is not None:
                gp_settings = brush.gpencil_settings
                if gp_settings.use_material_pin is False:
                    if len(ob.material_slots) > 0 and ob.active_material_index >= 0:
                        ma = ob.material_slots[ob.active_material_index].material
                else:
                    ma = gp_settings.material
            else:
                if len(ob.material_slots) > 0 and ob.active_material_index >= 0:
                    ma = ob.material_slots[ob.active_material_index].material

            if is_view3d and ma is not None and ma.grease_pencil is not None:
                gpcolor = ma.grease_pencil
                if gpcolor.stroke_style == 'SOLID':
                    row = layout.row()
                    row.prop(gpcolor, "color", text="Stroke Color")

        else:
            space = context.space_data
            row.template_ID(space, "pin_id")


class GreasePencilVertexcolorPanel:

    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True
        layout.use_property_decorate = False

        ts = context.scene.tool_settings
        is_vertex = context.mode == 'VERTEX_GPENCIL'
        gpencil_paint = ts.gpencil_vertex_paint if is_vertex else ts.gpencil_paint
        brush = gpencil_paint.brush
        gp_settings = brush.gpencil_settings
        tool = brush.gpencil_vertex_tool if is_vertex else brush.gpencil_tool

        ob = context.object

        if ob:
            col = layout.column()
            col.template_color_picker(brush, "color", value_slider=True)

            sub_row = layout.row(align=True)
            sub_row.prop(brush, "color", text="")
            sub_row.prop(brush, "secondary_color", text="")

            sub_row.operator("gpencil.tint_flip", icon='FILE_REFRESH', text="")

            row = layout.row(align=True)
            row.template_ID(gpencil_paint, "palette", new="palette.new")
            if gpencil_paint.palette:
                layout.template_palette(gpencil_paint, "palette", color=True)

            if tool in {'DRAW', 'FILL'} and is_vertex is False:
                row = layout.row(align=True)
                row.prop(gp_settings, "vertex_mode", text="Mode")
                row = layout.row(align=True)
                row.prop(gp_settings, "vertex_color_factor", slider=True, text="Mix Factor")


class GPENCIL_UL_layer(UIList):
    def draw_item(self, _context, layout, _data, item, icon, _active_data, _active_propname, _index):
        # assert(isinstance(item, bpy.types.GPencilLayer)
        gpl = item

        if self.layout_type in {'DEFAULT', 'COMPACT'}:
            if gpl.lock:
                layout.active = False

            row = layout.row(align=True)
            row.label(
                text="",
                icon='BONE_DATA' if gpl.is_parented else 'BLANK1',
            )
            row.prop(gpl, "info", text="", emboss=False)

            row = layout.row(align=True)

            icon_mask = 'MOD_MASK' if gpl.use_mask_layer else 'LAYER_ACTIVE'

            row.prop(gpl, "use_mask_layer", text="", icon=icon_mask, emboss=False)

            subrow = row.row(align=True)
            subrow.prop(
                gpl,
                "use_onion_skinning",
                text="",
                icon='ONIONSKIN_ON' if gpl.use_onion_skinning else 'ONIONSKIN_OFF',
                emboss=False,
            )
            row.prop(gpl, "hide", text="", emboss=False)
            row.prop(gpl, "lock", text="", emboss=False)
        elif self.layout_type == 'GRID':
            layout.alignment = 'CENTER'
            layout.label(
                text="",
                icon_value=icon,
            )


class GreasePencilSimplifyPanel:

    def draw_header(self, context):
        rd = context.scene.render
        self.layout.prop(rd, "simplify_gpencil", text="")

    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True
        layout.use_property_decorate = False

        rd = context.scene.render

        layout.active = rd.use_simplify and rd.simplify_gpencil

        col = layout.column()
        col.prop(rd, "simplify_gpencil_onplay")
        col.prop(rd, "simplify_gpencil_view_fill")
        col.prop(rd, "simplify_gpencil_modifier")
        col.prop(rd, "simplify_gpencil_shader_fx")
        col.prop(rd, "simplify_gpencil_tint")
        col.prop(rd, "simplify_gpencil_antialiasing")


class GreasePencilLayerTransformPanel:

    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True

        ob = context.object
        gpd = ob.data
        gpl = gpd.layers.active
        layout.active = not gpl.lock

        row = layout.row(align=True)
        row.prop(gpl, "location")

        row = layout.row(align=True)
        row.prop(gpl, "rotation")

        row = layout.row(align=True)
        row.prop(gpl, "scale")


class GreasePencilLayerAdjustmentsPanel:

    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True

        ob = context.object
        gpd = ob.data
        gpl = gpd.layers.active
        layout.active = not gpl.lock

        # Layer options
        # Offsets - Color Tint
        layout.enabled = not gpl.lock
        col = layout.column(align=True)
        col.prop(gpl, "tint_color")
        col.prop(gpl, "tint_factor", text="Factor", slider=True)

        # Offsets - Thickness
        col = layout.row(align=True)
        col.prop(gpl, "line_change", text="Stroke Thickness")


class GPENCIL_UL_masks(UIList):
    def draw_item(self, _context, layout, _data, item, icon, _active_data, _active_propname, _index):
        mask = item
        if self.layout_type in {'DEFAULT', 'COMPACT'}:
            row = layout.row(align=True)
            row.prop(mask, "name", text="", emboss=False, icon_value=icon)
            row.prop(mask, "invert", text="", emboss=False)
            row.prop(mask, "hide", text="", emboss=False)
        elif self.layout_type == 'GRID':
            layout.alignment = 'CENTER'
            layout.prop(mask, "name", text="", emboss=False, icon_value=icon)


class GPENCIL_MT_layer_mask_menu(Menu):
    bl_label = "Layer Specials"

    def draw(self, context):
        layout = self.layout
        ob = context.object
        gpd = ob.data
        gpl_active = gpd.layers.active
        done = False
        for gpl in gpd.layers:
            if gpl != gpl_active and gpl.info not in gpl_active.mask_layers:
                done = True
                layout.operator("gpencil.layer_mask_add", text=gpl.info).name = gpl.info

        if done is False:
            layout.label(text="No layers to add")


class GreasePencilLayerMasksPanel:
    def draw_header(self, context):
        ob = context.active_object
        gpd = ob.data
        gpl = gpd.layers.active

        self.layout.prop(gpl, "use_mask_layer", text="")

    def draw(self, context):
        ob = context.active_object
        gpd = ob.data
        gpl = gpd.layers.active

        layout = self.layout
        layout.enabled = gpl.use_mask_layer

        if gpl:
            rows = 4
            row = layout.row()
            col = row.column()
            col.template_list("GPENCIL_UL_masks", "", gpl, "mask_layers", gpl.mask_layers,
                              "active_mask_index", rows=rows, sort_lock=True)

            col2 = row.column(align=True)
            col2.menu("GPENCIL_MT_layer_mask_menu", icon='ADD', text="")
            col2.operator("gpencil.layer_mask_remove", icon='REMOVE', text="")

            col2.separator()

            sub = col2.column(align=True)
            sub.operator("gpencil.layer_mask_move", icon='TRIA_UP', text="").type = 'UP'
            sub.operator("gpencil.layer_mask_move", icon='TRIA_DOWN', text="").type = 'DOWN'


class GreasePencilLayerRelationsPanel:

    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True
        layout.use_property_decorate = False

        scene = context.scene
        ob = context.object
        gpd = ob.data
        gpl = gpd.layers.active

        col = layout.column()
        col.active = not gpl.lock
        col.prop(gpl, "parent")
        col.prop(gpl, "parent_type", text="Type")
        parent = gpl.parent

        if parent and gpl.parent_type == 'BONE' and parent.type == 'ARMATURE':
            col.prop_search(gpl, "parent_bone", parent.data, "bones", text="Bone")

        layout.separator()

        col = layout.row(align=True)
        col.prop(gpl, "pass_index")

        col = layout.row(align=True)
        col.prop_search(gpl, "viewlayer_render", scene, "view_layers", text="View Layer")

        col = layout.row(align=True)
        # Only enable this property when a view layer is selected.
        col.enabled = bool(gpl.viewlayer_render)
        col.prop(gpl, "use_viewlayer_masks")


class GreasePencilLayerDisplayPanel:

    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True
        layout.use_property_decorate = False

        ob = context.object
        gpd = ob.data
        gpl = gpd.layers.active

        use_colors = context.preferences.edit.use_anim_channel_group_colors

        col = layout.column(align=True)
        col.active = use_colors
        row = col.row(align=True)
        row.prop(gpl, "channel_color")
        if not use_colors:
            col.label(text="Channel Colors are disabled in Animation preferences")

        row = layout.row(align=True)
        row.prop(gpl, "use_solo_mode", text="Show Only on Keyframed")


class GreasePencilFlipTintColors(Operator):
    bl_label = "Flip Colors"
    bl_idname = "gpencil.tint_flip"
    bl_description = "Switch tint colors"

    @classmethod
    def poll(cls, context):
        ts = context.tool_settings
        settings = None
        if context.mode == 'PAINT_GPENCIL':
            settings = ts.gpencil_paint
        if context.mode == 'SCULPT_GPENCIL':
            settings = ts.gpencil_sculpt_paint
        elif context.mode == 'WEIGHT_GPENCIL':
            settings = ts.gpencil_weight_paint
        elif context.mode == 'VERTEX_GPENCIL':
            settings = ts.gpencil_vertex_paint

        return settings and settings.brush

    def execute(self, context):
        ts = context.tool_settings
        settings = None
        if context.mode == 'PAINT_GPENCIL':
            settings = ts.gpencil_paint
        if context.mode == 'SCULPT_GPENCIL':
            settings = ts.gpencil_sculpt_paint
        elif context.mode == 'WEIGHT_GPENCIL':
            settings = ts.gpencil_weight_paint
        elif context.mode == 'VERTEX_GPENCIL':
            settings = ts.gpencil_vertex_paint

        brush = settings.brush
        color = brush.color
        secondary_color = brush.secondary_color

        orig_prim = color.hsv
        orig_sec = secondary_color.hsv

        color.hsv = orig_sec
        secondary_color.hsv = orig_prim
        return {'FINISHED'}


classes = (
    GPENCIL_MT_snap,
    GPENCIL_MT_snap_pie,
    GPENCIL_MT_cleanup,
    GPENCIL_MT_move_to_layer,
    GPENCIL_MT_layer_active,
    GPENCIL_MT_material_active,

    GPENCIL_MT_gpencil_draw_delete,
    GPENCIL_MT_layer_mask_menu,

    GPENCIL_UL_annotation_layer,
    GPENCIL_UL_layer,
    GPENCIL_UL_masks,

    GreasePencilFlipTintColors,
)

if __name__ == "__main__":  # only for live edit.
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)