def test_create_files_from_root_objects(self): tmpDir = os.path.realpath(os.path.join(__dirname__, '../../tmp')) xplaneFile = xplane_file.createFileFromBlenderRootObject( bpy.data.objects['root_1']) out = xplaneFile.write() fh = open(os.path.join(tmpDir, 'test_export_root_objects_1.obj'), 'w') fh.write(out) fh.close() # auto filename from blender object self.assertEqual(xplaneFile.filename, 'root_1') # should contain 3 cubes self.assertEqual(len(xplaneFile.objects), 4) self.assertObjectsInXPlaneFile(xplaneFile, [ 'root_1', 'root_1_child_1', 'root_1_child_1_child', 'root_1_child_2' ]) self.assertXplaneFileHasBoneTree(xplaneFile, [ '0 Mesh: root_1', '1 Mesh: root_1_child_1', '2 Mesh: root_1_child_1_child', '1 Mesh: root_1_child_2' ]) self.assertFileOutputEqualsFixture( out, os.path.join(__dirname__, 'fixtures', 'test_export_root_objects_1.obj')) xplaneFile2 = xplane_file.createFileFromBlenderRootObject( bpy.data.objects['root_2']) out = xplaneFile2.write() fh = open(os.path.join(tmpDir, 'test_export_root_objects_2.obj'), 'w') fh.write(out) fh.close() # custom file name self.assertEqual(xplaneFile2.filename, 'custom_name') # should contain 1 cube self.assertEqual(len(xplaneFile2.objects), 1) self.assertObjectsInXPlaneFile(xplaneFile2, ['root_2']) self.assertXplaneFileHasBoneTree(xplaneFile2, ['0 Mesh: root_2']) self.assertFileOutputEqualsFixture( out, os.path.join(__dirname__, 'fixtures', 'test_export_root_objects_2.obj'))
def test_root_object_offsets_animated(self): per_obj_tests = [[ 'a cube', 'root_object_offsets_animated_a', 1, ['a cube'], ['0 Mesh: a cube'] ], [ 'b', 'root_object_offsets_animated_b', 2, ['b cube'], ['0 Empty: b', '1 Mesh: b cube'] ], [ 'c', 'root_object_offsets_animated_c', 2, ['c cube'], ['0 Empty: c', '1 Mesh: c cube'] ]] for one_obj_test in per_obj_tests: root_block = one_obj_test[0] file_stem = one_obj_test[1] obj_count = one_obj_test[2] obj_list = one_obj_test[3] bone_tree = one_obj_test[4] tmpDir = os.path.realpath(os.path.join(__dirname__, '../../tmp')) xplaneFile = xplane_file.createFileFromBlenderRootObject( bpy.data.objects[root_block]) out = xplaneFile.write() fh = open(os.path.join(tmpDir, file_stem + '.obj'), 'w') fh.write(out) fh.close() # auto filename from blender object self.assertEqual(xplaneFile.filename, file_stem) # Confirm total count and find all primitives self.assertEqual(len(xplaneFile.objects), obj_count) self.assertObjectsInXPlaneFile(xplaneFile, obj_list) # Confirm bone structure self.assertXplaneFileHasBoneTree(xplaneFile, bone_tree) self.assertFileOutputEqualsFixture( out, os.path.join(__dirname__, 'fixtures', file_stem + '.obj'))
def exportRootObject(self, root_object:Union[bpy.types.Object,str], dest:str = None)->str: ''' Returns the result of calling xplaneFile.write(), where xplaneFile came from a root object (by name or Blender data). The output can also simultaniously written to a destination ''' if isinstance(root_object,str): root_object = bpy.data.objects[root_object] xplaneFile = xplane_file.createFileFromBlenderRootObject(root_object) out = xplaneFile.write() if dest: with open(os.path.join(TMP_DIR, dest + '.obj'), 'w') as tmp_file: tmp_file.write(out) return out
def exportExportableRoot( self, potential_root: Union[xplane_helpers.PotentialRoot, str], dest: Optional[str] = None, force_visible=True, view_layer: Optional[bpy.types.ViewLayer] = None) -> str: """ Returns the result of calling xplaneFile.write() - dest is a filepath without the file extension .obj, writes result to the TMP_DIR if not None - force_visible forces a potential_root to be visible - view_layer is needed for checking if potential_root is visible, when None the current scene's 1st view layer is used If an XPlaneFile could not be made, a ValueError will bubble up """ view_layer = view_layer or bpy.context.scene.view_layers[0] assert isinstance( potential_root, (bpy.types.Collection, bpy.types.Object, str) ), f"root_object type ({type(potential_root)}) isn't allowed, must be Collection, Object, or str" if isinstance(potential_root, str): try: potential_root = bpy.data.collections[potential_root] except KeyError: try: potential_root = bpy.data.objects[potential_root] except KeyError: assert False, f"{potential_root} must be in bpy.data.collections|objects" if force_visible: xp_file = self.createXPlaneFileFromPotentialRoot( potential_root, view_layer) else: xp_file = xplane_file.createFileFromBlenderRootObject( potential_root, view_layer) out = xp_file.write() xplane_file._all_keyframe_infos.clear() if dest: with open(os.path.join(TMP_DIR, dest + '.obj'), 'w') as tmp_file: tmp_file.write(out) return out
def createXPlaneFileFromPotentialRoot( self, potential_root: Union[xplane_helpers.PotentialRoot, str], view_layer: Optional[bpy.types.ViewLayer] = None ) -> xplane_file.XPlaneFile: """ A thin wrapper around xplane_file.createFileFromBlenderObject, where the potential root temporarily is made exportable. """ potential_root = (test_creation_helpers. lookup_potential_root_from_name(potential_root) if isinstance(potential_root, str) else potential_root) view_layer = view_layer or bpy.context.scene.view_layers[0] with TemporarilyMakeRootExportable(potential_root, view_layer): xp_file = xplane_file.createFileFromBlenderRootObject( potential_root, view_layer) return xp_file
def exportRootObject(self, root_object: Union[bpy.types.Object, str], dest: str = None) -> str: ''' Returns the result of calling xplaneFile.write(), where xplaneFile came from a root object (by name or Blender data). The output can also simultaniously written to a destination ''' if isinstance(root_object, str): root_object = bpy.data.objects[root_object] xplaneFile = xplane_file.createFileFromBlenderRootObject(root_object) out = xplaneFile.write() if dest: with open(os.path.join(TMP_DIR, dest + '.obj'), 'w') as tmp_file: tmp_file.write(out) return out
def test_root_object_offsets_animated(self): per_obj_tests = [ ['a cube', 'root_object_offsets_animated_a', 1, ['a cube'], ['0 Mesh: a cube'] ], ['b', 'root_object_offsets_animated_b', 2, ['b cube'], ['0 Empty: b','1 Mesh: b cube'] ], ['c', 'root_object_offsets_animated_c', 2, ['c cube'], ['0 Empty: c','1 Mesh: c cube'] ] ] for one_obj_test in per_obj_tests: root_block = one_obj_test[0] file_stem = one_obj_test[1] obj_count = one_obj_test[2] obj_list = one_obj_test[3] bone_tree = one_obj_test[4] tmpDir = os.path.realpath(os.path.join(__dirname__, '../../tmp')) xplaneFile = xplane_file.createFileFromBlenderRootObject(bpy.data.objects[root_block]) out = xplaneFile.write() fh = open(os.path.join(tmpDir, file_stem + '.obj'), 'w') fh.write(out) fh.close() # auto filename from blender object self.assertEqual(xplaneFile.filename, file_stem) # Confirm total count and find all primitives self.assertEqual(len(xplaneFile.objects), obj_count) self.assertObjectsInXPlaneFile(xplaneFile,obj_list) # Confirm bone structure self.assertXplaneFileHasBoneTree(xplaneFile, bone_tree) self.assertFileOutputEqualsFixture( out, os.path.join(__dirname__, 'fixtures', file_stem+'.obj') )
def exportAnimationTestCase(self, name, dest): self.assertTrue(animation_file_mappings.mappings[name]) for layer in animation_file_mappings.mappings[name]: outFile = os.path.join( dest, os.path.basename( animation_file_mappings.mappings[name][layer])) print('Exporting to "%s"' % outFile) io_xplane2blender.tests.test_creation_helpers.make_root_exportable( bpy.data.collections[f"Layer {layer + 1}"]) try: xplaneFile = xplane_file.createFileFromBlenderRootObject( bpy.data.collections[f"Layer {layer + 1}"], bpy.context.scene.view_layers[0]) except xplane_file.NotExportableRootError: assert False, f"Unable to create XPlaneFile for {name} from Layer {layer + 1}" else: with open(outFile, "w") as outFile: out = xplaneFile.write() outFile.write(out)
def test_create_files_from_root_objects(self): tmpDir = os.path.realpath(os.path.join(__dirname__, '../../tmp')) xplaneFile = xplane_file.createFileFromBlenderRootObject(bpy.data.objects['root_1']) out = xplaneFile.write() fh = open(os.path.join(tmpDir, 'test_export_root_objects_1.obj'), 'w') fh.write(out) fh.close() # auto filename from blender object self.assertEqual(xplaneFile.filename, 'root_1') # should contain 3 cubes self.assertEqual(len(xplaneFile.objects), 4) self.assertObjectsInXPlaneFile( xplaneFile, [ 'root_1', 'root_1_child_1', 'root_1_child_1_child', 'root_1_child_2' ]) self.assertXplaneFileHasBoneTree( xplaneFile, [ '0 Mesh: root_1', '1 Mesh: root_1_child_1', '2 Mesh: root_1_child_1_child', '1 Mesh: root_1_child_2' ]) self.assertFileOutputEqualsFixture( out, os.path.join(__dirname__, 'fixtures', 'test_export_root_objects_1.obj') ) xplaneFile2 = xplane_file.createFileFromBlenderRootObject(bpy.data.objects['root_2']) out = xplaneFile2.write() fh = open(os.path.join(tmpDir, 'test_export_root_objects_2.obj'), 'w') fh.write(out) fh.close() # custom file name self.assertEqual(xplaneFile2.filename, 'custom_name') # should contain 1 cube self.assertEqual(len(xplaneFile2.objects), 1) self.assertObjectsInXPlaneFile( xplaneFile2, [ 'root_2' ]) self.assertXplaneFileHasBoneTree( xplaneFile2, [ '0 Mesh: root_2' ]) self.assertFileOutputEqualsFixture( out, os.path.join(__dirname__, 'fixtures', 'test_export_root_objects_2.obj') )