Loading a model by creating a seperate py file

Sure, but you can do similar with a “common” file–and that provides a means of access to other things that you might often use, too.

Like so:

class Common
    game = None
    
    @staticmethod
    def someCommonOperation(whateverDataItUses):
        # Logic here

The main program then sets these things as appropriate:

from direct.showbase.ShowBase import ShowBase
from Common import Common

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

        Common.game = self

        self.player = player

Which can then likewise be used as called for–and gives access to more than just one method:

from Common import Common

class SomeClass():
    def __init__(self):
        self.model = Common.game.loader.loadModel("aModelFile")

        self.health = Common.game.player.health * 2

And so on.

But why do I need a ShowBase?

I do not think that this will be appreciated by beginners, and so there is an opinion that the panda API is incomprehensible and complex.

You don’t! If you, personally, prefer to code in another way, then that’s okay!

I’m not arguing that you should code that way. I’m arguing that coding that way is valid and not as problematic as you’re suggesting.

I mean, if you know the dot-syntax, chaining it doesn’t seem that much more complicated?

It’s not the most elegant code, but it’s either that or a bunch of "getGlobalPtr"s, which to my eye look less newcomer-friendly. Or the to-be-deprecated (I gather) use of global variables (i.e. just “loader.loadModel”).

hey when i tried your code initially it worked, when i bought in in my virtual environment it showed the following error.

self.panda = load_my_model.body(model)
TypeError: body() missing 1 required positional argument: 'model'

this is the modelLoader.py

class loadMyModel():
    def __init__(self):
        pass

    def body(self, base, model):
        ob = base.loader.loadModel("Assets\Models\planet_sphere")
        ComDir = r'Assets\Models\hi_res_tex\a_' + model + ".jpg"
        ob_tex = base.loader.loadTexture(ComDir)
        ob.setTexture(ob_tex)
        return ob
    
    def astronaut(self, base, model):
        ob = base.Actor(model)

this is the trial.py

from panda3d.core import loadPrcFile
loadPrcFile("config/conf.prc")

from direct.showbase.ShowBase import ShowBase
from ModelLoader import loadMyModel

class trial(ShowBase):

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

        load_my_model = loadMyModel()
        model = "earth_daymap"
        self.main_model = load_my_model.body(model)
        self.main_model.reparentTo(render)


app = trial()
app.run()

this is happening only when I am calling for the texture and the same issue is appearing with the common method too. Is there something obvious that i am missing?

What is this?

That is the method that I created within the loadMyModel class

and the directory is correct and is working when called within the same file

A…

self.main_model = load_my_model.body(model)

replace:

self.main_model = load_my_model.body(base, model)

Yup got it, sorry if this was a silly question, I am yet to learn the core concepts of python as I am not a com sci student.

1 Like

That’s fair! I daresay that none of us knew these things to start with, after all! :slight_smile: