test_mesh_simple.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env python3
  2. # ##### BEGIN GPL LICENSE BLOCK #####
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software Foundation,
  16. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. #
  18. # ##### END GPL LICENSE BLOCK #####
  19. #
  20. # Call as follows:
  21. # python collada_mesh_simple.py --blender PATH_TO_BLENDER_EXE --testdir PATH_TO_SVN/lib/tests/collada/mesh
  22. #
  23. import sys
  24. import bpy
  25. import argparse
  26. import functools
  27. import shutil
  28. import tempfile
  29. import unittest
  30. import difflib
  31. import pathlib
  32. from pathlib import Path
  33. def with_tempdir(wrapped):
  34. """Creates a temporary directory for the function, cleaning up after it returns normally.
  35. When the wrapped function raises an exception, the contents of the temporary directory
  36. remain available for manual inspection.
  37. The wrapped function is called with an extra positional argument containing
  38. the pathlib.Path() of the temporary directory.
  39. """
  40. @functools.wraps(wrapped)
  41. def decorator(*args, **kwargs):
  42. dirname = tempfile.mkdtemp(prefix='blender-collada-test')
  43. #print("Using tempdir %s" % dirname)
  44. try:
  45. retval = wrapped(*args, pathlib.Path(dirname), **kwargs)
  46. except:
  47. print('Exception in %s, not cleaning up temporary directory %s' % (wrapped, dirname))
  48. raise
  49. else:
  50. shutil.rmtree(dirname)
  51. return retval
  52. return decorator
  53. LINE = "+----------------------------------------------------------------"
  54. class AbstractColladaTest(unittest.TestCase):
  55. @classmethod
  56. def setUpClass(cls):
  57. cls.testdir = pathlib.Path(args.testdir)
  58. def checkdae(self, reference, export):
  59. """
  60. collada verifier checks if exported dae file is the same as reference dae
  61. """
  62. ref = open(reference)
  63. exp = open(export)
  64. diff = difflib.unified_diff(ref.readlines(), exp.readlines(), lineterm='', n=0)
  65. ref.close()
  66. exp.close()
  67. diff_count = 0
  68. for line in diff:
  69. error = True
  70. for prefix in ('---', '+++', '@@'):
  71. # Ignore diff metadata
  72. if line.startswith(prefix):
  73. error = False
  74. break
  75. else:
  76. # Ignore time stamps
  77. for ignore in ('<created>', '<modified>', '<authoring_tool>'):
  78. if line[1:].strip().startswith(ignore):
  79. error = False
  80. break
  81. if error:
  82. diff_count += 1
  83. pline = line.strip()
  84. if diff_count == 1:
  85. print("\n%s" % LINE)
  86. print("|Test has errors:")
  87. print(LINE)
  88. pre = "reference" if pline[0] == "-" else "generated"
  89. print("| %s:%s" % (pre, pline[1:]))
  90. if diff_count > 0:
  91. print(LINE)
  92. print("ref :%s" % reference)
  93. print("test:%s" % export)
  94. print("%s\n" % LINE)
  95. return diff_count == 0
  96. class MeshExportTest(AbstractColladaTest):
  97. @with_tempdir
  98. def test_export_single_mesh(self, tempdir: pathlib.Path):
  99. test = "mesh_simple_001"
  100. reference_dae = self.testdir / Path("%s.dae" % test)
  101. outfile = tempdir / Path("%s_out.dae" % test)
  102. bpy.ops.wm.collada_export(
  103. filepath="%s" % str(outfile),
  104. check_existing=True,
  105. filemode=8,
  106. display_type="DEFAULT",
  107. sort_method="FILE_SORT_ALPHA",
  108. apply_modifiers=False,
  109. export_mesh_type=0,
  110. export_mesh_type_selection="view",
  111. selected=False,
  112. include_children=False,
  113. include_armatures=False,
  114. include_shapekeys=True,
  115. deform_bones_only=False,
  116. sampling_rate=0,
  117. active_uv_only=False,
  118. use_texture_copies=True,
  119. triangulate=False,
  120. use_object_instantiation=True,
  121. use_blender_profile=True,
  122. sort_by_name=False,
  123. export_transformation_type=0,
  124. export_transformation_type_selection="matrix",
  125. export_texture_type=0,
  126. export_texture_type_selection="mat",
  127. open_sim=False,
  128. limit_precision=False,
  129. keep_bind_info=False,
  130. )
  131. # Now check the resulting Collada file.
  132. if not self.checkdae(reference_dae, outfile):
  133. self.fail()
  134. if __name__ == '__main__':
  135. sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [])
  136. parser = argparse.ArgumentParser()
  137. parser.add_argument('--testdir', required=True)
  138. args, remaining = parser.parse_known_args()
  139. unittest.main(argv=sys.argv[0:1] + remaining)