Script to load blend file according to object name

I am wondering how to make a script that uses the name of the object it is attached to, to LibLoad a blend file of the same name. I am currently using this;

from bge import logic as gl
own=gl.getCurrentScene().objects["Empty"]
path=gl.expandPath("//")
path2=path+"lib1.blend" #lib1.blend contain the 
#mesh data to be merged with current scene
print("Path for Library is:"+path2)
if own["loaded"] == 0:
    gl.LibLoad(path2,"Scene", async=True)
    own["loaded"] =1
else:
    print("Library already loaded")

How would I define path2 to point to the blend file that has the same name as the object which the script is connected to? Feel free to rewrite my method if you have another way or if I am doing something wrong.

Something like:

relativePath = “//” + own.name + “.blend”
if not absolutePath in bge.logic.LibList():
…bge.logic.LibLoad(relativePath, “Scene”, async=True, load_actions=True)

Keep in mind though that not all valid bge object names are also valid path qualifiers, so choose the names wisely. You may parse the name to replace invalid characters but it’s easier and faster to just remember it when naming stuff.

I am sorry but your formatting has me a bit confused. Can you show where to insert this in my code?

This is a complete script:

import bge
owner = bge.logic.getCurrentController().owner
relativePath = "//" + owner.name + ".blend"
hasBeenLoaded = owner.get("Loaded", False)
if hasBeenLoaded == False:
    owner["Loaded"] = True
    bge.logic.LibLoad(relativePath, "Scene", async=True,load_actions=True)

Here we suppose that “owner”, which is the object that has the python controller attached via logic brics, is the object whose name defines the blender file to load. I haven’t tested it but i use a similar method, it should work.

Thank you very much for the help. Can you show me how to alter this LibFree snippet to be compatible with your code? It was written to work with my original code.

from bge import logic as gllib=gl.LibList()
own=gl.getCurrentScene().objects["Empty"]
if own["loaded"] == 1:
    gl.LibFree(lib[0])
    own["loaded"]=0
else:
    print("No Library Found")
import bge
own = bge.logic.getCurrentController().owner
relativePath = "//" + own.name + ".blend"
libraryName = bge.logic.expandPath(relativePath)
if libraryName in bge.logic.LibList():
    bge.logic.LibFree(libraryName)

As before, this should be the python controller of the object that identifies the library with its name. It is not necessary in principle, you can use any object and any way to find it, but it is easier to rely on the bound created with logic bricks.

Here is the complete code provided by pgi

import bge
owner = bge.logic.getCurrentController().owner
relativePath = "//" + owner.name + ".blend"
hasBeenLoaded = owner.get("Loaded", False)
if hasBeenLoaded == False:
    owner["Loaded"] = True
    bge.logic.LibLoad(relativePath, "Scene", async=True,load_actions=True)

import bge
own = bge.logic.getCurrentController().owner
relativePath = "//" + own.name + ".blend"
libraryName = bge.logic.expandPath(relativePath)
if libraryName in bge.logic.LibList():
     bge.logic.LibFree(libraryName)