Skip to content

Commit

Permalink
Closes #544, a surprisingly annoying unit test to update. And this me…
Browse files Browse the repository at this point in the history
…ans we're beta.2 ready! Right?
  • Loading branch information
tngreene committed Jun 21, 2020
1 parent 2eadc4c commit 409dcff
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
8 changes: 8 additions & 0 deletions io_xplane2blender/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,14 @@ def exportExportableRoot(

return out

@staticmethod
def get_XPlane2Blender_log_content()->List[str]:
"""
Returns the content of the log file after export as a collection of lines, no trailing new lines,
or KeyError if the text block doesn't exist yet (rare).
"""
return [l.body for l in bpy.data.texts["XPlane2Blender.log"].lines]


class XPlaneAnimationTestCase(XPlaneTestCase):
def setUp(self):
Expand Down
4 changes: 2 additions & 2 deletions io_xplane2blender/tests/test_creation_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,12 +623,12 @@ def make_root_unexportable(

if isinstance(exportable_root, bpy.types.Collection):
exportable_root.xplane.is_exportable_collection = False
# This is actually talking about "Visibile In Viewport" - the little eyeball
# This is actually talking about "Visible In Viewport" - the little eyeball
all_layer_collections = {lc.name: lc for lc in xplane_helpers.get_layer_collections_in_view_layer(view_layer)}
all_layer_collections[exportable_root.name].hide_viewport = True
elif isinstance(exportable_root, bpy.types.Object):
exportable_root.xplane.isExportableRoot = True
# This is actually talking about "Visibile In Viewport" - the little eyeball
# This is actually talking about "Visible In Viewport" - the little eyeball
exportable_root.hide_set(disable_viewport)
else:
assert False, "How did we get here?!"
Expand Down
36 changes: 18 additions & 18 deletions io_xplane2blender/xplane_types/xplane_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,34 @@ def createFileFromBlenderRootObject(potential_root:PotentialRoot, view_layer:bpy
Raises ValueError when exportable_root is not marked as exporter or something
prevents collection
"""
nested_errors: Set[str] = set()
if not xplane_helpers.is_exportable_root(potential_root, view_layer):
raise NotExportableRootError(f"{potential_root.name} is not an exportable root")
def log_nested_roots(potential_roots: List[PotentialRoot]):
err = "Exportable Roots cannot be nested, make '{}' a regular {} or ensure it is not in an exportable collection and none of its parents are exportable objects"
raise NotExportableRootError(f"{potential_root.name} is not a root")
nested_roots: Set[PotentialRoot] = set()
def find_nested_roots(potential_roots: List[PotentialRoot]):
nonlocal nested_roots
if isinstance(potential_root, bpy.types.Collection):
get_name = lambda r: r.name
nested_errors.update(
err.format(obj.name, "Collection")
for obj in filter(
lambda o: xplane_helpers.is_exportable_root(o, view_layer),
sorted(potential_root.all_objects, key=get_name)))
nested_roots.update(
o for o in potential_root.all_objects
if xplane_helpers.is_exportable_root(o, view_layer)
)

for child in potential_roots:
if xplane_helpers.is_exportable_root(child, view_layer):
nested_errors.add(err.format(child.name, "Object"))
log_nested_roots(child.children)
nested_roots.update({child})
find_nested_roots(child.children)

log_nested_roots(potential_root.children)
for error in sorted(nested_errors):
logger.error(error)
find_nested_roots(potential_root.children)
if nested_roots:
names = [f"'{potential_root.name}'"] + [f"'{r.name}'" for r in nested_roots]
logger.error(f"Nested roots found below '{potential_root.name}'. Checkmark only one of these as the Root: {', '.join(names)}")

#Name change, we're now confirmed exportable!
exportable_root = potential_root
layer_props = exportable_root.xplane.layer
filename = layer_props.name if layer_props.name else exportable_root.name
xplane_file = XPlaneFile(filename, layer_props)
xplane_file.create_xplane_bone_hierarchy(exportable_root)
assert xplane_file.rootBone, "Root Bone was not assaigned during __init__ function"
xplane_file.create_xplane_bone_hiearchy(exportable_root)
assert xplane_file.rootBone, "Root Bone was not assigned during __init__ function"
return xplane_file


Expand Down Expand Up @@ -129,7 +129,7 @@ def __init__(self, filename:str, options:xplane_props.XPlaneLayer)->None:
self.header = XPlaneHeader(self, 8)


def create_xplane_bone_hierarchy(self, exportable_root:ExportableRoot)->Optional[XPlaneObject]:
def create_xplane_bone_hiearchy(self, exportable_root:ExportableRoot)->Optional[XPlaneObject]:
def allowed_children(parent_like:Union[bpy.types.Collection, bpy.types.Object])->List[bpy.types.Object]:
"""
Returns only the objects the recurse function is allowed to use.
Expand Down
31 changes: 27 additions & 4 deletions tests/xplane_types/xplane_file/bad_nested_exportable_roots.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,46 @@

import bpy
from io_xplane2blender import xplane_config, xplane_helpers
from io_xplane2blender.xplane_helpers import XPlaneLogger, logger
from io_xplane2blender.tests import *

__dirname__ = os.path.dirname(__file__)


class TestBadNestedExportableRoots(XPlaneTestCase):
def test_nested_exportable_roots_caught(self)->None:
def reset():
test_creation_helpers.delete_all_text_files()
logger.clear()
logger.addTransport(XPlaneLogger.ConsoleTransport())
logger.addTransport(XPlaneLogger.InternalTextTransport())
# This test uses an extremely stupid hard-coded brittle method of counting errors,
# It is literally counting the ','s that appears in the relevant error message's
# list of "Checkmark only one of these" collections and object to fix
for i in range(2, -1, -1):
reset()
out = self.exportExportableRoot(bpy.data.collections[f"exportable_collection_{i}"], force_visible=False)
self.assertLoggerErrors(i)

try:
line = [l for l in XPlaneTestCase.get_XPlane2Blender_log_content() if "Nested roots found" in l][0]
except IndexError: # i == 0 should have no errors
self.assertLoggerErrors(0)
else:
self.assertEqual(line.count(","), i)

reset()
out = self.exportExportableRoot(bpy.data.collections["exportable_collection_has_nested_exportable_object"])
self.assertLoggerErrors(3)
line = [l for l in XPlaneTestCase.get_XPlane2Blender_log_content() if "Nested roots found" in l][0]
self.assertEqual(line.count(","), 3)

for i in range(2, -1, -1):
reset()
out = self.exportExportableRoot(bpy.data.objects[f"exportable_object_{i}"], force_visible=False)
self.assertLoggerErrors(i)
try:
line = [l for l in XPlaneTestCase.get_XPlane2Blender_log_content() if "Nested roots found" in l][0]
except IndexError: # i == 0 should have no errors
self.assertLoggerErrors(0)
else:
self.assertEqual(line.count(","), i)


runTestCases([TestBadNestedExportableRoots])

0 comments on commit 409dcff

Please sign in to comment.