-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility_presets_setup.py
196 lines (154 loc) · 5.68 KB
/
utility_presets_setup.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
import os
import bpy
import statistics
from datetime import datetime
import json
from . import utility
#########################
#### LOAD AND INSERT ####
#########################
# Read and desirialize JSON file
def ReadPresetFile(path):
jsonfile = open(path)
preset = json.load(jsonfile)
preset["path"] = path
jsonfile.close()
return preset
# Insert preset from given file
def InsertPreset(self,context,path):
node_tree = context.space_data.node_tree
preset = ReadPresetFile(path)
# create nodes and set locations
newNodeList = []
for i in range(len(preset.get("nodes"))):
newNode = node_tree.nodes.new(preset.get("nodes")[i])
newNode.location = [preset.get("x")[i],preset.get("y")[i]]
newNodeList.append(newNode)
if len(newNodeList) == 0:
return
# process settings before trying to create links (important for list type nodes)
for setting in preset.get("settings"):
setattr(newNodeList[setting[0]],setting[1],setting[2])
# create links
for link in preset.get("links"):
node_tree.links.new(newNodeList[link[0]].outputs[link[2]],newNodeList[link[1]].inputs[link[3]])
# post settings after link creation (like inheritance properties)
for setting in preset.get("postsettings"):
setattr(newNodeList[setting[0]],setting[1],setting[2])
return
# Get setup presets both built-in and custom
def PresetDefinitions():
addonPrefs = bpy.context.preferences.addons[__package__].preferences
dir_custom = addonPrefs.customSetupPresets
dir_addon = os.path.join(os.path.dirname(os.path.realpath(__file__)),"setuppresets")
# gather built-in files
files = []
for item in os.listdir(dir_addon):
path = os.path.join(dir_addon,item)
if os.path.isfile(path):
if os.path.splitext(path)[1] == ".json":
files.append(path)
# gather external files
if os.path.isdir(dir_custom):
for item in os.listdir(dir_custom):
path = os.path.join(dir_custom,item)
if os.path.isfile(path):
if os.path.splitext(path)[1] == ".json":
files.append(path)
# read files
returnpresets = []
for file in files:
returnpresets.append(ReadPresetFile(file))
return returnpresets
# Load presets into UI list data
def ReloadPresets():
RawSetups = PresetDefinitions()
bpy.context.scene.MCFG_SP_PresetList.clear()
for setup in RawSetups:
newItem = bpy.context.scene.MCFG_SP_PresetList.add()
newItem.name = setup.get("name")
newItem.desc = setup.get("desc")
newItem.custom = setup.get("custom")
newItem.path = setup.get("path")
return
# Delete preset file
def DeletePreset(path):
os.remove(path)
ReloadPresets()
##################
#### Generate ####
##################
# Generate preset in dictionary format
def FormatPreset(context):
nodeTree = context.space_data.node_tree
# starting values
name = context.scene.MCFG_SP_PresetName
desc = context.scene.MCFG_SP_PresetDesc
nodes = []
x = []
y = []
settings = []
links = []
postsettings = []
if name == "":
utility.ShowInfoBox("Cannot create preset without name","Error",'ERROR')
return ""
# populate nodes, x, y and settings
for node in nodeTree.nodes:
nodes.append(node.bl_idname)
x.append(node.location[0])
y.append(node.location[1])
nodesettings = node.presetsettings()
if len(nodesettings) != 0:
for setting in nodesettings:
settings.append([len(nodes) -1] + setting)
nodesettingspost = node.presetpostsettings()
if len(nodesettingspost) != 0:
for setting in nodesettingspost:
postsettings.append([len(nodes) -1] + setting)
# populate links
for link in nodeTree.links:
nodeout = nodeTree.nodes.values().index(link.from_node)
nodein = nodeTree.nodes.values().index(link.to_node)
socketout = link.from_node.outputs.values().index(link.from_socket)
socketin = link.to_node.inputs.values().index(link.to_socket)
links.append([nodeout,nodein,socketout,socketin])
# make the average location 0,0
meanX = statistics.mean(x)
meanY = statistics.mean(y)
for i in range(len(nodes)):
x[i] = int(round(x[i] - meanX,-1))
y[i] = int(round(y[i] - meanY,-1))
# creating dictionary
preset = dict()
preset["custom"] = True
preset["name"] = name
preset["desc"] = desc
preset["nodes"] = nodes
preset["x"] = x
preset["y"] = y
preset["settings"] = settings
preset["links"] = links
preset["postsettings"] = postsettings
return preset
# Generate preset file name
def FormatPresetFileName():
name = "setuppreset"
stamp = datetime.now().strftime("%Y%m%d%H%M%S")
return (name + "_" + stamp)
# Create setup preset from node tree
def CreatePreset(self,context):
addonPrefs = bpy.context.preferences.addons[__package__].preferences
folderpath = addonPrefs.customSetupPresets
filepath = os.path.join(folderpath,FormatPresetFileName() + ".json")
# file name conflict handling
if os.path.isfile(filepath) and not addonPrefs.customSetupPresetsReplace:
utility.ShowInfoBox("File name conflict occured","Error",'ERROR')
return
# creating preset dictionary
newSetup = FormatPreset(context)
if newSetup == "":
return
outputfile = open(filepath,"w")
outputfile.write(json.dumps(newSetup,indent = 4))
outputfile.close()