In the previous part of the Panda3D series we exported the building model to the glTF format.
In this part we’ll load the model in Panda3D.
So, without further ado open the project folder in Visual Studio Code or any other editor of your choice. Here you can see all the folders and files that are in your project folder (A). Double-click the test.py file. It will open in a new tab (B):
For clarity’s sake let’s move the configuration code to a myConfig.prc file. Create the file in VSC and type in the following configuration code:
window-title Test Game
win-size 1200 675
Now, in order to load the configuration in the test.py file, modify it like so:
# File name: test.py
from direct.showbase.ShowBase import ShowBase
from panda3d.core import WindowProperties
# We'll need this to load the configuration file.
from panda3d.core import load_prc_file
# Now let's load the configuration file.
load_prc_file('myConfig.prc')
# Now we can remove the configuration code from here.
class Slugrace3D(ShowBase):
def __init__(self):
ShowBase.__init__(self)
app = Slugrace3D()
app.run()
If you run the app now, you will see just the same empty window as before:
panda3d-gltf
Panda3D doesn’t handle glTF files out of the box. We have to install the panda3d-gltf utility. You will find it on the following page:
https://github.com/Moguri/panda3d-gltf
You can use pip to install the panda3d-gltf package. So, open the terminal and run the following command:
pip install panda3d-gltf
Before we load the model in Panda3D, we need another utility, so let’s install it too.
panda3d-simplepbr
The other utility is panda3d-simplepbr. You will find it here:
https://github.com/Moguri/panda3d-simplepbr
This utility is necessary to output the textures correctly. You can install it using pip as well. Just run the following command in your terminal:
pip install panda3d-simplepbr
Loading the Model in Panda3D
With the two utilities installed, let’s load our model in Panda3D.
In order to load a model we use the loadModel method on the Panda3D built-in loader object. As the argument we have to pass the path to the glTF file.
When the model is loaded, it appears at the point (0, 0, 0), so at the same location as the camera. This is why we won’t see it unless we move it to a different position. To reposition an object we use the setPos method and pass the X, Y and Z coordinates as arguments to it.
And there’s one more thing. We’re going to talk about it in more detail a bit later, but for now it’s enough to say that for a model to be seen at all, it must be parented to another Panda3D built-in object, render. To do that we use the reparentTo method on the model and pass render as the argument.
So, here’s the code:
# File name: test.py
from direct.showbase.ShowBase import ShowBase
from panda3d.core import WindowProperties
from panda3d.core import load_prc_file
load_prc_file('myConfig.prc')
class Slugrace3D(ShowBase):
def __init__(self):
ShowBase.__init__(self)
# Let's load the building model and save it as self.building.
self.building = loader.loadModel("models/building/building.gltf")
# Let's position the building further away from the camera.
# To this end we must set the Y argument to a positive number.
self.building.setPos(0, 50, 0)
# Finally, let's parent the building model to render so that we
# can see it.
self.building.reparentTo(render)
app = Slugrace3D()
app.run()
As we installed the panda3d-gltf utility, we automatically added support for glTF files to the loader object. If you now run the app, you will see the following:
So, looks like the model is there, but we don’t see the textures. We’re going to fix this in a minute, but first try navigating in the game window. Panda3D automatically enables that.
Use the left mouse button to pan, the middle mouse button to orbit and the right mouse button to zoom in and out:
But what about the textures? In order to see them, we must import simplepbr and then inside the __init__ method of the app class (the one that inherits from ShowBase) call its init method (watch the name – no underscores):
# File name: test.py
from direct.showbase.ShowBase import ShowBase
from panda3d.core import WindowProperties
from panda3d.core import load_prc_file
# We need to import simplepbr.
import simplepbr
load_prc_file('myConfig.prc')
class Slugrace3D(ShowBase):
def __init__(self):
ShowBase.__init__(self)
# We must call simplepbr's init method here.
simplepbr.init()
self.building = loader.loadModel("models/building/building.gltf")
self.building.setPos(0, 50, 0)
self.building.reparentTo(render)
app = Slugrace3D()
app.run()
Now if you run the app, you should be able to see your fully textured model in the window:
Go ahead and use your mouse buttons to pan, orbit and zoom:
Or like so:
Now that we know how to load models in Panda3D, we’ll be testing all our other models right away. Then we can tweak the models if necessary and export them again. In the following parts of the series we’ll have a look at the other models that we need in our game.