forked from amorilia/blender_nif_plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathruntest_smrailroads.py
173 lines (145 loc) · 7.44 KB
/
runtest_smrailroads.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
"""Automated Sid Meier's Railroads tests for the blender nif scripts."""
# ***** BEGIN LICENSE BLOCK *****
#
# BSD License
#
# Copyright (c) 2005-2010, NIF File Format Library and Tools
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the NIF File Format Library and Tools project may not be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ***** END LICENSE BLOCK *****
import logging
from itertools import izip
import Blender
from nif_test import TestSuite
from pyffi.formats.nif import NifFormat
# some tests to import and export nif files
class SMRailroadsTestSuite(TestSuite):
def hasNoSpecProp(self, block):
self.logger.info("Has no specular property?")
return all((not isinstance(prop, NifFormat.NiSpecularProperty))
for prop in block.properties)
def hasVColProp(self, block):
self.logger.info("Has vertex color property?")
return any(isinstance(prop, NifFormat.NiVertexColorProperty)
for prop in block.properties)
def hasZBufProp(self, block):
self.logger.info("Has z-buffer property?")
return any(isinstance(prop, NifFormat.NiZBufferProperty)
for prop in block.properties)
def hasIntegerExtra(self, trishape, name, value):
self.logger.info("Has %s with value %i?" % (name, value))
for extra in trishape.get_extra_datas():
if (isinstance(extra, NifFormat.NiIntegerExtraData)
and extra.name == name):
# success if value matches
return (extra.integer_data == value)
# extra block not found: failure
return False
def has_shader_texture(self, texprop, name, shaderindex):
self.logger.info("Has shader texture %s at index %i?" % (name, shaderindex))
shader_tex_desc = texprop.shader_textures[shaderindex]
return shader_tex_desc.texture_data.source.file_name.lower() == name.lower()
def checkSMRailRoads(self, root_block):
# sanity check
assert(isinstance(root_block, NifFormat.NiNode))
# find geom
geom = root_block.find(block_type=NifFormat.NiGeometry)
# root block property test
assert(self.hasVColProp(root_block))
assert(self.hasZBufProp(root_block))
# geometry property test
assert(self.hasNoSpecProp(geom))
# geometry extra data test
assert(self.hasIntegerExtra(geom, "EnvironmentIntensityIndex", 3))
assert(self.hasIntegerExtra(geom, "EnvironmentMapIndex", 0))
assert(self.hasIntegerExtra(geom, "LightCubeMapIndex", 4))
assert(self.hasIntegerExtra(geom, "NormalMapIndex", 1))
assert(self.hasIntegerExtra(geom, "ShadowTextureIndex", 5))
assert(self.hasIntegerExtra(geom, "SpecularIntensityIndex", 2))
# find texturing property
texprop = geom.find(block_type=NifFormat.NiTexturingProperty)
# geometry diffuse texture test
self.logger.info("Checking base texture.")
assert(texprop.has_base_texture)
assert(texprop.base_texture.source.file_name[-9:].lower() == "_diff.dds")
texbasename = texprop.base_texture.source.file_name[:-9]
# geometry shader textures
assert(self.has_shader_texture(texprop, "RRT_Engine_Env_map.dds", 0))
assert(self.has_shader_texture(texprop, texbasename + "_NRML.dds", 1))
assert(self.has_shader_texture(texprop, texbasename + "_SPEC.dds", 2))
assert(self.has_shader_texture(texprop, texbasename + "_EMSK.dds", 3))
assert(self.has_shader_texture(texprop, "RRT_Cube_Light_map_128.dds", 4))
# note: 5 is apparently never used, although it has an extra index
# check ninode flag
assert(root_block.flags == 16)
assert(geom.flags == 16)
def run(self):
nif_import = self.test(
filename = 'test/nif/smrailroads1.nif')
root_block = nif_import.root_blocks[0]
# this is a generic regression test of the test itself
# the original nif MUST pass it (if not there is a bug in the
# testing code)
self.logger.info(
"Checking original nif (for regression, MUST succeed).")
self.checkSMRailRoads(root_block)
# check that specularity was imported (these nifs do not have specular
# properties)
self.logger.info("Checking specular color import.")
testgeom = root_block.find(block_type=NifFormat.NiGeometry,
block_name="Test")
nifspec = testgeom.find(block_type=NifFormat.NiMaterialProperty).specular_color
blendermat = Blender.Object.Get("Test").data.materials[0]
assert(abs(blendermat.getSpec() - 1.0) < 1e-5)
blenderspec = blendermat.getSpecCol()
assert(abs(nifspec.r - blenderspec[0]) < 1e-5)
assert(abs(nifspec.g - blenderspec[1]) < 1e-5)
assert(abs(nifspec.b - blenderspec[2]) < 1e-5)
nif_export = self.test(
filename = 'test/nif/_smrailroads1.nif',
config = dict(EXPORT_VERSION = "Sid Meier's Railroads"),
selection = ['Test'])
root_block = nif_export.root_blocks[0]
# check exported specularity
self.logger.info("Checking specular color export.")
testgeom_export = root_block.find(block_type=NifFormat.NiGeometry,
block_name="Test")
nifspec_export = testgeom.find(block_type=NifFormat.NiMaterialProperty).specular_color
assert(abs(nifspec.r - nifspec_export.r) < 1e-5)
assert(abs(nifspec.g - nifspec_export.g) < 1e-5)
assert(abs(nifspec.b - nifspec_export.b) < 1e-5)
self.logger.info("Checking alpha flags and threshold export.")
nifalpha_export = testgeom_export.find(block_type=NifFormat.NiAlphaProperty)
assert(nifalpha_export.flags == 13037)
assert(nifalpha_export.threshold == 150)
self.logger.info("Checking extra shader export.")
assert(testgeom_export.has_shader)
assert(testgeom_export.shader_name == "RRT_NormalMap_Spec_Env_CubeLight")
# check that the re-exported file still passes the check
self.logger.info("Checking exported nif...")
self.checkSMRailRoads(root_block)
suite = SMRailroadsTestSuite("smrailroads")
suite.run()