-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBigPicNode.py
202 lines (171 loc) · 6.56 KB
/
BigPicNode.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
import bpy
import gpu
import os
from gpu_extras.batch import batch_for_shader
from bpy.types import Node
from . MindmapNodeBase import MindmapTreeNode
def draw_callback_px(self, context):
space = context.space_data
editor = space.type
if space and editor == 'NODE_EDITOR':
if space.node_tree:
nodes = space.node_tree.nodes
if self.node_name in nodes:
node = nodes[self.node_name]
pic_size = node.my_pic_size
position = node.location
filepath = node.my_pic
image_name = bpy.path.basename(filepath)
image = bpy.data.images[image_name]
texture = gpu.texture.from_image(image)
for region in context.area.regions:
if region.type == 'WINDOW':
view = region.view2d
draw_position = view.view_to_region(*position)
x1, y1 = view.region_to_view(0, 0)
x2, y2, = view.region_to_view(
region.width,
region.height
)
view_width = x2 - x1
view_height = y2 - y1
scale_x = region.width / view_width
scale_y = region.height / view_height
width = image.size[0] * pic_size * scale_x
height = image.size[1] * pic_size * scale_y
x = draw_position[0] + (node.width * scale_x)
y = draw_position[1] - height
try:
shader = gpu.shader.from_builtin('IMAGE')
except NameError:
shader = gpu.shader.from_builtin('2D_IMAGE')
batch = batch_for_shader(
shader, 'TRI_FAN',
{
"pos": (
(x, y),
(x + width, y),
(x + width, y + height),
(x, y + height)
),
"texCoord": (
(0, 0),
(1, 0),
(1, 1),
(0, 1)
),
},
)
shader.bind()
shader.uniform_sampler("image", texture)
batch.draw(shader)
def add_pic(self, context):
filepath = self.my_pic
bpy.ops.image.open(filepath=filepath)
bpy.ops.image.pack()
# BigPic Node
class BigPicNode(Node, MindmapTreeNode):
'''A Big Picture only node'''
# Optional. If not explicitly defined, the python class name is used.
bl_idname = 'BigPicNodeType'
# Label for nice name display
bl_label = "BigPic"
# Icon identifier
bl_icon = 'NONE' # 'OUTLINER_OB_FONT'
# === Custom Properties ===
my_title_prop: bpy.props.StringProperty(
name='',
default='BigPic' # noqa: F821
)
my_node_color: bpy.props.FloatVectorProperty(
name='',
default=(0.6, 0, 0),
min=0.0,
max=1.0,
subtype='COLOR', # noqa: F821
)
my_pic_size: bpy.props.FloatProperty(
name='Size', # noqa: F821
default=1.0,
soft_min=0.01,
soft_max=10,
)
default_image = os.path.join(
os.path.dirname(__file__),
"images//SVAvatar.png"
)
my_pic: bpy.props.StringProperty(
name='', # noqa: F821
default=default_image, # noqa: F821
subtype='FILE_PATH', # noqa: F821
update=add_pic
)
_handle = None
# === Optional Functions ===
# Initialization function, called when a new node is created.
def init(self, context):
self.use_custom_color = True
self.color = (0.2, 0.2, 0.2)
self.width = 20
self.hide = True
filepath = self.my_pic
bpy.ops.image.open(filepath=filepath)
bpy.ops.image.pack()
bpy.ops.node.draw_big_pic(
'INVOKE_DEFAULT',
node_name=self.name,
)
# Copy function to initialize a copied node from an existing one.
def copy(self, node):
print("Copying from node ", node)
bpy.ops.node.draw_big_pic(
'INVOKE_DEFAULT',
node_name=self.name,
)
# Free function to clean up on removal.
def free(self):
if self._handle:
bpy.context.window_manager.modal_handler_remove(self._handle)
print("Removing node ", self, ", Goodbye!")
# Additional buttons displayed on the node.
def draw_buttons(self, context, layout):
column = layout.column()
column.prop(self, "my_title_prop", icon='GREASEPENCIL')
column.prop(self, "my_pic", icon='IMAGE')
column.prop(self, "my_pic_size", icon='UV_SYNC_SELECT')
# Detail buttons in the sidebar.
# If function is not defined, the draw_buttons function is used instead
def draw_buttons_ext(self, context, layout):
column = layout.column()
column.prop(self, "my_title_prop", icon='GREASEPENCIL')
column.prop(self, "my_pic", icon='IMAGE')
column.prop(self, "my_pic_size", icon='UV_SYNC_SELECT')
def update(self):
self.my_title_prop = self.my_title_prop
class DrawBigPicOperator(bpy.types.Operator):
bl_idname = "node.draw_big_pic"
bl_label = "Draw Big Picture Operator"
node_name: bpy.props.StringProperty()
# draw_text: bpy.props.StringProperty()
def modal(self, context, event):
if context.space_data is not None:
if context.space_data.type == 'NODE_EDITOR':
context.area.tag_redraw()
return {'PASS_THROUGH'} # do not block execution
else:
return {'PASS_THROUGH'}
def invoke(self, context, event):
if context.space_data.type == 'NODE_EDITOR':
self._handle = context.space_data.draw_handler_add(
draw_callback_px,
(self, context),
'WINDOW',
'BACKDROP' # BACKDROP or POST_PIXEL
)
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
elif self._handle:
context.space_data.draw_handler_remove(self._handle, 'WINDOW')
return {'FINISHED'}
else:
return {'CANCELLED'}