system_demo_mode: minor usability improvements
[blender-addons.git] / space_view3d_brush_menus / utils_core.py
blob1bff1960b49006247225f919810620c1259107b6
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 import bpy
5 get_addon_name = 'space_view3d_brush_menus'
7 # Property Icon Width
8 PIW = ' '
11 # check for (currently) brushes being linked
12 def get_brush_link(context, types="brush"):
13 tool_settings = context.tool_settings
14 has_brush = None
16 if get_mode() == 'SCULPT':
17 datapath = tool_settings.sculpt
19 elif get_mode() == 'VERTEX_PAINT':
20 datapath = tool_settings.vertex_paint
22 elif get_mode() == 'WEIGHT_PAINT':
23 datapath = tool_settings.weight_paint
25 elif get_mode() == 'TEXTURE_PAINT':
26 datapath = tool_settings.image_paint
27 else:
28 datapath = None
30 if types == "brush":
31 has_brush = getattr(datapath, "brush", None)
33 return has_brush
36 # Addon settings
37 def 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
42 return colum_n
45 def 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)")
49 is_func = "Function" if func else "Operator"
50 print("\n[Sculpt/Paint Brush Menus]\n{}: {}\nError: {}\n".format(is_func, op_name, error))
53 # Object modes:
54 # 'OBJECT' 'EDIT' 'SCULPT'
55 # 'VERTEX_PAINT' 'WEIGHT_PAINT' 'TEXTURE_PAINT'
56 # 'PARTICLE_EDIT' 'POSE' 'GPENCIL_EDIT'
57 def get_mode():
58 return bpy.context.object.mode
60 def menuprop(item, name, value, data_path,
61 icon='NONE', disable=False, disable_icon=None,
62 custom_disable_exp=None, method=None, path=False):
64 # disable the ui
65 if disable:
66 disabled = False
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
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
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
86 # change the icon to the disable_icon if the ui has been disabled
87 if disable_icon and disabled:
88 icon = disable_icon
90 # creates the menu item
91 prop = item.operator("wm.context_set_value", text=name, icon=icon)
93 # sets what the menu item changes
94 if path:
95 prop.value = value
96 value = eval(value)
98 elif type(value) == str:
99 prop.value = "'{}'".format(value)
101 else:
102 prop.value = '{}'.format(value)
104 # sets the path to what is changed
105 prop.data_path = data_path