Dynamic loading of multiple scenes?

I’m busy working on a LOD system…but when I use the “LibLoad” function it seems to load all the scenes in that blend file…where I want it to load only specific scenes.

bge.logic.LibLoad(blend, type, data)


How do I tell it to load a specific scene in that blend file?

There currently is no way to specify a single scene. There once was a patch that added this feature, but it’s old and outdated.

Thanks for the reply!
Mmm…that is a bit of a mission. That would mean that I would have to create a scene for every object(or group of objects) that I want to load in.

Are there perhaps other ways to load libraries/objects in and out? Also, do you maybe know where that patch is…maybe I/somone could revive it?

Well, from an organizational standpoint it’s not a terrible idea to use one blend per object anyways.

For what i can say to you from my experience, the best way to store object data is with .obj files. They are smaller and can be modified by whatever program. If you look at the example used by ideasman42 while presenting a video using the Dynamic Loading feature he implemented you’ll understand what i mean.
Here the script (it’s outdated, and i haven’t corrected it to work properly, but i’ll do in a few time)

def import_obj_file(file, data_name="MyMesh"):

    verts, faces = [], []
    for line in file.readlines():

        words = line.split()

        if len(words) == 0 or words[0].startswith(b'#'):

        elif words[0] == b'v':
            verts.append( (float(words[1]), float(words[2]), float(words[3])) )

        elif words[0] == b'f':
            faces.append( [int(vi)-1 for vi in words[1:]] )

    # create the mesh

    import bpy

    def flatten_list(list_of_tuples):

        return [i for t in list_of_tuples for i in t]

    def unpack_face_list(list_of_tuples):

        l = []

        for t in list_of_tuples:

            if len(t)==3:
                l.extend([t[0], t[1], t[2], 0])

                l.extend([t[0],t[1],t[2],0,  t[0],t[2],t[3],0])

        return l

    verts_flat = flatten_list(verts)
    faces_flat = unpack_face_list(faces)

    me= bpy.data.meshes.new(data_name)

    me.add_geometry(len(verts), 0, int(len(faces_flat)/4))

    me.verts.foreach_set("co", verts_flat)

    me.faces.foreach_set("verts", faces_flat)


    me.name = data_name

    print ("  ", data_name, "faces:", len(me.faces))

def import_obj_gz(path, data_name):

    import_obj_file(gzip.open(path, 'r'), data_name) # for non gz open(path, 'r')

import gzip, os, GameLogic

sce = GameLogic.getCurrentScene()

own = GameLogic.getCurrentController().owner

dummy = sce.objectsInactive["DUMMY"]

path = GameLogic.expandPath("//") + "/"

def main():

    OBJ_INDEX = GameLogic.OBJ_INDEX = getattr(GameLogic, 'OBJ_INDEX', -1) + 1


    if OBJ_INDEX == 0:

        OBJ_FILES = GameLogic.OBJ_FILES = [f for f in os.listdir(path) if f.endswith('.gz')]


        OBJ_FILES.reverse() # smallest first.


        OBJ_FILES = GameLogic.OBJ_FILES

    if OBJ_INDEX < len(OBJ_FILES):

        f = OBJ_FILES[OBJ_INDEX]

        id = f.split('.')[0]

        import_obj_gz(path+f, id)

        ob = sce.addObject(dummy, own)

        # ob.localPosition.x += OBJ_INDEX*0.1

        ob.replaceMesh(GameLogic.LibNew(f, 'Mesh', [id])[0])

        m = ob.meshes[0]

        print(m.numPolygons, m.getVertexArrayLength(0))


        if m.numPolygons:






        if GameLogic.LibFree(OBJ_FILES[OBJ_INDEX]):

            print("  ", "freeing:", OBJ_FILES[OBJ_INDEX])


            GameLogic.OBJ_INDEX = -1 # none left, restart

With the script he reads from the obj file the data about vertices and faces, and then creates the mesh (Just look at the obj_importer script to get an idea of how this can be done, even loading the material) and creates a new Lib. Then uses this new lib to assign the new mesh to a “dummy” object… May be a bit tricky, but i found it’s the best way to handle lots of objects…

@Captain Oblivion: Yea…I also thought that it’s going to be the best way…although I’m trying to work with one main file(which is referencing multiple objects) which then gets saved out into multiple .blend files named according to the grid space. The one place I’m struggling is reading “Groups” or amount of “Groups” from the main .blend file via python?
(I basically want to make a “for loop” that counts amount of groups in external file and then makes a .blend file of each group based on the amount of groups)

@Makers_F: I’m not sure how that would work with the BGE? Also .obj files only save 1 UV layout and also not the materials. Unless I’m understaning you wrong?

Anyone have ideas on how to read the amount of groups for an external .blend file??? (not the BGE python, but for normal Blender python)

When blender exports a mesh (or scene) in obj format, it also export a .mtl file that contains the material. Btw mine was only a way, if you feel you are limited by the 1uv, then try something different :slight_smile:

Here the post i mentioned above

and the video

The problem with .obj and .mtl is it doesn’t save the logic as well, which in a game is pretty much the most important part.