-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathadd_custom_tool_shelf.py
211 lines (154 loc) · 5.71 KB
/
add_custom_tool_shelf.py
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# 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.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
# Todo:
# - make it possible to add more custom tool panels
# - list all tools in an orderly fashion
# - add icons to tools that have one
# - save tool panel so you can load it later
# - make tools callable with a hotkey as floating menu
bl_info = {
"name": "Add Custom Tool Panel",
"description": "Add tools to a custom tool shelf",
"author": "jasperge",
"version": (0, 1),
"blender": (2, 69, 0),
"location": "View 3D > Toolbar",
"wiki_url": "",
"tracker_url": "",
"category": "3D View"}
import bpy
from bpy.props import (StringProperty,
EnumProperty)
def panel_types():
basetype = bpy.types.Panel
for typename in dir(bpy.types):
btype = getattr(bpy.types, typename)
if issubclass(btype, basetype):
yield btype
def panels_by_label(label):
for ptype in panel_types():
if getattr(ptype.bl_rna, "bl_label", None) == label:
yield ptype
def create_panel(label):
class CustomToolPanel(bpy.types.Panel):
bl_label = label
bl_idname = "VIEW3D_PT_custom_tool_panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
def draw(self, context):
layout = self.layout
col = layout.column(align=True)
bpy.utils.register_class(CustomToolPanel)
def remove_panel():
bpy.utils.unregister_class(
getattr(bpy.types, 'VIEW3D_PT_custom_tool_panel'))
class WM_OT_add_panel(bpy.types.Operator):
"""Add a panel to the Tool Shelf"""
bl_description = "Add a panel to the Tool Shelf"
bl_idname = "wm.panel_add"
bl_label = "Add Custom Panel"
bl_space_type = 'VIEW_3D'
label = StringProperty(
name='Panel name',
description='The name for the custom tool panel',
default='Custom')
def execute(self, context):
existing_panels = panels_by_label(self.label)
if not list(existing_panels):
create_panel(self.label)
return {'FINISHED'}
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self, width=240)
def draw(self, context):
layout = self.layout
col = layout.column(align=True)
col.prop(self, 'label')
class WM_OT_remove_panel(bpy.types.Operator):
"""Remove a panel from the Tool Shelf"""
bl_description = "Remove a panel from the Tool Shelf"
bl_idname = "wm.panel_remove"
bl_label = "Remove Custom Panel"
bl_space_type = 'VIEW_3D'
def execute(self, context):
remove_panel()
return {'FINISHED'}
class WM_OT_add_tool(bpy.types.Operator):
"""Add a tool to the custom panel"""
bl_description = "Add a tool to the custom panel"
bl_idname = "wm.tool_add"
bl_label = "Add Tool"
bl_space_type = 'VIEW_3D'
tools = EnumProperty(
name="Tool",
items=(('mesh.primitive_monkey_add', "Monkey", "Construct a Suzanne mesh"),
('mesh.primitive_cube_add', "Cube", "Construct a cube mesh"),
),
default='mesh.primitive_monkey_add'
)
def execute(self, context):
tool_to_add = self.tools
def draw_my_buttons(self, context):
self.layout.operator(tool_to_add)
bpy.types.VIEW3D_PT_custom_tool_panel.append(draw_my_buttons)
return {'FINISHED'}
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self, width=240)
def draw(self, context):
layout = self.layout
col = layout.column(align=True)
col.prop(self, 'tools')
class WM_OT_remove_tool(bpy.types.Operator):
"""Remove a tool from the custom panel"""
bl_description = "Remove a tool from the custom panel"
bl_idname = "wm.tool_remove"
bl_label = "Remove Tool"
bl_space_type = 'VIEW3D'
def execute(self, context):
print('oeps')
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self, width=240)
def draw(self, context):
layout = self.layout
col = layout.column(align=True)
layout.operator_menu_enum("mesh.primitive_circle_add",
property="fill_type",
text="Fill type"
)
class AddCustomToolsPanel(bpy.types.Panel):
bl_label = "Add Custom Tools Panel"
bl_idname = "VIEW3D_PT_add_custom_tools_panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
def draw(self, context):
layout = self.layout
col = layout.column(align=True)
col.operator('wm.panel_add')
col.operator('wm.panel_remove')
col.separator()
col.operator('wm.tool_add')
col.operator('wm.tool_remove')
def register():
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()