-
Notifications
You must be signed in to change notification settings - Fork 40
/
commentnode.py
128 lines (100 loc) · 3.95 KB
/
commentnode.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
# based on original code from
# http://blender.stackexchange.com/questions/7825/is-there-a-way-to-make-comments-in-the-node-editor
# modifications by Shane Ambler
bl_info = {
"name": "Comment Node",
"author": "CoDEmanX",
"version": (1, 2),
"blender": (2, 80, 0),
"location": "Node Editor > Add > Commenting > Comment",
"description": "Add a comment node to keep notes with a node tree.",
"warning": "",
"wiki_url": "https://github.com/sambler/myblendercontrib/blob/master/commentnode.py",
"tracker_url": "https://github.com/sambler/myblendercontrib/issues",
"category": "Node"}
import bpy
from bpy.types import Node, Operator
from bpy.props import BoolProperty, IntProperty, CollectionProperty
class NodeComment(Operator):
bl_idname = "node.comment"
bl_label = "Node Comment"
index : IntProperty(default=-1)
@classmethod
def poll(cls, context):
return (context.area.type == 'NODE_EDITOR' and
context.area.spaces[0].node_tree.nodes.active is not None and
context.area.spaces[0].node_tree.nodes.active.bl_idname == CommentNode.bl_idname)
def execute(self, context):
node = context.area.spaces[0].node_tree.nodes.active
if self.index == -1:
node.myCollProperty.add()
node.myBoolProperty = True
else:
try:
node.myCollProperty.remove(self.index)
except IndexError:
print("Invalid collection index")
return {'CANCELLED'}
return {'FINISHED'}
# Derived from the Node base type.
class CommentNode(Node):
# === Basics ===
# Description string
'''A comment node'''
# Optional identifier string. If not explicitly defined, the python class name is used.
bl_idname = 'CommentNodeType'
# Label for nice name display
bl_label = 'Comment Node'
# Icon identifier
bl_icon = 'GREASEPENCIL'
myBoolProperty : bpy.props.BoolProperty(name="Edit")
myCollProperty : bpy.props.CollectionProperty(type=bpy.types.PropertyGroup)
def init(self, context):
self.width = 300
# Copy function to initialize a copied node from an existing one.
def copy(self, node):
print("Copying from node ", node)
# Free function to clean up on removal.
def free(self):
print("Removing node ", self, ", Goodbye!")
# Additional buttons displayed on the node.
def draw_buttons(self, context, layout):
#layout.label("Node settings")
row = layout.row(align=True)
row.operator(NodeComment.bl_idname, text="Add", icon="ZOOM_IN").index = -1
row.prop(self, "myBoolProperty", text="", icon="GREASEPENCIL")
col = layout.column(align=True)
if self.myBoolProperty:
for i, line in enumerate(self.myCollProperty):
row = col.row(align=True)
row.prop(line, "name", text="")
row.operator(NodeComment.bl_idname, text="", icon="ZOOM_OUT").index = i
else:
for line in self.myCollProperty:
col.label(line.name)
import nodeitems_utils
from nodeitems_utils import NodeCategory, NodeItem
# our own base class with an appropriate poll function,
# so the categories only show up in our own tree type
class CommentNodeCategory(NodeCategory):
pass
# all categories in a list
comment_node_categories = [
# identifier, label, items list
CommentNodeCategory("COMMENTNODES", "Commenting", items=[
NodeItem(CommentNode.bl_idname, label="Comment"),
]),
]
def register():
bpy.utils.register_class(NodeComment)
bpy.utils.register_class(CommentNode)
try:
nodeitems_utils.register_node_categories("COMMENT_NODES", comment_node_categories)
except:
pass
def unregister():
nodeitems_utils.unregister_node_categories("COMMENT_NODES")
bpy.utils.unregister_class(NodeComment)
bpy.utils.unregister_class(CommentNode)
if __name__ == "__main__":
register()