From 36fdbed2f0a45d9766db80fef987614b7637c1a2 Mon Sep 17 00:00:00 2001 From: Jeroen Bakker Date: Mon, 22 Mar 2021 11:33:48 +0100 Subject: LibraryOverrides: Initial Test Case. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added a first test case for review. This will be the base for future test cases. The current API is sufficient for what is expected for such a low level API. One concern is that you need to trigger a save in order to update the library overrides structure. Not expected from TD/Dev point of view. Test cases are very important when implementing restrictive mode as it is a second evaluation mode that has impact on the (current) permissive mode. Reviewed By: Sebastián Barschkis, Bastien Montagne Differential Revision: https://developer.blender.org/D10747 --- tests/python/CMakeLists.txt | 6 ++ tests/python/bl_blendfile_library_overrides.py | 107 +++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 tests/python/bl_blendfile_library_overrides.py (limited to 'tests/python') diff --git a/tests/python/CMakeLists.txt b/tests/python/CMakeLists.txt index 260eb75d64f..969b748e973 100644 --- a/tests/python/CMakeLists.txt +++ b/tests/python/CMakeLists.txt @@ -154,6 +154,12 @@ add_blender_test( --output-dir ${TEST_OUT_DIR}/blendfile_io/ ) +add_blender_test( + blendfile_library_overrides + --python ${CMAKE_CURRENT_LIST_DIR}/bl_blendfile_library_overrides.py -- + --output-dir ${TEST_OUT_DIR}/blendfile_io/ +) + # ------------------------------------------------------------------------------ # MODELING TESTS add_blender_test( diff --git a/tests/python/bl_blendfile_library_overrides.py b/tests/python/bl_blendfile_library_overrides.py new file mode 100644 index 00000000000..87e25eff8ca --- /dev/null +++ b/tests/python/bl_blendfile_library_overrides.py @@ -0,0 +1,107 @@ +# Apache License, Version 2.0 + +# ./blender.bin --background -noaudio --python tests/python/bl_blendfile_library_overrides.py +import pathlib +import bpy +import sys +import os +import unittest + +sys.path.append(os.path.dirname(os.path.realpath(__file__))) +from bl_blendfile_utils import TestHelper + + +class TestLibraryOverrides(TestHelper, unittest.TestCase): + MESH_LIBRARY_PARENT = "LibMeshParent" + OBJECT_LIBRARY_PARENT = "LibMeshParent" + MESH_LIBRARY_CHILD = "LibMeshChild" + OBJECT_LIBRARY_CHILD = "LibMeshChild" + + def __init__(self, args): + self.args = args + + output_dir = pathlib.Path(self.args.output_dir) + self.ensure_path(str(output_dir)) + self.output_path = output_dir / "blendlib_overrides.blend" + self.test_output_path = output_dir / "blendlib_overrides_test.blend" + + bpy.ops.wm.read_homefile(use_empty=True, use_factory_startup=True) + mesh = bpy.data.meshes.new(TestLibraryOverrides.MESH_LIBRARY_PARENT) + obj = bpy.data.objects.new(TestLibraryOverrides.OBJECT_LIBRARY_PARENT, object_data=mesh) + bpy.context.collection.objects.link(obj) + mesh_child = bpy.data.meshes.new(TestLibraryOverrides.MESH_LIBRARY_CHILD) + obj_child = bpy.data.objects.new(TestLibraryOverrides.OBJECT_LIBRARY_CHILD, object_data=mesh_child) + obj_child.parent = obj + bpy.context.collection.objects.link(obj_child) + bpy.ops.wm.save_as_mainfile(filepath=str(self.output_path), check_existing=False, compress=False) + + def __ensure_override_library_updated(self): + # During save the override_library is updated. + bpy.ops.wm.save_as_mainfile(filepath=str(self.test_output_path), check_existing=False, compress=False) + + def test_link_and_override_property(self): + bpy.ops.wm.read_homefile(use_empty=True, use_factory_startup=True) + bpy.data.orphans_purge() + + link_dir = self.output_path / "Object" + bpy.ops.wm.link(directory=str(link_dir), filename=TestLibraryOverrides.OBJECT_LIBRARY_PARENT) + + obj = bpy.data.objects[TestLibraryOverrides.OBJECT_LIBRARY_PARENT] + assert(obj.override_library is None) + local_id = obj.override_create() + assert(local_id.override_library) + assert(local_id.data.override_library is None) + assert(len(local_id.override_library.properties) == 0) + + local_id.location.y = 1.0 + + self.__ensure_override_library_updated() + + assert (len(local_id.override_library.properties) == 1) + assert(len(local_id.override_library.properties) == 1) + override_prop = local_id.override_library.properties[0] + assert(override_prop.rna_path, "location"); + assert(len(override_prop.operations) == 1) + override_operation = override_prop.operations[0] + assert (override_operation.operation == 'REPLACE') + # Setting location.y overridded all elements in the location array. -1 is a wildcard. + assert(override_operation.subitem_local_index == -1) + + +TESTS = ( + TestLibraryOverrides, +) + + +def argparse_create(): + import argparse + + # When --help or no args are given, print this help + description = "Test library overrides of blend file." + parser = argparse.ArgumentParser(description=description) + parser.add_argument( + "--output-dir", + dest="output_dir", + default=".", + help="Where to output temp saved blendfiles", + required=False, + ) + + return parser + + +def main(): + args = argparse_create().parse_args() + + # Don't write thumbnails into the home directory. + bpy.context.preferences.filepaths.use_save_preview_images = False + + for Test in TESTS: + Test(args).run_all_tests() + + +if __name__ == '__main__': + import sys + sys.argv = [__file__] + \ + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else []) + main() -- cgit v1.2.3