How to unload objects properly?

04 December 2015 17:17
Hi @ll,

I want to load, unload and load an object. Therefore I am storing in the 'object loaded callback' the data_id of the object, given as the callback parameter.
This data_id is later-on used to unload the object with

Executing the unload function, the object disappears visually, giving the impression that unloading worked. But reading with
all objects, the object name remains still there.

Attached is a small project, in which an environment scene gets dynamically loaded first (the ground plane), and then, by pressing the 'load object'-button, a Torus object is dynamically loaded. By pressing the 'unload object'-button the object unloads "visually". The third button tries to set a shader-node-parameter of the Torus (controlling the level, all above the level shows as gray material, below as black).

There is some, perhaps related/influencing, circumstance occurring: The function
does not find the Torus object.
(I found the m_scenes.get_active() API call and thought, perhaps there is not marked the right scene as active. But no other scene was available, only the 'EnvironmentScene'-Scene of the first dynamic load. )

In the attached gif is also visible the javascript-console. The shown console-error is the before mentioned 'not finding via get_object_by_name()' function. I left the error-message only for demonstration. The demo behaves the same, when the error-message/cause is commented out.
The searched object instead is found manually by iterating over all objects via
and comparing with the object-name. At console message:script.js:92 we see that the Torus is present multiple times and not unloaded.

'A' torus was found (the first in list), and shader-node-parameter-changed (because no further errors arise), but not from the visible one.

Am I missing something to unload (or load) an object properly?
Please help,
@ Emotional3D
04 December 2015 18:21

I've corrected your "setObjectMaterialParameterHeightToRandomValue" function. Take a look at the following code lines, please:

function setObjectMaterialParameterHeightToRandomValue(){
        var sceneName = "TorusScene";
        var objName = "Torus";
        //objName = "Plane";
        var materialName = "Material";
        var parameterName = "Height";
        var value = Math.random()*2.5;

        //console.log( "Before: ", m_scenes.get_active() );
        //console.log( "All scene names: ", m_scenes.get_scenes() );
        //console.log( "After: ", m_scenes.get_active() );
        //console.log( "All scene names: ", m_scenes.get_scenes() );

        // get_object_by_name("Torus") finds nothing.
        var objFoundWithAPICall = m_scenes.get_object_by_name(objName, objectID);
        console.log( "Found Object via API-Call get_object_by_name('Torus'): ", objFoundWithAPICall );
        // var obj = findObjectByName(objName);
        // console.log( "Found Object by Name 'Torus' over all objects via get_all_objects(): ", obj );

        m_obj.set_nodemat_value(objFoundWithAPICall, [materialName, parameterName], value);

I appended your global variable "objectID" to m_scenes.get_object_by_name function. It's necessary, because this function uses 0 value by default. And the engine tries to find your object in main scene objects. The "get_all_objects" function uses m_obj.DATA_ID_ALL as a default data_id value.

The m_scenes.get_object_by_name cannot use DATA_ID_ALL by default value for data_id variable, because you can load the same scene twice or more times. In this case this function will have to return two or more objects, but it must return only one object.

04 December 2015 18:30
Roman, thank you for clarifying.

Cheers, Daniel
@ Emotional3D
04 December 2015 18:32
Can I give you an advice?

You should disable scenes dynamic loading if you've already loaded one and don't unload it. I can press loading button 100 times and, when I press unload button, only the latest loaded scene will be unloaded.
04 December 2015 18:38
Take a look at this file, please
04 December 2015 18:39
Yes, of course, always very much appreciated.
(This was only a test-project for reproducing/showing my question. Main project will keep track of that)

Thank you Roman!
@ Emotional3D
04 January 2016 14:08
In previous Post#5657 we resolved how to get the correct instance reference.
But the initial topic persists - it seems that some resources/objects don't get unloaded.

Loading and unloading different objects (000.json, 001.json, …, 004.json) and a subsequent listing of all objects from the active scene via
lists still all prior loaded-and-unloaded objects.

With even more complex objects (geometry-, texture- and shader-wise) it shows on mobile devices by crashing the website after loading and unloading 10-20 objects. The attached project contain simpler/smaller objects with which the crash is not apparent, but the object clogging can be seen/reproduced in console-log.

Do I miss something? Please help.
@ Emotional3D
04 January 2016 18:00
Hi, Daniel

This behavior is actually a bug, we'll try to fix it by the next release coming later this month. Thanks for reporting and Happy New Year
04 January 2016 18:15
Thank you Alexander, and С Новым Годом too for you and the whole Blend4Web-Team.
@ Emotional3D
26 January 2016 16:39

The issue has been resolved. Please check the upcoming release.
Please register or log in to leave a reply.