LibLoad

Hello,

I have several questions about LibLoad. When I load a blend file using logic.LibLoad(lib, ‘Scene’) I’m able to add the objects of that blend file, using logic.getCurrentScene().addObject(). But I’m not able to use KX_GameObject.replaceMesh(). This only works when I load a blend using logic.LibLoad(lib, ‘Mesh’)

So I thought if I’ld do it like this, it would work:

from bge import logic

def loadLib():
    
    libName = 'Lib'
    lib = logic.expandPath('//' + libName + '.blend')
    logic.LibLoad(lib, 'Scene')
    logic.LibLoad(lib, 'Mesh')

But it doesn’t, because I can load a blend file only once.
The Console says: ValueError: blend file already open “D:…”

So anyone knows how I could get the meshes once I’ve loaded with type ‘Scene’?

All loaded meshes are available to replace mesh.

I think you simply have a naming problem of your meshes. I bet most of your meshes have names like: cube.* or plane.* ;).

Better use references than names or at least ensure mesh names are unique.

Thanks for your response, Monster.

I made sure it couldn’t be a naming problem. I guess you’ll have to see for yourself.

logic.LibLoad(’*.blend’, ‘Mesh’)KX_Scene.addObject() --> NO
KX_GameObject.replaceMesh() --> YES

logic.LibLoad(’*.blend’, ‘Scene’)KX_Scene.addObject() --> YES
KX_GameObject.replaceMesh() --> NO

Attachments

Libload00.zip (143 KB)

Try this code:



from bge import logic

def load():
    
    libName = 'Library'
    lib = logic.expandPath('//' + libName + '.blend')
    scene = logic.LibLoad(lib, 'Scene')
    
    
    
def replace(cont):
    
    if not cont.sensors[0].positive:
        return
    
    scene = logic.getCurrentScene()
    cont.owner.replaceMesh(findMesh('Table'))
    
def findMesh(meshName, objects=None):
    if objects is None:
        scene = logic.getCurrentScene()
        mesh = findMesh(meshName, scene.objects)
        if mesh:
            return mesh
        
        mesh = findMesh(meshName, scene.objectsInactive)
        if mesh:
            return mesh
        return
      
    for obj in objects:
        if not obj.meshes:
            continue
        mesh = obj.meshes[0]
        if mesh.name == meshName:
            return mesh
    

I can remember there was/is a BUG regarding mesh names.

@ CTBM: Thanks, I already had seen this thread. But nothing in it shows this issue I have.

@ Monster: Thanks, I guessed it was a bug, because loading the KX_GameObject normally should also load the KX_MeshProxy. Luckily we can get it using KX_Scene.objectsInactive. I forgot about this function, so thanks a lot!

Also, how could I report this bug? I haven’t done this before.

EDIT: already figured it out
EDIT2: can’t finalize my account for some reason, so couldn’t add this to the BugTracker. Maybe someone else could do it in my stead?

I remember now having this issue a little ago.

what i do is load the Scene. using libload. i only need the object name to get the meshname. and call it through the object.



logic.LibLoad(Path + File,"Scene", load_actions=True, load_scripts=True)

NewMeshObj = Scene.objects[NewMeshObject]
Obj.replaceMesh(NewMeshObj.meshes[0], True, False)


using the meshes property you can just select the first one from the objects meshlist to get the name of the mesh.

CTBM

Thanks CTBM.

I have another question about LibLoad. Maybe someone can answer it.

In a nutshell, for my project I want to add objects from a blend file into two active scenes. But when I’ve loaded the blend file into one scene, naturally I’m not able to add its objects to the other scene. To add an object to a scene it has to be on an inactive Layer in that same scene. And of course if I try to load the blend file in the other scene I can’t because you can load it only once.

A solution would be to make a copy of the blend file, just for the purpose of being able to get its contents. Is there no other way?

EDIT: could I add objects using bge.types.KX_SCA_AddObjectActuator(SCA_IActuator) and fill in the object field? I’m gonna try that.
EDIT2: doesn’t work either

yeh i was gunna suggest using Links aswell. The problem is libLoad is too basic in its current form (Transferring Lights) to be a total solution. and links have the problem of not being able to be loaded and freed in realtime. for a balance its best to use both and if anything that links or libload don’t solve its just a case of appending it to your current Scene.

Glad you solved your problem.