Forum

get_object_by_name unable to retrieve object

25 April 2017 19:43
Hi, I am loading multiple json files into a single scene as part of a large configurator. Each of these files has multiple objects. Node materials have been used so that the colors for these materials can be changed dynamically through the api. However after loading the models, get_object_by_name() is unable to find the object even though get_all_objects() shows the object in the list.

Here is my simple code.

function load() {
    m_data.load("assets/webparts/env.json", loaded_env, preloader_cb);
}

function preloader_cb(percentage) {
          m_preloader.update_preloader(percentage);
}
      
function loaded_env(data_id) {
    m_app.enable_camera_controls();
    m_data.load("assets/webparts/back/Pu10RoSt_Cl_back.json", loaded_back);
}

function loaded_back(data_id) {
    m_data.load("assets/webparts/front/Pu10RoSt_Cl_front.json", loaded_front);
}

function loaded_front(data_id) {
    m_data.load("assets/webparts/bottom/Pu10RoSt_St_bottom.json", loaded_bottom);
}

function loaded_bottom(data_id) {
    m_data.load("assets/webparts/insole/Pu10RoSt_top.json", loaded_top);
}

function loaded_top(data_id) {
    set_initial_mats();
}

function set_initial_mats() {
    var object_list = m_scenes.get_all_objects();
    console.log(object_list);
    var _frontclasser = m_scenes.get_object_by_name("front_classer");
    var _backclasser = m_scenes.check_object_by_name("back_classer");
    console.log(_frontclasser);
    console.log(_backclasser);
//    m_mat.set_nodemat_rgb(_frontclasser,["material", "base_color"], color_list["biege"][0],color_list["biege"][1],color_list["biege"][2]);
}


get_all_objects() throws a bunch of objects, of which "back_classer" and "front_classer" are clearly objects present in the scene. But get_object_by_name gives B4W ERROR: get object front_classer: not found: and check_object_by_name gives false. Why is this happening? Am I doing something wrong or is this a bug? get_all_objects() partial output can be seen below.


Object
$q:Array(0)
$s:null
Bx:false
Cd:"back_classer"
Ce:false
Df:null
EC:false
Fv:Array(0)
Hp:""
I:null
Ir:null
J:null
K:Array(1)
Kc:Array(0)
Kg:Object
Le:null
Lj:false
OA:null
Og:null
Oi:"48951b6d7f7a01a6aba41f626aba227e"
Ox:Array(0)
Qf:Array(0)
Ub:""
Wp:Array(0)
Y:null
Yi:true
Zs:Array(0)
al:false
anchor:null
cc:null
fa:Array(0)
fr:null
ig:null
is_character:false
is_dynamic:true
jv:false
lD:false
mk:10
name:"back_classer"
nb:null
o:Object
om:null
parent:null
qm:null
rc:Object
sg:Array(0)
sp:false
type:"MESH"
uu:null
ve:Array(0)
wt:null
xD:Object
xn:false
yl:Array(0)
zq:false
zr:"AUTO"
__proto__:Object
26 April 2017 10:45
Hi, in case of multiple dynamically loaded scenes you need to specify a special "data_id" parameter when calling this method, for example:
m_scenes.get_object_by_name("front_classer", 2);

This id is a counter for loaded scenes (or JSONs): for the starting scene and its objects it is 0, for the dynamically loaded and their objects it is 1,2,3,…
Its purpose is to distinguish multiple objects with the same name, because you can load the same JSON file multiple times and thus have many "front_classer" objects.

In your case it will be:
"assets/webparts/env.json" - 0
"assets/webparts/back/Pu10RoSt_Cl_back.json" - 1
"assets/webparts/front/Pu10RoSt_Cl_front.json" - 2
"assets/webparts/bottom/Pu10RoSt_St_bottom.json" - 3
"assets/webparts/insole/Pu10RoSt_top.json" - 4

Also you get "data_id" in the loaded_*** functions as a parameter.
Blend4Web Team
26 April 2017 19:45
Thanks! I thought data_id is an optional parameter and is basically used only for unloading models and all the objects have to be unqiuely named across files, and was trying to think of a work-around for this. What you say makes a lot more sense! Thank you, made my life much easier.
 
Please register or log in to leave a reply.