forked from tkeskita/BVtkNodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
showhide_properties.py
113 lines (93 loc) · 3.88 KB
/
showhide_properties.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
from .core import l # Import logging
from . import core
import bpy
from .cache import BVTKCache
class BVTK_PT_ShowHide_Properties(bpy.types.Panel):
"""BVTK Show/hide properties panel"""
bl_label = "Show/Hide Properties"
bl_space_type = "NODE_EDITOR"
bl_region_type = "UI"
bl_category = "Properties"
@classmethod
def poll(cls, context):
return (
context.active_node is not None
and context.space_data.tree_type == "BVTK_NodeTreeType"
and hasattr(context.active_node, "b_properties")
)
def draw(self, context):
layout = self.layout
active_node = context.active_node
m_properties = context.active_node.m_properties()
for i in range(len(m_properties)):
row = layout.row()
row.prop(active_node, "b_properties", index=i)
# Take property name from m_properties
mp = m_properties[i]
if mp.startswith("m_") or mp.startswith("e_"):
mp = mp[2:]
row.label(text=mp)
# Custom code editing operators
row = layout.row()
row.operator("node.bvtk_custom_code_edit", text="Edit Custom Code")
row = layout.row()
row.operator("node.bvtk_custom_code_save", text="Save Custom Code")
class BVTK_OT_Edit_Custom_Code(bpy.types.Operator):
"""Edit Custom Code text string for Active Node in Text Editor"""
bl_idname = "node.bvtk_custom_code_edit"
bl_label = "Edit Custom Code"
node_id: bpy.props.IntProperty(default=0) # Used to save node button press is from
def execute(self, context):
if self.node_id == 0: # Call from properities panel
active_node = context.active_node
else: # Call from node
# Get node based on node_id tag and make it active
active_node = BVTKCache.get_node(self.node_id)
active_node.select = True
nt = BVTKCache.get_tree(self.node_id)
nt.nodes.active = active_node
name = "BVTK"
if name not in bpy.data.texts.keys():
text = bpy.data.texts.new(name)
else:
text = bpy.data.texts[name]
text.from_string(active_node.custom_code)
flag = True
areas = context.screen.areas
for area in areas:
if area.type == "TEXT_EDITOR":
for space in area.spaces:
if space.type == "TEXT_EDITOR":
if flag:
space.text = text
space.top = 0
flag = False
self.report(
{"INFO"}, "Edit node %r " % active_node.name + "in text editor %r" % name
)
return {"FINISHED"}
class BVTK_OT_Save_Custom_Code(bpy.types.Operator):
"""Save Custom Code text from Text Editor to Active Node"""
bl_idname = "node.bvtk_custom_code_save"
bl_label = "Save Custom Code"
node_id: bpy.props.IntProperty(default=0) # Used to save node button press is from
def execute(self, context):
if self.node_id == 0: # Call from properities panel
active_node = context.active_node
else: # Call from node
# Get node based on node_id tag and make it active
active_node = BVTKCache.get_node(self.node_id)
active_node.select = True
nt = BVTKCache.get_tree(self.node_id)
nt.nodes.active = active_node
name = "BVTK"
if name not in bpy.data.texts.keys():
self.report({"ERROR"}, "No %r text found in text editor!" % name)
return {"FINISHED"}
else:
active_node.custom_code = bpy.data.texts[name].as_string()
self.report({"INFO"}, "Saved Custom Code from %r" % name)
return {"FINISHED"}
core.add_ui_class(BVTK_PT_ShowHide_Properties)
core.add_class(BVTK_OT_Edit_Custom_Code)
core.add_class(BVTK_OT_Save_Custom_Code)