Home » Panda3D Part 14 – Loading glTF Objects in Panda3D
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
Learn how to make beautiful GUI apps
in Python using the Kivy framework.
Comprehensive, for Kivy beginners, easy to follow.
Get the book here (PDF) or on Amazon:
ebook / paperback (black and white) / paperback (full color)
Now, in order to load the configuration in the test.py file, modify it like so:
#Filename:test.py fromdirect.showbase.ShowBaseimportShowBase frompanda3d.coreimportWindowProperties #We'llneedthistoloadtheconfigurationfile. frompanda3d.coreimportload_prc_file #Nowlet'sloadtheconfigurationfile. load_prc_file('myConfig.prc') #Nowwecanremovetheconfigurationcodefromhere. classSlugrace3D(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.
Your Panda3D Magazine
Make Awesome Games and Other 3D Apps
with Panda3D and Blender using Python.
Cool stuff, easy to follow articles.
Get the magazine here (PDF).
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:
#Filename:test.py fromdirect.showbase.ShowBaseimportShowBase frompanda3d.coreimportWindowProperties frompanda3d.coreimportload_prc_file load_prc_file('myConfig.prc') classSlugrace3D(ShowBase): def__init__(self): ShowBase.__init__(self) #Let'sloadthebuildingmodelandsaveitasself.building. self.building=loader.loadModel("models/building/building.gltf") #Let'spositionthebuildingfurtherawayfromthecamera. #TothisendwemustsettheYargumenttoapositivenumber. self.building.setPos(0,50,0) #Finally,let'sparentthebuildingmodeltorendersothatwe #canseeit. 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:
Learn the basics of Python, including OOP.
with lots of exercises, easy to follow
The course is available on Udemy.
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):
#Filename:test.py fromdirect.showbase.ShowBaseimportShowBase frompanda3d.coreimportWindowProperties frompanda3d.coreimportload_prc_file #Weneedtoimportsimplepbr. importsimplepbr load_prc_file('myConfig.prc') classSlugrace3D(ShowBase): def__init__(self): ShowBase.__init__(self) #Wemustcallsimplepbr'sinitmethodhere. 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.
Learn the basics of 3D modeling in Blender.
step-by-step, easy to follow, visually rich
The course is available on Udemy and on Skillshare.