Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bl_alembic_import_test.py « python « tests - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3a4af26e111ce94f9f1b33828ee67df99aa7569 (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

# <pep8 compliant>

"""
./blender.bin --background -noaudio --factory-startup --python tests/python/bl_alembic_import_test.py -- --testdir /path/to/lib/tests/alembic
"""

import pathlib
import sys
import unittest

import bpy

args = None


class AbstractAlembicTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.testdir = args.testdir

    def setUp(self):
        self.assertTrue(self.testdir.exists(),
                        'Test dir %s should exist' % self.testdir)

        # Make sure we always start with a known-empty file.
        bpy.ops.wm.open_mainfile(filepath=str(self.testdir / "empty.blend"))

    def assertAlmostEqualFloatArray(self, actual, expect, places=6, delta=None):
        """Asserts that the arrays of floats are almost equal."""

        self.assertEqual(len(actual), len(expect),
                         'Actual array has %d items, expected %d' % (len(actual), len(expect)))

        for idx, (act, exp) in enumerate(zip(actual, expect)):
            self.assertAlmostEqual(act, exp, places=places, delta=delta,
                                   msg='%f != %f at index %d' % (act, exp, idx))


class SimpleImportTest(AbstractAlembicTest):
    def test_import_cube_hierarchy(self):
        res = bpy.ops.wm.alembic_import(
            filepath=str(self.testdir / "cubes-hierarchy.abc"),
            as_background_job=False)
        self.assertEqual({'FINISHED'}, res)

        # The objects should be linked to scene_collection in Blender 2.8,
        # and to scene in Blender 2.7x.
        objects = bpy.context.scene.objects
        self.assertEqual(13, len(objects))

        # Test the hierarchy.
        self.assertIsNone(objects['Cube'].parent)
        self.assertEqual(objects['Cube'], objects['Cube_001'].parent)
        self.assertEqual(objects['Cube'], objects['Cube_002'].parent)
        self.assertEqual(objects['Cube'], objects['Cube_003'].parent)
        self.assertEqual(objects['Cube_003'], objects['Cube_004'].parent)
        self.assertEqual(objects['Cube_003'], objects['Cube_005'].parent)
        self.assertEqual(objects['Cube_003'], objects['Cube_006'].parent)

    def test_inherit_or_not(self):
        res = bpy.ops.wm.alembic_import(
            filepath=str(self.testdir / "T52022-inheritance.abc"),
            as_background_job=False)
        self.assertEqual({'FINISHED'}, res)

        # The objects should be linked to scene_collection in Blender 2.8,
        # and to scene in Blender 2.7x.
        objects = bpy.context.scene.objects

        # ABC parent is top-level object, which translates to nothing in Blender
        self.assertIsNone(objects['locator1'].parent)

        # ABC parent is locator1, but locator2 has "inherits Xforms" = false, which
        # translates to "no parent" in Blender.
        self.assertIsNone(objects['locator2'].parent)

        # Shouldn't have inherited the ABC parent's transform.
        x, y, z = objects['locator2'].matrix_world.to_translation()
        self.assertAlmostEqual(0, x)
        self.assertAlmostEqual(0, y)
        self.assertAlmostEqual(2, z)

        # ABC parent is inherited and translates to normal parent in Blender.
        self.assertEqual(objects['locator2'], objects['locatorShape2'].parent)

        # Should have inherited its ABC parent's transform.
        x, y, z = objects['locatorShape2'].matrix_world.to_translation()
        self.assertAlmostEqual(0, x)
        self.assertAlmostEqual(0, y)
        self.assertAlmostEqual(2, z)


    def test_select_after_import(self):
        # Add a sphere, so that there is something in the scene, selected, and active,
        # before we do the Alembic import.
        bpy.ops.mesh.primitive_uv_sphere_add()
        sphere = bpy.context.active_object
        self.assertEqual('Sphere', sphere.name)
        self.assertEqual([sphere], bpy.context.selected_objects)

        bpy.ops.wm.alembic_import(
            filepath=str(self.testdir / "cubes-hierarchy.abc"),
            as_background_job=False)

        # The active object is probably the first one that was imported, but this
        # behaviour is not defined. At least it should be one of the cubes, and
        # not the sphere.
        self.assertNotEqual(sphere, bpy.context.active_object)
        self.assertTrue('Cube' in bpy.context.active_object.name)

        # All cubes should be selected, but the sphere shouldn't be.
        for ob in bpy.data.objects:
            self.assertEqual('Cube' in ob.name, ob.select)

    def test_change_path_constraint(self):
        import math

        fname = 'cube-rotating1.abc'
        abc = self.testdir / fname
        relpath = bpy.path.relpath(str(abc))

        res = bpy.ops.wm.alembic_import(filepath=str(abc), as_background_job=False)
        self.assertEqual({'FINISHED'}, res)
        cube = bpy.context.active_object

        # Check that the file loaded ok.
        bpy.context.scene.frame_set(10)
        x, y, z = cube.matrix_world.to_euler('XYZ')
        self.assertAlmostEqual(x, 0)
        self.assertAlmostEqual(y, 0)
        self.assertAlmostEqual(z, math.pi / 2, places=5)

        # Change path from absolute to relative. This should not break the animation.
        bpy.context.scene.frame_set(1)
        bpy.data.cache_files[fname].filepath = relpath
        bpy.context.scene.frame_set(10)

        x, y, z = cube.matrix_world.to_euler('XYZ')
        self.assertAlmostEqual(x, 0)
        self.assertAlmostEqual(y, 0)
        self.assertAlmostEqual(z, math.pi / 2, places=5)

        # Replace the Alembic file; this should apply new animation.
        bpy.data.cache_files[fname].filepath = relpath.replace('1.abc', '2.abc')
        bpy.context.scene.update()

        if args.with_legacy_depsgraph:
            bpy.context.scene.frame_set(10)

        x, y, z = cube.matrix_world.to_euler('XYZ')
        self.assertAlmostEqual(x, math.pi / 2, places=5)
        self.assertAlmostEqual(y, 0)
        self.assertAlmostEqual(z, 0)

    def test_change_path_modifier(self):
        import math

        fname = 'animated-mesh.abc'
        abc = self.testdir / fname
        relpath = bpy.path.relpath(str(abc))

        res = bpy.ops.wm.alembic_import(filepath=str(abc), as_background_job=False)
        self.assertEqual({'FINISHED'}, res)
        plane = bpy.context.active_object

        # Check that the file loaded ok.
        bpy.context.scene.frame_set(6)
        mesh = plane.to_mesh(bpy.context.scene, True, 'RENDER')
        self.assertAlmostEqual(-1, mesh.vertices[0].co.x)
        self.assertAlmostEqual(-1, mesh.vertices[0].co.y)
        self.assertAlmostEqual(0.5905638933181763, mesh.vertices[0].co.z)

        # Change path from absolute to relative. This should not break the animation.
        bpy.context.scene.frame_set(1)
        bpy.data.cache_files[fname].filepath = relpath
        bpy.context.scene.frame_set(6)

        mesh = plane.to_mesh(bpy.context.scene, True, 'RENDER')
        self.assertAlmostEqual(1, mesh.vertices[3].co.x)
        self.assertAlmostEqual(1, mesh.vertices[3].co.y)
        self.assertAlmostEqual(0.5905638933181763, mesh.vertices[3].co.z)

    def test_import_long_names(self):
        # This file contains very long names. The longest name is 4047 chars.
        bpy.ops.wm.alembic_import(
            filepath=str(self.testdir / "long-names.abc"),
            as_background_job=False)

        self.assertIn('Cube', bpy.data.objects)
        self.assertEqual('CubeShape', bpy.data.objects['Cube'].data.name)


class VertexColourImportTest(AbstractAlembicTest):
    def test_import_from_houdini(self):
        # Houdini saved "face-varying", and as RGB.
        res = bpy.ops.wm.alembic_import(
            filepath=str(self.testdir / "vertex-colours-houdini.abc"),
            as_background_job=False)
        self.assertEqual({'FINISHED'}, res)

        ob = bpy.context.active_object
        layer = ob.data.vertex_colors['Cf']  # MeshLoopColorLayer

        # Test some known-good values.
        self.assertAlmostEqualFloatArray(layer.data[0].color, (0, 0, 0))
        self.assertAlmostEqualFloatArray(layer.data[98].color, (0.9019607, 0.4745098, 0.2666666))
        self.assertAlmostEqualFloatArray(layer.data[99].color, (0.8941176, 0.4705882, 0.2627451))

    def test_import_from_blender(self):
        # Blender saved per-vertex, and as RGBA.
        res = bpy.ops.wm.alembic_import(
            filepath=str(self.testdir / "vertex-colours-blender.abc"),
            as_background_job=False)
        self.assertEqual({'FINISHED'}, res)

        ob = bpy.context.active_object
        layer = ob.data.vertex_colors['Cf']  # MeshLoopColorLayer

        # Test some known-good values.
        self.assertAlmostEqualFloatArray(layer.data[0].color, (1.0, 0.0156862, 0.3607843))
        self.assertAlmostEqualFloatArray(layer.data[98].color, (0.0941176, 0.1215686, 0.9137254))
        self.assertAlmostEqualFloatArray(layer.data[99].color, (0.1294117, 0.3529411, 0.7529411))


def main():
    global args
    import argparse

    if '--' in sys.argv:
        argv = [sys.argv[0]] + sys.argv[sys.argv.index('--') + 1:]
    else:
        argv = sys.argv

    parser = argparse.ArgumentParser()
    parser.add_argument('--testdir', required=True, type=pathlib.Path)
    parser.add_argument('--with-legacy-depsgraph', default=False,
                        type=lambda v: v in {'ON', 'YES', 'TRUE'})
    args, remaining = parser.parse_known_args(argv)

    unittest.main(argv=remaining)


if __name__ == "__main__":
    import traceback
    # So a python error exits Blender itself too
    try:
        main()
    except SystemExit:
        raise
    except:
        traceback.print_exc()
        sys.exit(1)