forked from tkeskita/BVtkNodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
favorites.py
80 lines (66 loc) · 2.43 KB
/
favorites.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
from .core import *
from . import favorites_data
# -----------------------------------------------------------------------------
# Favorites panel
# -----------------------------------------------------------------------------
# Favorites must be an array of (node.bl_idname, node.bl_label) tuples
favorites = favorites_data.favorites
favorites_file = favorites_data.__file__
class BVTK_PT_Favorites(bpy.types.Panel):
"""BVTK Favorites Panel"""
bl_label = "Favorites"
bl_space_type = "NODE_EDITOR"
bl_region_type = "UI"
bl_category = "Favorites"
@classmethod
def poll(cls, context):
return context.space_data.tree_type == "BVTK_NodeTreeType"
def draw(self, context):
global favorites
active_node = context.active_node
layout = self.layout
# Button to add active node to favorites
if active_node:
add = layout.operator(
"node.bvtk_update_favorites", icon="ZOOM_IN", text=active_node.bl_label
)
add.label = active_node.bl_label
add.type = active_node.bl_idname
layout.separator()
# Favorites buttons
for f in favorites:
row = layout.row(align=True)
remove = row.operator(
"node.bvtk_update_favorites", icon="PANEL_CLOSE", text=""
)
remove.label = f[1]
remove.type = f[0]
remove.remove = True
op = row.operator("node.add_node", text=f[1])
op.type = f[0]
op.use_transform = True
class BVTK_OT_UpdateFavorites(bpy.types.Operator):
"""Update favorites operator"""
bl_idname = "node.bvtk_update_favorites"
bl_label = "add/remove favorites"
remove: bpy.props.BoolProperty(default=False)
label: bpy.props.StringProperty()
type: bpy.props.StringProperty()
def execute(self, context):
global favorites
global favorites_file
fav = (self.type, self.label)
if self.remove:
favorites.remove(fav)
else:
if fav in favorites:
self.report({"INFO"}, "Already in favorites")
return {"FINISHED"}
favorites.append(fav)
open(favorites_file, "w").write(
"favorites = " + repr(favorites).replace("),", "),\n")
)
self.remove = False
return {"FINISHED"}
add_ui_class(BVTK_PT_Favorites)
add_ui_class(BVTK_OT_UpdateFavorites)