def test_loader_ram_cache(test_filename): """Tests that the Python loader plug-ins write to the RAM cache.""" # Ensure a clean slate. from panda3d.core import ModelPool ModelPool.release_all_models() with registered_type(DummyLoader): model1 = Loader.get_global_ptr().load_sync( test_filename, LoaderOptions(LoaderOptions.LF_no_disk_cache | LoaderOptions.LF_allow_instance)) assert model1 is not None assert model1.name == "loaded" assert ModelPool.has_model(test_filename) assert ModelPool.get_model(test_filename, True) == model1 model2 = Loader.get_global_ptr().load_sync( test_filename, LoaderOptions(LoaderOptions.LF_cache_only | LoaderOptions.LF_allow_instance)) assert model2 is not None assert model1 == model2 ModelPool.release_model(model2)
def test_loader_compressed(test_pz_filename): """Tests for loading .pz files and the supports_compressed flag.""" class TestLoader: extensions = ["test"] @staticmethod def load_file(path, options, record=None): return ModelRoot("loaded") # Test with property absent with registered_type(TestLoader): model = Loader.get_global_ptr().load_sync( test_pz_filename, LoaderOptions(LoaderOptions.LF_no_cache)) assert model is None # Test with property False, should give same result TestLoader.supports_compressed = False with registered_type(TestLoader): model = Loader.get_global_ptr().load_sync( test_pz_filename, LoaderOptions(LoaderOptions.LF_no_cache)) assert model is None # Test with property True, should work TestLoader.supports_compressed = True with registered_type(TestLoader): model = Loader.get_global_ptr().load_sync( test_pz_filename, LoaderOptions(LoaderOptions.LF_no_cache)) assert model is not None assert model.name == "loaded" # Test with property invalid type, should not register TestLoader.supports_compressed = None with pytest.raises(TypeError): LoaderFileTypeRegistry.get_global_ptr().register_type(TestLoader)
def __init__(self, input_file, output_file=None, prefabs=""): self.dir = os.path.dirname(input_file) self.depth = 0 self.cardmaker = CardMaker("image") self.cardmaker.set_frame(-0.5, 0.5, -0.5, 0.5) self.linesegs = LineSegs() self.textnode = TextNode("text") self.tilesheets = [] # Every tsx file loaded. self.tiles = {} # Every unique tile/card. self.node = NodePath("tmx_root") # load prefab models self.prefabs = {} if prefabs: loader = Loader.get_global_ptr() for prefab_node in loader.load_sync(prefabs).get_children(): prefab_node.clear_transform() self.prefabs[prefab_node.name] = NodePath(prefab_node) self.tmx = ET.parse(input_file).getroot() self.xscale = int(self.tmx.get("tilewidth")) self.yscale = int(self.tmx.get("tileheight")) self.size = [0, 0] self.load_group(self.tmx) if output_file: self.export_bam(output_file)
def test_loader_success(test_filename): """Tests that a normal dummy loader successfully loads.""" with registered_type(DummyLoader): model = Loader.get_global_ptr().load_sync( test_filename, LoaderOptions(LoaderOptions.LF_no_cache)) assert model is not None assert model.name == "loaded"
def test_loader_extensions(test_filename): """Tests multi-extension loaders.""" class MultiExtensionLoader: extensions = ["test1", "teSt2"] @staticmethod def load_file(path, options, record=None): return ModelRoot("loaded") fp1 = tempfile.NamedTemporaryFile(suffix='.test1', delete=False) fp1.write(b"test1") fp1.close() fn1 = Filename.from_os_specific(fp1.name) fn1.make_true_case() fp2 = tempfile.NamedTemporaryFile(suffix='.TEST2', delete=False) fp2.write(b"test2") fp2.close() fn2 = Filename.from_os_specific(fp2.name) fn2.make_true_case() try: with registered_type(MultiExtensionLoader): model1 = Loader.get_global_ptr().load_sync( fn1, LoaderOptions(LoaderOptions.LF_no_cache)) assert model1 is not None assert model1.name == "loaded" model2 = Loader.get_global_ptr().load_sync( fn2, LoaderOptions(LoaderOptions.LF_no_cache)) assert model2 is not None assert model2.name == "loaded" finally: os.unlink(fp1.name) os.unlink(fp2.name) # Ensure that both were unregistered. registry = LoaderFileTypeRegistry.get_global_ptr() assert not registry.get_type_from_extension("test1") assert not registry.get_type_from_extension("test2")
def test_loader_exception(test_filename): """Tests for a loader that raises an exception.""" class FailingLoader: extensions = ["test"] @staticmethod def load_file(path, options, record=None): raise Exception("test error") with registered_type(FailingLoader): model = Loader.get_global_ptr().load_sync( test_filename, LoaderOptions(LoaderOptions.LF_no_cache)) assert model is None
def test_loader_nonexistent(): """Verifies that non-existent files fail before calling load_file.""" flag = [False] class AssertiveLoader: extensions = ["test"] @staticmethod def load_file(path, options, record=None): flag[0] = True assert False, "should never get here" with registered_type(AssertiveLoader): model = Loader.get_global_ptr().load_sync( "/non-existent", LoaderOptions(LoaderOptions.LF_no_cache)) assert model is None assert not flag[0]
import sys sys.path.insert(0, "../../") from Native.RSNative import MeshSplitterWriter from panda3d.core import Loader, NodePath # TODO: Make input- and output-file command line parameters, something like: # convert.py -o output.rpsg input.bam INPUT_FILE = "Scene.bam" OUTPUT_FILE = "model.rpsg" loader = Loader.get_global_ptr() model = NodePath(loader.load_sync(INPUT_FILE)) # Flatten the transform of the model, since we don't store the transform model.flatten_strong() print("Loaded model ...") writer = MeshSplitterWriter() # Collect all geoms of the model and register them to the writer geom_nodes = model.find_all_matches("**/+GeomNode") for geom_node in geom_nodes: geom_node = geom_node.node() for geom_idx in range(geom_node.get_num_geoms()): geom = geom_node.get_geom(geom_idx) geom_state = geom_node.get_geom_state(geom_idx)