trying to addObject from a LibLoad library

This is my first post, I’m still farely new, please bear with me.

I would like to use LibLoad and addObject for my asset library. I need ‘Scene’ from LibLoad because all my assets have logic bricks and properties attached to them and these need to be transferred as well, not just the meshes.

I have managed to use a basic script for LibLoad here:

from bge import logic

path = logic.expandPath('//test_library_01.blend')
logic.LibLoad(path,'Scene')

This LibLoad script is applied to an Empty on layer2 with an Always Sensor. It works fine, when layer2 is active I can see the library loaded, no issues.

Then I use this addObject script:


import bge
from bge import logic

scene = logic.getCurrentScene()
scene.addObject("Suzanne","Empty_L1")

It is applied to empty object “Empty_L1” on layer1 with a “spacebar” Keyboard Sensor. “Suzanne” is a monkey head that is on the main .blend file in layer2 originally. It is not imported with the LibLoad. When I hit “spacebar” Suzanne is loaded from layer2 onto layer1. Again, the script works fine, no issues.

The problem is when instead of “Suzanne” I input an object name “insert_3” from the LibLoad library. Nothing happens. The console error message that I get is this:

“ValueError: scene.addObject(object, reference, time): KX_Scene (first argument), requested name “insert_3” did not match any KX_GameObject in this scene”

I am trying to get a few working scripts together for operations that I need that the logic bricks don’t provide. Then just cut/paste as needed. This one has me very stumped… Please help.

It’s important to note that the concept of layers doesn’t really apply in the game engine. There are only active and inactive objects. Objects are active if they were in a selected layer when the game engine starts. Otherwise they are inactive. For loaded libraries, objects are active if they were in a selected layer when the blend file was last saved and are inactive otherwise.

We don’t really have enough information to tell you exactly what’s wrong since you haven’t said which layers you have selected when you start the game engine and which layers were selected in the loaded library. We also need to know if the “insert_3” object is on an active layer or not. For objects to be added properly using the addObject method, they need to be on an inactive layer.

Another thing people often tend to overlook is that objects aren’t available from a loaded blend file until the frame after the LibLoad call has been made. See the note from the API:

Note Asynchronously loaded libraries will not be available immediately after LibLoad() returns. Use the returned KX_LibLoadStatus to figure out when the libraries are ready.

test_library_02.blend (428 KB)

My 2 files, the main .blend and the library .blend, are attached.

It’s important to note that the concept of layers doesn’t really apply in the game engine. There are only active and inactive objects. Objects are active if they were in a selected layer when the game engine starts. Otherwise they are inactive. For loaded libraries, objects are active if they were in a selected layer when the blend file was last saved and are inactive otherwise.

This makes sense and sheds some new light on the subject. However, after revisiting the issue I am still having the problem.

Another thing people often tend to overlook is that objects aren’t available from a loaded blend file until the frame after the LibLoad call has been made

I don’t think that this is the issue. The LibLoad script is called immediately on starting the game with an Always Sensor. And the addObject script isn’t called for seconds later with the Spacebar Keyboard Sensor.

And regarding an “asynchronous” LibLoad, the API says that the default argument is async=False. If I understand this correctly then that would mean that it wouldn’t apply. Regardless I did run tests with the argument async=False as well as async=True.

But hopefully with the .blend files you should have a bit more information. And thank you for the help, Mobious! Much appreciated! :yes:

Attachments

test_libload_12.blend (504 KB)

you need to save your assets blend on an empty layer.

i attached a libload demo similar to the setup i use.

Attachments

demo.zip (379 KB)

if you dont use async, then the library is ready for use next line. you just need to wait for the libload function to finish.

i have some pretty heavy asset libraries and they only take a second or so to finish.

Sorry guys. Like I said, I am still very new to this. I’ve been using blender for 5+ years but it’s only been in mostly modeling. Only this year did I start in on Game Dev, and I’ve only been studying Python and coding for the last 2 months.

So, Daedalus_MDW, I really appreciate you sharing with me your demo file. But the scripts in there are very advanced for me. I am struggling with deciphering them.

logic.LibLoad( (logic.expandPath("//lib.blend")), "Scene", load_actions=True, verbose=False, load_scripts=True, async=False)

Your LibLoad line here is understandable. I think I have my LibLoad working properly. I have it loading into layer2. And when I run the script while layer2 is active I can see the library loading.

But your addObject lines here:

PLROBJ = SCENE.addObject("Player", OWNER, 0)
PLRMESH = SCENE.addObject(NAME, OWNER, 0)

I don’t understand… because they are part of a large function that does many other things. And I think that maybe within all that other stuff in the SPAWN() function is what I’m missing.

Again, I’m sorry for my “newb”-ness. But I guess then that’s why I’m here. :slight_smile: And thank you, Daedalus_MDW, for all the help!

Got it! Thank you, Mobious and Daedalus_MDW!

Much like what Mobious was saying, my problem was in how I coordinated my active/inactive layers on my library.blend and my main.blend.

I had it like this: ((WRONG))
library.blend = assets on ACTIVE layer
main.blend = assets being loaded onto INACTIVE layer

What works is: ((RIGHT))
library.blend = assets on INACTIVE layer
main.blend = assets being loaded onto ACTIVE layer

Thanks again for the help!!! :yes: