-
Notifications
You must be signed in to change notification settings - Fork 1
/
directory.py
executable file
·224 lines (198 loc) · 6.32 KB
/
directory.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python
import sys, os
sys.path.append("..")
import cqparts_bucket
from cqparts_bucket import *
#from experimental.zignig import *
import cqparts.search as cs
import cqparts
from cqparts.display import display
import json
import zipfile
from anytree import Node, RenderTree, NodeMixin, PreOrderIter
from anytree.search import findall, find_by_attr, findall_by_attr
from anytree.resolver import Resolver
import views
import render
import db
import importer
from thing import thing
class Directory:
def __init__(self, base, name, prefix="cache", export="model"):
self.name = name
self.d = cs.index.copy()
self.res = Resolver("name")
self.base = base
self.class_dict = {}
self.k = {}
self.export_path = export
self.export_prefix = prefix
self.root = thing(base)
self.build_tree(name, self.root)
self.build_tree("showcase", self.root)
self.build_other()
self.alter_build(name)
self.store = db.Store(prefix=prefix)
def alter_build(self,name):
reg = cs.index.copy()
s = reg[name]
keys = s.keys()
for i in keys:
sub = s[i].copy()
print(i,sub)
def build_other(self):
p = thing("lib", parent=self.root)
k = list(self.d.keys())
for i in k:
self.build_tree(i, p)
def build_tree(self, name, root):
print(name,root)
if name not in self.d:
return
p = thing(name, parent=root)
tr = self.d.pop(name)
for j in tr:
b = thing(j, parent=p)
for k in tr[j]:
cn = type(k()).__module__ + "." + k.__name__
t = thing(k.__name__, parent=b, c=k, classname=cn, doc=k.__doc__)
self.class_dict[cn] = t
self.k[p.get_path() + "/" + j + "/" + k.__name__] = t
def get_path(self, path):
r = self.res.get(self.root, path)
return r
def get_name(self, name):
n = findall_by_attr(self.root, name)
return n[0]
def exists(self, key):
if "/" + key in self.k:
return True
return False
def prefix(self, key):
v = self.res.get(self.root, "/" + key)
data = {}
if v.parent != None:
data["parent"] = v.parent.get_path()
l = []
for i in v.children:
if i.loaded == False:
self.store.fetch(i)
l.append(i.dir())
data["list"] = l
data["leaf"] = False
data["name"] = v.name
data["path"] = v.get_path()
return data
def build_part(self, params):
key = params.pop("classname", None)
if key in self.class_dict:
fixes = {}
for i in params:
try:
fixes[i] = float(params[i])
except:
pass
item = self.class_dict[key]
item.params.update(fixes)
item.rendered = False
self.export(item)
self.store.upsert(item)
def export(self, t):
folder_name = self.export_prefix + "/" + self.export_path + "/" + t.name
try:
os.removedirs(folder_name)
except:
pass
try:
os.makedirs(folder_name)
except:
pass
# create the object
o = t.c(**t.params)
gltf_path = folder_name + "/out.gltf"
r = o.exporter("gltf")
# build the gltf
v = r(gltf_path)
view = [r.scene_min, r.scene_max]
t.view = views.placed(view)
t.gltf_path = "/" + self.export_prefix + "/" + self.export_path + "/" + t.name
def set_image(self, imgname):
name = imgname.split(".")[0]
t = self.get_name(name)
if t is not None:
path = os.sep + self.export_prefix + os.sep + "img" + os.sep
t.image_path = path + imgname
t.pending = False
t.rendered = True
self.store.upsert(t)
def fetch(self, t):
self.store.fetch(t)
# due to multiple export paths (for dumping)
# this is set by itself
t.gltf_path = "/" + self.export_prefix + "/" + self.export_path + "/" + t.name
def params(self, key):
if self.exists(key) == False:
print(str(key) + "keyfail")
print(self.k.keys())
t = self.k["/" + key]
d = {}
if t.loaded == False:
self.fetch(t)
if t.built == False:
inst = t.c()
pi = inst.params().items()
for i in pi:
# only grab the floats for now
if isinstance(i[1], float):
d[i[0]] = i[1]
if isinstance(i[1], int):
d[i[0]] = i[1]
t.params = d
self.export(t)
t.built = True
t.rendered = False
self.store.upsert(t)
info = t.info()
return info
def get_zipped(self, t):
r = self.export_prefix + os.sep + self.export_path + os.sep + t.name + os.sep
export_folder = self.export_prefix + os.sep + "zip"
zip_file = export_folder + os.sep + t.name + ".zip"
f = os.listdir(r)
try:
os.makedirs(export_folder)
except:
pass
z = zipfile.ZipFile(zip_file, "w")
for i in f:
z.write(r + i, t.name + os.sep + i)
z.close()
return zip_file
def treeiter(self, key):
nodes = []
item = self.get_path(key)
for node in PreOrderIter(item):
if node.loaded == False:
self.fetch(node)
nodes.append(node)
return nodes
def rerender(self):
j = self.treeiter('export')
for i in j:
if i.is_leaf:
i.rendered = False
def stats(self):
j = self.treeiter('export')
counter = 0
build_counter = 0
render_count = 0
for i in j:
if i.is_leaf:
counter = counter + 1
if i.built:
build_counter = build_counter + 1
if i.rendered:
render_count = render_count + 1
return { "total":counter ,"built": build_counter,"rendered":render_count}
if __name__ == "__main__":
d = Directory("examples","export")