Loader.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """Loader module: contains the Loader class"""
  2. from PandaModules import *
  3. from DirectNotifyGlobal import *
  4. class Loader:
  5. """Loader class: contains method to load models, sounds and code"""
  6. notify = directNotify.newCategory("Loader")
  7. # special methods
  8. def __init__(self, base):
  9. """__init__(self)
  10. Loader constructor"""
  11. self.__base = base
  12. self.__loader = PandaLoader()
  13. self.__texturePool = TexturePool()
  14. self.__modelPool = ModelPool()
  15. self.__audioPool = AudioPool()
  16. # model loading funcs
  17. def loadModel(self, modelPath):
  18. """loadModel(self, string)
  19. Attempt to load a model from given file path, return
  20. a nodepath to the model if successful or None otherwise."""
  21. Loader.notify.info("Loading model: %s" % (modelPath) )
  22. node = self.__loader.loadSync(Filename(modelPath))
  23. if (node != None):
  24. nodePath = self.__base.hidden.attachNewNode(node)
  25. else:
  26. nodePath = None
  27. return nodePath
  28. def loadModelOnce(self, modelPath):
  29. """loadModelOnce(self, string)
  30. Attempt to load a model from modelPool, if not present
  31. then attempt to load it from disk. Return a nodepath to
  32. the model if successful or None otherwise"""
  33. Loader.notify.info("Loading model once: %s" % (modelPath))
  34. node = self.__modelPool.loadModel(modelPath)
  35. if (node != None):
  36. nodePath = self.__base.hidden.attachNewNode(node)
  37. else:
  38. nodePath = None
  39. return nodePath
  40. def loadModelCopy(self, modelPath):
  41. """loadModelCopy(self, string)
  42. Attempt to load a model from modelPool, if not present
  43. then attempt to load it from disk. Return a nodepath to
  44. a copy of the model if successful or None otherwise"""
  45. Loader.notify.info("Loading model copy: %s" % (modelPath))
  46. # utilize load once goodness
  47. nodePath = self.loadModelOnce(modelPath)
  48. if (nodePath != None):
  49. return (nodePath.copyTo(self.__base.hidden))
  50. else:
  51. return None
  52. # texture loading funcs
  53. def loadTexture(self, texturePath):
  54. """loadTexture(self, string)
  55. Attempt to load a texture from the given file path using
  56. TexturePool class. Returns None if not found"""
  57. Loader.notify.info("Loading texture: %s" % (texturePath) )
  58. texture = self.__texturePool.loadTexture(Filename(texturePath))
  59. return texture
  60. # sound loading funcs
  61. def loadSample(self, samplePath):
  62. """loadSample(self, string)
  63. Attempt to load a sound from the given file path using
  64. Cary's sound class. Returns None if not found"""
  65. Loader.notify.info("Loading sound: %s" % (samplePath) )
  66. sound = self.__audioPool.loadSample(samplePath)
  67. return sound
  68. def loadMusic(self, musicPath):
  69. """loadMusic(self, string)
  70. Attempt to load music from the given file path using
  71. Cary's sound class. Returns None if not found"""
  72. Loader.notify.info("Loading music: %s" % (musicPath) )
  73. music = self.__audioPool.loadMusic(musicPath)
  74. return music