Browse Source

showbase: Create Loader before ShowBase, importable from ShowBaseGlobal

This makes it possible to load models before opening ShowBase
rdb 1 year ago
parent
commit
343ec6b367

+ 8 - 3
direct/src/showbase/Loader.py

@@ -126,7 +126,7 @@ class Loader(DirectObject):
                 yield await req
                 yield await req
 
 
     # special methods
     # special methods
-    def __init__(self, base):
+    def __init__(self, base=None):
         self.base = base
         self.base = base
         self.loader = PandaLoader.getGlobalPtr()
         self.loader = PandaLoader.getGlobalPtr()
 
 
@@ -136,8 +136,6 @@ class Loader(DirectObject):
         Loader.loaderIndex += 1
         Loader.loaderIndex += 1
         self.accept(self.hook, self.__gotAsyncObject)
         self.accept(self.hook, self.__gotAsyncObject)
 
 
-        self._loadPythonFileTypes()
-
     def destroy(self):
     def destroy(self):
         self.ignore(self.hook)
         self.ignore(self.hook)
         self.loader.stopThreads()
         self.loader.stopThreads()
@@ -229,6 +227,10 @@ class Loader(DirectObject):
         """
         """
 
 
         assert Loader.notify.debug("Loading model: %s" % (modelPath,))
         assert Loader.notify.debug("Loading model: %s" % (modelPath,))
+
+        if not self._loadedPythonFileTypes:
+            self._loadPythonFileTypes()
+
         if loaderOptions is None:
         if loaderOptions is None:
             loaderOptions = LoaderOptions()
             loaderOptions = LoaderOptions()
         else:
         else:
@@ -416,6 +418,9 @@ class Loader(DirectObject):
         a callback is used, the model is saved asynchronously, and the
         a callback is used, the model is saved asynchronously, and the
         true/false status is passed to the callback function. """
         true/false status is passed to the callback function. """
 
 
+        if not self._loadedPythonFileTypes:
+            self._loadPythonFileTypes()
+
         if loaderOptions is None:
         if loaderOptions is None:
             loaderOptions = LoaderOptions()
             loaderOptions = LoaderOptions()
         else:
         else:

+ 2 - 2
direct/src/showbase/ShowBase.py

@@ -436,9 +436,9 @@ class ShowBase(DirectObject.DirectObject):
         self.useTrackball()
         self.useTrackball()
 
 
         #: `.Loader.Loader` object.
         #: `.Loader.Loader` object.
-        self.loader = Loader.Loader(self)
+        self.loader = ShowBaseGlobal.loader
+        self.loader.base = self
         self.graphicsEngine.setDefaultLoader(self.loader.loader)
         self.graphicsEngine.setDefaultLoader(self.loader.loader)
-        ShowBaseGlobal.loader = self.loader
 
 
         #: The global event manager, as imported from `.EventManagerGlobal`.
         #: The global event manager, as imported from `.EventManagerGlobal`.
         self.eventMgr = eventMgr
         self.eventMgr = eventMgr

+ 2 - 1
direct/src/showbase/ShowBaseGlobal.py

@@ -62,7 +62,8 @@ aspect2d = render2d.attachNewNode(PGTop("aspect2d"))
 #: A dummy scene graph that is not being rendered by anything.
 #: A dummy scene graph that is not being rendered by anything.
 hidden = NodePath("hidden")
 hidden = NodePath("hidden")
 
 
-loader: Loader
+#: The global Loader instance for models, textures, etc.
+loader = Loader()
 
 
 # Set direct notify categories now that we have config
 # Set direct notify categories now that we have config
 directNotify.setDconfigLevels()
 directNotify.setDconfigLevels()