forked from gabdube/tinyblend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tinyblend.py
179 lines (127 loc) · 5.48 KB
/
test_tinyblend.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
# -*- coding: utf-8 -*-
"""
Assets loader tests
author: Gabriel Dube
"""
import sys
from os.path import dirname as dn
sys.path.append(dn(dn(__file__)))
import pytest, gc
from tinyblend import BlenderFile, BlenderObjectFactory, BlenderObject, BlenderFileImportException, BlenderFileReadException
def test_open_blend_file():
blend = BlenderFile('fixtures/test1.blend')
head = blend.header
assert 'VersionInfo(major=2, minor=7, rev=7)', repr(head.version)
assert BlenderFile.Arch.X64, head.arch
assert BlenderFile.Endian.Little, head.endian
blend.close()
def test_open_blend_file_28():
blend = BlenderFile('fixtures/test_blender28.blend')
head = blend.header
assert 'VersionInfo(major=2, minor=8, rev=0)', repr(head.version)
assert BlenderFile.Arch.X64, head.arch
assert BlenderFile.Endian.Little, head.endian
blend.close()
def test_should_read_scene_data():
blend = BlenderFile('fixtures/test1.blend')
worlds = blend.list('World')
assert worlds.file is blend, 'World file is not blend'
assert len(worlds) == 1, 'Test blend should have one world'
world = worlds.find_by_name('TestWorld')
assert world.file is blend
assert isinstance(world, worlds.object)
assert world.VERSION == blend.header.version
assert len(world.mtex) == 18
assert world.aodist > 12.8999 and world.aodist < 12.90001
assert world.id.name[0:11] == b'WOTestWorld'
scenes = blend.list('Scene')
assert len(scenes) == 1, 'Test blend should have one scene'
rctfs = blend.list('rctf')
pytest.raises(BlenderFileReadException, rctfs.find_by_name, 'blah') # rctf object do not have a name
pytest.raises(BlenderFileReadException, blend.list, 'foos') # foos is not a valid structure
pytest.raises(KeyError, worlds.find_by_name, 'BOO') # There are no worlds by the name of BOO in the blend file
blend.close()
def test_should_read_scene_data_28():
blend = BlenderFile('fixtures/test_blender28.blend')
worlds = blend.list('World')
assert worlds.file is blend, 'World file is not blend'
assert len(worlds) == 1, 'Test blend should have one world'
scenes = blend.list('Scene')
assert len(scenes) == 1, 'Test blend should have one scene'
blend.close()
def test_equality():
blend = BlenderFile('fixtures/test1.blend')
worlds = blend.list('World')
world1 = worlds.find_by_name('TestWorld')
world2 = worlds.find_by_name('TestWorld')
assert id(world1) is not id(world2)
assert world1 == world2
def test_should_lookup_pointer():
BlenderObject.CACHE = {}
BlenderObjectFactory.CACHE = {}
blend = BlenderFile('fixtures/test1.blend')
worlds = blend.list('World')
scenes = blend.list('Scene')
world = worlds.find_by_name('TestWorld')
scene = scenes.find_by_name('MyTestScene')
pytest.raises(BlenderFileReadException, blend._from_address, 0)
pytest.raises(AttributeError, setattr, scene, 'world', 0)
pytest.raises(AttributeError, delattr, scene, 'world')
scene_world = scene.world
assert type(scene_world) is worlds.object
assert scene_world is not world
assert scene.world == world
assert scene.world is scene_world
assert scene.id.next is None # Null pointer lookup returns None
def test_should_lookup_pointer_array():
blend = BlenderFile('fixtures/test1.blend')
obj = blend.list('Object').find_by_name('Suzanne')
data = obj.data
assert data.totvert == len(data.mvert)
def test_blend_struct_lookup():
blend = BlenderFile('fixtures/test1.blend')
scene_index = blend.index.type_names.index('Scene')
float_index = blend.index.type_names.index('float')
bad_index = 983742
struct = blend._struct_lookup(scene_index)
assert struct.index == scene_index, 'Struct index is not scene index'
pytest.raises(BlenderFileReadException, blend._struct_lookup, float_index)
pytest.raises(BlenderFileReadException, blend._struct_lookup, 983742)
blend.close()
def test_weakref():
blend = BlenderFile('fixtures/test1.blend')
worlds = blend.list('World')
del blend
pytest.raises(RuntimeError, getattr, worlds, 'file')
pytest.raises(RuntimeError, len, worlds)
pytest.raises(RuntimeError, repr, worlds)
pytest.raises(RuntimeError, str, worlds)
pytest.raises(RuntimeError, worlds.find_by_name, '...')
def test_cache_lookup():
blend = BlenderFile('fixtures/test1.blend')
v = blend.header.version
worlds = blend.list('World')
assert BlenderObjectFactory.CACHE[v]['World']() is not None
assert BlenderObject.CACHE[v]['World']() is not None
del worlds
gc.collect()
assert BlenderObjectFactory.CACHE[v]['World']() is None
assert BlenderObject.CACHE[v]['World']() is None
worlds = blend.list('World')
assert isinstance(worlds, BlenderObjectFactory)
assert BlenderObjectFactory.CACHE[v]['World']() is not None
assert BlenderObject.CACHE[v]['World']() is not None
blend.close()
def test_list_structures():
blend = BlenderFile('fixtures/test1.blend')
structs = blend.list_structures()
assert len(structs) > 30
assert 'Scene' in structs
def test_tree():
blend = BlenderFile('fixtures/test1.blend')
scene_repr = blend.tree('Scene')
assert len(scene_repr.splitlines()) > 100
assert 'ID' in scene_repr
def test_open_bad_blend_file():
pytest.raises(BlenderFileImportException, BlenderFile, 'fixtures/test2.blend')
pytest.raises(BlenderFileImportException, BlenderFile, 'fixtures/test3.blend')