Creating a Colored and Scaled Cube in Panda3D

  • Share this:

Code introduction


This function creates a random cube in a Panda3D application. It sets the color and size of the cube, loads the cube model, applies the material with the specified color, scales the cube, and adds it to the scene.


Technology Stack : Panda3D, ShowBase, PandaNode, Material, Vec4, loadModel

Code Type : Panda3D Application

Code Difficulty : Intermediate


                
                    
def create_random_cube(color, size):
    from direct.showbase.ShowBase import ShowBase
    from panda3d.core import PandaNode, Material, Vec4
    from panda3d.core import loadModel

    class RandomCubeApp(ShowBase):
        def __init__(self):
            ShowBase.__init__(self)

            # Load the cube model
            self.cube = loadModel("models/cube.egg")

            # Set the color of the cube
            self.material = Material()
            self.material.setShaderInput("color", Vec4(color))
            self.cube.setMaterial(self.material)

            # Scale the cube to the specified size
            self.cube.setScale(size)

            # Add the cube to the scene
            self.sceneNode.attachChild(self.cube)

    app = RandomCubeApp()
    app.run()