1 """Automated tests for the blender nif scripts."""
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 import logging
36
37 import Blender
38 from import_nif import NifImport
39 from export_nif import NifExport
40 from nif_common import NifConfig
41
43 """A test suite class.
44
45 @ivar scene: The Blender scene for this test suite.
46 @type scene: Blender.Scene.Scene
47 @ivar layer: The current Blender layer in the scene.
48 @type layer: C{int}
49 """
51 """Initialize a new test suite with given name.
52
53 @param name: The name of the test (will be used as name of the scene
54 for this test).
55 @type name: C{str}
56 """
57 self.logger = logging.getLogger("niftools.blender.test")
58 self.scene = Blender.Scene.New(name)
59 self.layer = 1
60
61
62 self.scene.makeCurrent()
63
64 self.scene.setLayers([self.layer])
65
66 - def test(self, filename=None, config=None, selection=None, next_layer=None):
67 """Run given test, and increase layer after export.
68
69 @param filename: The name of the file to test.
70 @type filename: C{str}
71 @param config: Configuration options that differ from their default
72 values. If the dictionary has the key EXPORT_VERSION then the
73 selection is exported, otherwise a file is imported. Optional.
74 @type config: C{dict}
75 @param selection: List of names of objects to select before running
76 the test. Optional.
77 @type selection: C{list} of C{str}
78 @param next_layer: Whether or not to advance a layer after the test.
79 If C{None} (default) then a layer is advanced only after export.
80 @type next_layer: C{bool} or C{NoneType}
81 @return: The import or export object.
82 @rtype: L{NifImport} or L{NifExport}
83 """
84
85 if filename is None:
86 raise ValueError("A test must specify a filename.")
87 if config is None:
88 config = {}
89 if selection is None:
90 selection = []
91
92
93 self.scene.objects.selected = [
94 ob for ob in self.scene.objects if ob.name in selection]
95
96
97 finalconfig = dict(**NifConfig.DEFAULTS)
98 logging.getLogger("niftools").setLevel(logging.DEBUG)
99 logging.getLogger("pyffi").setLevel(logging.WARNING)
100
101 for key, value in config.items():
102 finalconfig[key] = value
103
104
105 if 'EXPORT_VERSION' in config:
106
107 self.logger.info("Exporting %s" % filename)
108
109 finalconfig["EXPORT_FILE"] = filename
110 result = NifExport(**finalconfig)
111
112
113
114
115 if next_layer in (None, True):
116 self.layer += 1
117 self.scene.setLayers([self.layer])
118
119
120 return result
121 else:
122 self.logger.info("Importing %s" % filename)
123
124
125 finalconfig["IMPORT_FILE"] = filename
126 result = NifImport(**finalconfig)
127
128 if next_layer is True:
129 self.layer += 1
130 self.scene.setLayers([self.layer])
131
132 return result
133
135 """Run the test suite. Override."""
136 raise NotImplementedError
137
139 """Test the specified files.
140
141 @deprecated: Derive your tests from the L{TestSuite} class instead!
142 @param directory: Folder where the test nif files reside.
143
144 @param files: A list of all files to test. Each entry in the list
145 is a tuple containing the filename, a dictionary of config options
146 , and a list of names of
147 objects to select before running the import.
148 """
149 scene = Blender.Scene.GetCurrent()
150 layer = 1
151
152 for filename, filecfg, selection in files:
153
154 scene.objects.selected = [
155 ob for ob in scene.objects if ob.name in selection]
156
157
158 config = dict(**NifConfig.DEFAULTS)
159 for key, value in filecfg.items():
160 config[key] = value
161 logger = logging.getLogger("niftools.blender.test")
162 logging.getLogger("niftools").setLevel(logging.DEBUG)
163 logging.getLogger("pyffi").setLevel(logging.WARNING)
164
165
166 if 'EXPORT_VERSION' in filecfg:
167
168 logger.info("Exporting %s" % filename)
169
170 config["EXPORT_FILE"] = "%s/%s" % (directory, filename)
171 NifExport(**config)
172
173
174
175
176 layer += 1
177 scene.setLayers([layer])
178 else:
179 logger.info("Importing %s" % filename)
180
181 config["IMPORT_FILE"] = "%s/%s" % (directory, filename)
182
183
184 NifImport(**config)
185
186
187 scene.objects.selected = []
188
189 scene.setLayers([1])
190