Panda3D Part 14 – Loading glTF Objects in Panda3D - Prospero Coder (2024)

Home » Panda3D Part 14 – Loading glTF Objects in Panda3D

Spread the love


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):

Panda3D Part 14 – Loading glTF Objects in Panda3D - Prospero Coder (1)

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:

 #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 Part 14 – Loading glTF Objects in Panda3D - Prospero Coder (3)

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:

Panda3D Part 14 – Loading glTF Objects in Panda3D - Prospero Coder (5)

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:

Panda3D Part 14 – Loading glTF Objects in Panda3D - Prospero Coder (6)
Panda3D Part 14 – Loading glTF Objects in Panda3D - Prospero Coder (7)

Python Jumpstart Course

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:

Panda3D Part 14 – Loading glTF Objects in Panda3D - Prospero Coder (8)

Go ahead and use your mouse buttons to pan, orbit and zoom:

Panda3D Part 14 – Loading glTF Objects in Panda3D - Prospero Coder (9)

Or like so:

Panda3D Part 14 – Loading glTF Objects in Panda3D - Prospero Coder (10)

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.

Panda3D Part 14 – Loading glTF Objects in Panda3D - Prospero Coder (11)

Blender Jumpstart Course

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.


Spread the love

Panda3D Part 14 – Loading glTF Objects in Panda3D - Prospero Coder (2024)
Top Articles
Latest Posts
Article information

Author: Roderick King

Last Updated:

Views: 6802

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.