Model Files — Panda3D Manual (2024)

The most common way to put geometry in the scene is by loading it from a file.A model file contains a tree of nodes, similar to a scene graph, under whichthe actual geometry is stored that Panda3D can render to the screen.

Model files can contain static geometry, but they can also contain informationfor animating the model, as well as information about the model’s material, ie.what color the material has, and how this color changes under the influence oflighting.

Panda does not distinguish between big stuff and small stuff. For example, if avirtual world contains a coffee cup on a table, and the table is in the middleof a small island, then the coffee cup, the table, and the island are allmodels: each is a piece of static non-animated geometry. A model file couldalso contain the entire island with all the smaller models included within it.

Panda3D does distinguish between animated and non-animated geometry, however.“Animated” in this sense means that the geometry changes shape; a flying ballwouldn’t be considered animated, because it stays a sphere. Animated models areexplained further in the Animated Models section; this section justexplains how to load non-animated geometry.

Loading a model from a file isn’t the only way to put geometry on the screen.Many engines provide tools to create terrain, and store that terrain intoheightmap images. Panda3D can generate geometry for a kind of heightmap terrain;refer to the Terrain section for more information. For many simplescenes, though, it is simpler to use a static model loaded from a file ratherthan a dynamically generated heightmap terrain.

File Formats

Models can be stored in one of a number of file formats. Panda3D’s own nativeformat for storing models is EGG. This is a human-readable format containinga textual description of the geometry and its animations and materials.Panda3D provides various tools that can convert model files from other formatsto the EGG format, and manipulate EGG files in various ways.

The other native format of Panda3D is the BAM format, which is a binaryrepresentation of the internal object structure of Panda3D. As such, it is veryefficient to load. Therefore, it is also the format of choice when shipping thegame to an end-user. Panda3D will automatically convert your models to BAM forcaching purposes, or when packaging a finished game for distribution.

There are a range of plug-ins available to load models in other formats, suchas the glTF format, which is a widely-used format in theindustry. See Supported Model Formats for more information aboutsupported formats.

Loading a Model File

You can load a model file using the following code:

myNodePath = loader.loadModel("path/to/models/myModel.egg")

This call can take an absolute or relative path, although it is stronglyrecommended to use relative paths only. These relative paths are resolvedusing the “model path”, which is set by default to the path of the mainPython file. Do note that the path always uses forward slashes, even onWindows. See Panda Filename Syntax for more information about filenames.

The first time you make this call for a particular model, that model is read andsaved in a table in memory; on each subsequent call, the model is simply copiedfrom the table, instead of reading the file.

This call returns a NodePath object representing the root of themodel’s tree of nodes. This object is used to manipulate the model further andplace it into the scene graph.

Note

In many examples, you will see that the extension is omitted. In this case,Panda3D will automatically look for a file with the .egg extension.

Placing the Model in the Scene Graph

The most important manipulation is to change the parent of a node. A model is bydefault loaded without a parent, but it needs to be placed into an active scenegraph so that Panda3D will be able to find the model’s geometry and render it tothe screen.

The default 3D scene graph is called render, and this is how toreparent the model to this scene graph:

myModel.reparentTo(render)

It is possible to reparent the model to any node (even to another model, or toa sub-part of a different model), not just to render!What’s important is that it is parented to a node that is itself parented to ascene graph, so that Panda3D can find it. Otherwise, the model will remaininvisible.

The converse is to remove a model from the scene graph, which can be done asfollows:

myModel.detachNode()

The Model Cache

The first time you load an EGG file, it loads slowly. However, the second timeyou use that same EGG file, it loads fast. This is possible because Panda3D isquietly translating the EGG file into a performance-optimized BAM file. Itstores these BAM files in a directory called the model cache. The next timeyou try to load the EGG file, if it has not been modified on disk, Panda3D willload the corresponding optimized BAM file from the model cache instead.

Where this cache is stored depends on your operating system. On Windows, it isusually in C:\Users\YourUser\AppData\Local\Panda3D-1.10, whereas onLinux, it can be found in ~/.cache/panda3d. The location can be controlledusing the model-cache-dir variable in yourConfig.prc file, or disabled by setting thisvariable to an empty string.

You can alternatively force a model to bypass the model cache by passing thenoCache=True argument to the loader.loadModel call.

Compressed Models

Because EGG files are text-based, they can get rather large in size. It is oftendesirable to store them in a compressed fashion so that they take up less space.This can be done using the pzip utility that ships with Panda3D:

pzip model.egg

That will turn it into a file called “model.egg.pz”, which will be considerablysmaller. Panda3D will be able to load this model without any extra steps.

To undo this step and return it to its original form, just run it through the“punzip” utility.

Loading Models Asynchronously

When loading very large models, it can sometimes take some time before the modelhas finished loading. If this is done while the user is interacting with theprogram, it generates an undesirable lag. To alleviate this, Panda3D can loadmodels in the background, without disrupting the user experience.

See Asynchronous Loading for more information about these techniques. However,they are advanced techniques and if you are still learning Panda3D it isrecommended to revisit this later, when optimizing your program.

Model Files — Panda3D Manual (2024)
Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 6161

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.