Skip to content

Instantly share code, notes, and snippets.

@rdb rdb/test.py
Last active Jul 30, 2019

Embed
What would you like to do?
Custom Python loader plug-in example for Panda3D
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
You can’t perform that action at this time.