Last active
July 30, 2019 10:50
-
-
Save rdb/cb3c2f4a98ce371c722e3f297b445153 to your computer and use it in GitHub Desktop.
Custom Python loader plug-in example for Panda3D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from direct.showbase.ShowBase import ShowBase | |
from panda3d.core import * | |
# Use this import to make sure you support the VFS | |
from direct.stdpy.file import open | |
class SmurfLoader: | |
# Human-readable name of the format | |
name = "Smurf" | |
# Supported extensions, without leading dot. | |
# The primary extension should come first. | |
extensions = ['smurf', 'smrf'] | |
# Set this to True if you use the methods on VirtualFileSystem with | |
# auto_unwrap=True to automatically unwrap .gz/.pz files. | |
supports_compressed = False | |
@staticmethod | |
def load_file(path, options, record=None): | |
fp = open(path, 'rb') | |
# Any raised exception will count as a load failure | |
if fp.read(4) != b'smrf': | |
raise RuntimeError("Not a valid smurf file!") | |
# Mark dependent files as you encounter them (for proper caching) | |
if record: | |
record.add_dependent_file(referenced_image_path) | |
# We may return any PandaNode, but ModelRoot is best | |
root = ModelRoot("loaded model") | |
return root | |
# Optional; but don't define it if you don't support saving | |
#@staticmethod | |
#def save_file(path, options, node): | |
# open(path).write('stuff') | |
# To register it explicitly: | |
registry = LoaderFileTypeRegistry.get_global_ptr() | |
registry.register_type(SmurfLoader) | |
# To have Panda import it from an external package, put this in your metadata: | |
#entry_points={"panda3d.loaders": ["smurf = my_package.my_module:SmurfLoader"]} | |
# Happy loading! | |
base = ShowBase() | |
model = loader.loadModel('model.smurf') | |
model.reparent_to(base.render) | |
model.ls() | |
base.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment