Next, we'll create a sky sphere writing tool that we can use to create sky sphere bam files.

  1. Open a new file in Notepad++. Type the following two lines at the top:
    import direct.directbase.DirectStart
    from pandac.PandaModules import *
    
  2. Next, we'll define a new class and its __init__ method:
    class SkySphereWriter:
    def __init__(self):
    
  3. After that, we'll load the skybox as a cubemap and also load the inverted sphere, which is a normal 1 unit sphere, except that the faces point inward instead of outward. The # sign in the first line is replaced with the numbers 0 through 5 when Panda3D loads the cubemap.
    self.texture = loader.loadCubeMap("../Images/Purple_#.png")
    self.sphere = loader.loadModel("InvertedSphere.egg")
    
  4. Now, we need to create 3D texture coordinates for the sphere. To do that, we need to make two calls on the sphere: setTexGen() and setTexProjector().
    self.sphere.setTexGen(TextureStage.getDefault(),
    TexGenAttrib.MWorldPosition)
    self.sphere.setTexProjector(TextureStage.getDefault(),
    render, self.sphere)
    
  5. The sphere is ready to have the texture applied, so we'll do that next:
    self.sphere.setTexture(self.texture)
    
  6. We can also put in some NodePath settings here so that they'll be saved in the bam file:
    self.sphere.setLightOff()
    self.sphere.setScale(1500)
    
  7. Now, we just need to write out the file, and reparent the sphere to render so that we can preview it:
    self.sphere.writeBamFile("PurpleSkySphere.bam")
    self.sphere.reparentTo(render)
    
  8. To finish up the file, we need to instantiate our class and call the run() method:
    SSW = SkySphereWriter()
    run()
    
  9. Save the file in BGP3D/Models as SkySphereWriter.py and run it.
Time for action - creating the sky sphere in Panda3D