1# gpl author: Ryan Inch (Imaginer)
2
3import bpy
4
5get_addon_name = 'space_view3d_brush_menus'
6
7# Property Icon Width
8PIW = '       '
9
10
11# check for (currently) brushes being linked
12def get_brush_link(context, types="brush"):
13    tool_settings = context.tool_settings
14    has_brush = None
15
16    if get_mode() == 'SCULPT':
17        datapath = tool_settings.sculpt
18
19    elif get_mode() == 'VERTEX_PAINT':
20        datapath = tool_settings.vertex_paint
21
22    elif get_mode() == 'WEIGHT_PAINT':
23        datapath = tool_settings.weight_paint
24
25    elif get_mode() == 'TEXTURE_PAINT':
26        datapath = tool_settings.image_paint
27    else:
28        datapath = None
29
30    if types == "brush":
31        has_brush = getattr(datapath, "brush", None)
32
33    return has_brush
34
35
36# Addon settings
37def addon_settings():
38    # separate function just for more convenience
39    addon = bpy.context.preferences.addons[get_addon_name]
40    colum_n = addon.preferences.column_set if addon else 1
41
42    return colum_n
43
44
45def error_handlers(self, op_name, error, reports="ERROR", func=False):
46    if self and reports:
47        self.report({'WARNING'}, reports + " (See Console for more info)")
48
49    is_func = "Function" if func else "Operator"
50    print("\n[Sculpt/Paint Brush Menus]\n{}: {}\nError: {}\n".format(is_func, op_name, error))
51
52
53# Object modes:
54# 'OBJECT' 'EDIT' 'SCULPT'
55# 'VERTEX_PAINT' 'WEIGHT_PAINT' 'TEXTURE_PAINT'
56# 'PARTICLE_EDIT' 'POSE' 'GPENCIL_EDIT'
57def get_mode():
58    return bpy.context.object.mode
59
60def menuprop(item, name, value, data_path,
61             icon='NONE', disable=False, disable_icon=None,
62             custom_disable_exp=None, method=None, path=False):
63
64    # disable the ui
65    if disable:
66        disabled = False
67
68        # used if you need a custom expression to disable the ui
69        if custom_disable_exp:
70            if custom_disable_exp[0] == custom_disable_exp[1]:
71                item.enabled = False
72                disabled = True
73
74        # check if the ui should be disabled for numbers
75        elif isinstance(eval("bpy.context.{}".format(data_path)), float):
76            if round(eval("bpy.context.{}".format(data_path)), 2) == value:
77                item.enabled = False
78                disabled = True
79
80        # check if the ui should be disabled for anything else
81        else:
82            if eval("bpy.context.{}".format(data_path)) == value:
83                item.enabled = False
84                disabled = True
85
86        # change the icon to the disable_icon if the ui has been disabled
87        if disable_icon and disabled:
88            icon = disable_icon
89
90    # creates the menu item
91    prop = item.operator("wm.context_set_value", text=name, icon=icon)
92
93    # sets what the menu item changes
94    if path:
95        prop.value = value
96        value = eval(value)
97
98    elif type(value) == str:
99        prop.value = "'{}'".format(value)
100
101    else:
102        prop.value = '{}'.format(value)
103
104    # sets the path to what is changed
105    prop.data_path = data_path
106