A model is what we call any 3D object you'll use in Panda3D. Working with and manipulating your models will likely be the bulk of how you interact with Panda3D, since they are the central way to create 3D functionality in any Term Project.
Creating and Importing Models
There are many ways to create models that can be used in Panda3D. However, Panda3D only accepts models in EGG (
.egg) format, so be sure to only use that file type. You can find pre-existing models on the internet, or even create your own.
In order to create your own models, we recommend using Blender, along with the extension YABEE. Blender has its own complicated functionality, but you can use it to make 3D objects and export them to a format compatible with Panda3D.
Loading and Rendering Models
Now that you have the model file that you want, we need to somehow add it into our 3D scene that we made in the last section! Let's say that you have a model called "
coolCat.egg", and it's saved in the file path "
models/cats/coolCat.egg". You need the following code:
class myApp(ShowBase): def __init__(self): ShowBase.__init__(self) self.cat = loader.loadModel("models/cats/coolCat.egg") self.cat.reparentTo(render)
Make sure to add this code into the init method from your class you made in the last section! This ensures that the class, or your actual app, will be able to use the model as a variable: in our case,
loader.loadModel simply loads the egg file into your program, and allows you to use it later. However, it does not actually make it appear on the screen.
reparentTo(render) actually connects the model to your 3D scene, effectively adding it into the 3D world you've created. You can think of render as a high level node that everything in the scene graph is connected to. If you want anything to show up, you need it to be connected to the render somehow.
Let's say you wanted to give our cat model some cool accessories. To do this, load in your hat model and reparent it to the cat instead of to the render.This reparents the model to an existing model rather than to the global render. Reparenting means if you wanted to make the cat move around, the hat will automatically move with it! If you hadn't connected them, you would have had to move both the cat and the hat to get the proper effect.
Now your two models are connected! Reparenting can be very useful if you have lots of models working together, especially with animations. The following code demonstrates this:
self.cat = loader.loadModel("models/cats/coolCat.egg") self.cat.reparentTo(render) self.hat = loader.loadModel("models/accessories/hat.egg") self.hat.reparentTo(self.cat)
Models usually aren't very fun if you don't have any colors or textures on them. One way to add color/texture is when you're making the 3D model in the first place (ie in Blender). There are many ways to do this while initially creating your model, so we won't go over it here.
However, if you want, Panda3D lets you apply textures to models after they've been loaded into your program! Any
.png file can be a texture, so this is a good technique if you want a model to have a photo on its surface, instead of just colors. The following code is used to apply textures:
myTexture = loader.loadTexture("textures/myCoolTexture.png") self.cat.setTexGen(TextureStage.getDefault(), TexGenAttrib.MWorldPosition) self.cat.setTexture(myTexture)
Let's say that we want to apply the picture "myCoolTexture.png" onto the cat model we made above. Again, we need to load the texture into our program, which is what the
loader.loadTexture line is for. The next 2 lines of code will essentially connect the texture you've created to the model you want to apply it to.
Whenever you load and render a model into the 3D scene, it appears in a default location. This isn't always ideal, since you might not want that object to be there! There are two ways you can statically move around a model.
self.cat.setPos(x, y, z)
Anywhere in your program, you can use the
setPos method to set the location of any model to your desired (x, y, z) point.
self.cat.setHpr(h, p, r)
Similarly to the
setPos method, the
setHpr method allows you to change the orientation of any model to a desired (h, p, r) point. If you're still confused about HPR, go back to the intro to learn about it!