[Bf-blender-cvs] [8b151c5cdbd] soc-2021-geometry-nodes-regression-test: Updated the script, adding support for cmd arguments

Himanshi Kalra noreply at git.blender.org
Thu Jun 17 07:46:16 CEST 2021


Commit: 8b151c5cdbd8a4c81a7f49a9b470883504822936
Author: Himanshi Kalra
Date:   Tue Jun 15 13:18:29 2021 +0530
Branches: soc-2021-geometry-nodes-regression-test
https://developer.blender.org/rB8b151c5cdbd8a4c81a7f49a9b470883504822936

Updated the script, adding support for cmd arguments

Added exception errors for try except, formatting, adding comments
Improved the workflow, updated error messages. Re-running of the
test after BLENDER_TEST_UPDATE.

===================================================================

M	tests/python/geo_node_test.py

===================================================================

diff --git a/tests/python/geo_node_test.py b/tests/python/geo_node_test.py
index a684aeaf54d..e6cf5d8c61b 100644
--- a/tests/python/geo_node_test.py
+++ b/tests/python/geo_node_test.py
@@ -1,53 +1,122 @@
-# Load a blend file 
+# ##### 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>
+
+
+# Script for running tests pre-loaded from a blend file.
+#
+# General Idea
+#
+# Load a blend file
 # Select the object
 # Apply the GN modifier on a duplicated object
 # Compare the result
 # If test pass, print("SUCESS")
 # If test fail, print("FAIL")
-    # Update tests if BLENDER_TEST_UPDATE flag is set.
-# Display result of failed tests [?]
+#    Update tests if BLENDER_TEST_UPDATE flag is set.
+# Display result of failed tests
 
 # Code to be re-used from Mesh Test
 # Depending on what all we want to use
- ## the mesh comparison code
- ## -- run-test code
+# the mesh comparison code
+# -- run-test code
 # Code to be re-used from a Compositor
- ## Edit Cmake to iterate over directories.
-
-### Questions ###
-# Usage of __slots__ (only getting save memory, optimize) [Can Skip specific to Compositor]
-# How to keep track of failed tests.
-# Every blend file will run the test script and no memory.
-# For compositor, it only tells which directory has a failed test, not the exact file name
-# Pre-decide on the name of the test object and expected object ? Default name of the modifier?
-# Should I make it generic for any modifier or just geometry nodes?
-
+# Edit Cmake to iterate over directories.
 
 ### RUN TEST COMMAND ###
-# blender -b path_to_blend_file --python path/to/geo_node_test.py
+# blender -b path_to_blend_file --python path/to/geo_node_test.py -- [--first-time]
 
 import bpy
 import os
+import sys
+
 
-def get_objects():
+def get_test_object():
+    """
+    Get test object or raise Exception.
+    """
     try:
-        test_object = bpy.data.objects["test_obj"]
-    except:
+        test_object = bpy.data.objects["test_object"]
+    except KeyError:
         raise Exception("No test object found!")
+    return test_object
+
+
+def get_expected_object():
+    """
+    Get expected object or raise Exception.
+    """
     try:
-        expected_object = bpy.data.objects["expected_obj"]
-    except:
+        expected_object = bpy.data.objects["expected_object"]
+    except KeyError:
         raise Exception("No expected object found!")
-    return [test_object, expected_object]
+    return expected_object
 
-def apply_modifier(evaluated_object):
+
+def select_the_object(any_object):
     """
-    Apply all modifiers added to the current object [Discuss]
+    Select the given object.
     """
     bpy.ops.object.mode_set(mode="OBJECT")
     bpy.ops.object.select_all(action="DESELECT")
-    bpy.context.view_layer.objects.active = evaluated_object
-    
+    bpy.context.view_layer.objects.active = any_object
+
+    return any_object
+
+
+def remove_modifiers_from_object(any_object):
+    """
+    Remove modifiers from the selected object.
+    """
+    any_object = select_the_object(any_object)
+    modifier_list = list(any_object.modifiers)
+    for modifier in modifier_list:
+        bpy.ops.object.modifier_remove(modifier=modifier.name)
+    return any_object
+
+
+def run_first_time():
+    """
+    Automatically creates the expected object when script
+    is run with argument "--first-time"
+    """
+    try:
+        expected_object = bpy.data.objects["expected_object"]
+        print("\nExpected Object already, skipping creating a new object.")
+        return
+    except KeyError:
+        expected_object = duplicate_test_object(get_test_object())
+        expected_object.location = (0, 10, 0)
+        expected_object.name = "expected_object"
+
+        expected_object = remove_modifiers_from_object(expected_object)
+
+        # Save file with the expected object.
+        bpy.ops.wm.save_as_mainfile(filepath=bpy.data.filepath)
+
+
+def apply_modifier(evaluated_object):
+    """
+    Apply all modifiers (Geometry Nodes for now) added to the current object [Discuss]
+    """
+    evaluated_object = select_the_object(evaluated_object)
+
     modifiers_list = evaluated_object.modifiers
 
     if modifiers_list[0].type == "NODES":
@@ -58,26 +127,30 @@ def apply_modifier(evaluated_object):
 
 
 def compare_mesh(evaluated_object, expected_object):
+    """
+    Compares the meshes of evaluated and expected objects.
+    """
     evaluated_data = evaluated_object.data
     exp_data = expected_object.data
     result = evaluated_data.unit_test_compare(mesh=exp_data)
     if result == "Same":
-        print("PASS")    
+        print("PASS")
     else:
         failed_test(evaluated_object, expected_object, result)
 
-def passed_test():
-    pass
 
 def failed_test(evaluated_object, expected_object, result):
     """
-    [Need discussion]
+    Prints the failed test.
+    Updates the expected object on failure if BLENDER_TEST_UPDATE
+    environment variable is set.
     """
+    print("FAIL with {}".format(result))
     update_test_flag = os.getenv('BLENDER_TEST_UPDATE') is not None
     if not update_test_flag:
-        print("Test failed with {}".format(result))
         return
-    
+
+    print("Updating the test...")
     evaluated_object.location = expected_object.location
     expected_object_name = expected_object.name
     bpy.data.objects.remove(expected_object, do_unlink=True)
@@ -87,11 +160,16 @@ def failed_test(evaluated_object, expected_object, result):
     bpy.ops.wm.save_as_mainfile(filepath=bpy.data.filepath)
 
     print("The test file was updated with new expected object")
-    print("The blend file was saved.")
+    print("The blend file : {} was updated.".format(
+        bpy.path.display_name_from_filepath(bpy.data.filepath)))
+    print("Re-running the test...")
+    main()
+
 
 def duplicate_test_object(test_object):
-    # Duplicate test object.
-    bpy.ops.object.mode_set(mode="OBJECT")
+    """
+    Duplicate test object.
+    """
     bpy.ops.object.select_all(action="DESELECT")
     bpy.context.view_layer.objects.active = test_object
 
@@ -101,13 +179,28 @@ def duplicate_test_object(test_object):
     evaluated_object.name = "evaluated_object"
     return evaluated_object
 
+
 def main():
-    test_object, expected_object = get_objects()
+    """
+    Main function controlling the workflow and running the tests.
+    """
+    argv = sys.argv
+    try:
+        command = argv[argv.index("--") + 1:]
+        for cmd in command:
+            if cmd == "--first-time":
+                run_first_time()
+                break
+    except:
+        # If no arguments were given to Python, run normally.
+        pass
+
+    test_object = get_test_object()
+    expected_object = get_expected_object()
     evaluated_object = duplicate_test_object(test_object)
     evaluated_object = apply_modifier(evaluated_object)
     compare_mesh(evaluated_object, expected_object)
 
 
-
 if __name__ == "__main__":
-    main()
\ No newline at end of file
+    main()



More information about the Bf-blender-cvs mailing list