step2_basic_setup.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python
  2. # Author: Shao Zhang and Phil Saltzman
  3. # Last Updated: 2015-03-13
  4. #
  5. # This tutorial is intended as a initial panda scripting lesson going over
  6. # display initialization, loading models, placing objects, and the scene graph.
  7. #
  8. # Step 2: After initializing panda, we define a class called World. We put
  9. # all of our code in a class to provide a convenient way to keep track of
  10. # all of the variables our project will use, and in later tutorials to handle
  11. # keyboard input.
  12. # The code contained in the __init__ method is executed when we instantiate
  13. # the class (at the end of this file). Inside __init__ we will first change
  14. # the background color of the window. We then disable the mouse-based camera
  15. # control and set the camera position.
  16. # Initialize Panda and create a window
  17. from direct.showbase.ShowBase import ShowBase
  18. base = ShowBase()
  19. from panda3d.core import * # Contains most of Panda's modules
  20. from direct.gui.DirectGui import * # Imports Gui objects we use for putting
  21. # text on the screen
  22. import sys
  23. class World(object): # Our main class
  24. def __init__(self): # The initialization method caused when a
  25. # world object is created
  26. # Create some text overlayed on our screen.
  27. # We will use similar commands in all of our tutorials to create titles and
  28. # instruction guides.
  29. self.title = OnscreenText(
  30. text="Panda3D: Tutorial 1 - Solar System",
  31. parent=base.a2dBottomRight, align=TextNode.A_right,
  32. style=1, fg=(1, 1, 1, 1), pos=(-0.1, 0.1), scale=.07)
  33. # Make the background color black (R=0, G=0, B=0)
  34. # instead of the default grey
  35. base.setBackgroundColor(0, 0, 0)
  36. # By default, the mouse controls the camera. Often, we disable that so that
  37. # the camera can be placed manually (if we don't do this, our placement
  38. # commands will be overridden by the mouse control)
  39. base.disableMouse()
  40. # Set the camera position (x, y, z)
  41. camera.setPos(0, 0, 45)
  42. # Set the camera orientation (heading, pitch, roll) in degrees
  43. camera.setHpr(0, -90, 0)
  44. # end class world
  45. # Now that our class is defined, we create an instance of it.
  46. # Doing so calls the __init__ method set up above
  47. w = World()
  48. # As usual - run() must be called before anything can be shown on screen
  49. base.run()