-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk_importExport.py
192 lines (166 loc) · 4.97 KB
/
k_importExport.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
import Blender
from Blender import Scene
class FileOperator:
def listTypes(self):
pass
def findTypes(self,filename):
"""
returns all possible file types for a given filename
"""
_,ext,_=self.separate(filename)
foundTypes=[]
types=self.listTypes()
for t in types:
if ext in t.extensions:
foundTypes.append(t)
return foundTypes
def separate(self,filename):
"""
separates a filename like myfile.foo:myobj1:myobj2
into an undecorated filename, extension, and objects array
"""
objs=[]
pos=filename.rfind(".")
ext=""
if pos>-1:
objs=filename[pos+1:].split(":")
ext=objs[0]
objs=objs[1:]
filename=filename[0:pos+4] # cull off any :... junk
return filename, ext, objs
def __str__(self):
result=self.__class__.__name__+":\n"
types=self.listTypes()
for t in types:
result=result+"\t"+t.__str__()+"\n"
return result
class FileImporter(FileOperator):
def importFile(self,filename):
pass
def listTypes(self):
pass
class FileExporter(FileOperator):
def exportFile(self,filename):
pass
def listTypes(self):
pass
class FileImporterSet(FileImporter):
"""
a fileImporter capable of containing other fileImporters
"""
def __init__(self):
self.subImporters=[]
def importFile(self,filename):
"""
Imports the filename and returns the object or None.
Also, this will use the first available importer. For better control,
use findTypes(filename), select one, then to selectedType.owner.importFile(filename)
"""
types=self.findTypes(filename)
if len(types)>0:
return types[0].owner.importFile(filename)
return None
def add(self,importer):
"""
adds a sub-importer to this importer
"""
self.subImporters.append(importer)
def listTypes(self):
"""
Lists all acceptable types for this importer
"""
allTypes=[]
for sub in self.subImporters:
types=sub.listTypes()
if len(types)>0:
allTypes=allTypes+types
return allTypes
class FileExporterSet(FileExporter):
"""
a fileExporter capable of containing other fileExporters
"""
def __init__(self):
subExporters=[]
def exportFile(self,filename):
"""
Imports the filename and returns the object or None.
Also, this will use the first available exporter. For better control,
use findTypes(filename), select one, then to selectedType.owner.exportFile(filename)
"""
types=self.findTypes(filename)
if len(types)>0:
return types[0].owner.importFile(filename)
return None
def add(self,exporter):
"""
adds a sub-importer to this exporter
"""
self.subExporters.append(exporter)
def listTypes(self):
"""
Lists all acceptable types for this exporter
"""
allTypes=[]
for sub in self.subExporters:
types=sub.listTypes()
if len(types)>0:
allTypes=allTypes+types
return allTypes
class ImageImporter(FileImporter):
def importFile(self,filename):
pass
def listTypes(self):
pass
def importAsPlane(self,filename,w=None,h=None):
pass
def importAsTerrain(self,filename,w=None,h=None):
pass
class ObjectImporter(FileImporter):
def importFile(self,filename):
pass
def listTypes(self):
pass
class PathImporter(FileImporter):
def importFile(self,filename):
pass
def listTypes(self):
pass
def importAsMotion(self,filename,obj):
pass
def importAsIpo(self,filename,obj,curveName):
pass
class FileType:
def __init__(self, owner, name, extensions):
self.name=name
self.owner=owner
if type(extensions).__name__!='list':
self.extensions=[]
self.extensions.append(extensions)
else:
self.extensions=extensions
def __str__(self):
result=self.name+" using "+self.owner.__class__.__name__+" ["
for x in self.extensions:
result=result+" ."+x
return result+" ]"
# Create default lists
importers=FileImporterSet()
exporters=FileExporterSet()
# Import whatever is available
def loadAll():
try:
import os, glob, sys
fullPython=True
except ImportError:
fullPython=False
if fullPython==True:
sys.path.append("inout")
for inout in glob.glob("inout"+os.path.sep+"*"):
inout=inout.split(os.path.sep)[-1][0:-3]
m=__import__(inout, globals(), locals())
else:
import sys
sys.path.append("inout")
import blender_standard
def main():
loadAll()