Omission or bug?

25 July 2017 01:22
Hey Guys,

I thought long and hard whether I should post this in the bugs category… I'm pretty new to your framework and js, but after spending hours and hours, with the guidance of a few other forums, I've decided to post.

There's certainly a chance I've missed something, but the coding problem I have seems so obvious, that at the very least, it's an omission, so worth raising.

If I have missed something obvious, please accept my apologies in advance and feel free to move this to a more relevant category.

OK. I've been working with the inherit material feature and the problem in it's simplest form is this… I can declare the object 'to' and the object 'from' as any variable I like, such as:

var donor001 = m_scenes.get_object_by_name("donorCube");

However, it seems there is no other way to declare the materials, other than hardcoding them by name. It is not possible to declare the material as a variable and include it within the inherit.material function.

If I'm wrong, I'm wrong. I'm sorry… Please set me straight and tell me how it's done.

Here's my function code, an explanation and the console output:


function button_controls() {    

    document.getElementById("action1").addEventListener("click", function(e) {

var sel = _selected_obj;        
var targetMat = m_mat.get_materials_names(sel);             
var donor001 = m_scenes.get_object_by_name("donorCube");

m_mat.inherit_material(donor001, "donorMat", sel, targetMat);        


So my function is mapped to a HTML button and it works fine if both my materials are named. But if you look at the code, I want a 'picked object' to inherit material form a pre-named source.

Looking more closely at this function:

m_mat.inherit_material(donor001, "donorMat", sel, targetMat); 

1/ The donor object and it's material are set up correctly.
2/ My selected object; 'sel' works fine if I name it's material, but targetMat doesn't work as a true variable.

I looked at the semantics of the command to start with; 'm_mat.get_materials_names' against 'm_scenes.get_object_by_name'… Obviously one is actually retrieving the object, whilst the other is retrieving the NAME of a material. You can't apply a NAME to an object. Not sure if that's right, but I guess it would make sense.

The console reports that: B4W ERROR: inherit_material(): material "RecMat" not found on the object "RecCube".

So I know the function is working correctly, as RecMat is the right name for the material and RecCube is the right name of the object. If you look at the code again, you'll see that I've set up a log to report the name and material of the selected object. These both return true, with the right names.

So why can't B4W identify a material as a pre-declared variable?

Help appreciated, as this has pretty much put a halt to my current project.

All the best

25 July 2017 18:52
Without the whole project to test, I can only guess but get_materials_names() returns an array.
Can you try:
var targetMat = m_mat.get_materials_names(sel);
25 July 2017 19:18
Hey Will, thanks for the pointer.

The console returns the name of the material; 'RecMat'.

I'm totally stuck here, so I've uploaded the project. If you wouldn't mind taking a look that would be so helpful, I'm absolutely stuck on this one.

Here's a link to the zip:

The project's main HTML file contains the menu and active links (although I'm sure they work fine)

In preview, if you click the large cube on the left, it flips to the right menu, then click the link 'THIS ONE' (at the top), to replicate the problem.

Let me know if you have any luck, I'm banging my head on the wall with this one


25 July 2017 19:25

I looked at your console code and incorporated it within the function, like:

m_mat.inherit_material(donor001, "donorMat", sel, targetMat[0]); 

This works!

I don't know why it works, but it does. Can you explain to me what's actually happening, thanks.

Big help!

25 July 2017 19:26
Ok Charlie, I will have a look at it. I will have time later today to dive into it.
26 July 2017 02:02
Ah you got it working.
So you probably know what an array in JavaScript looks like.

var cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars[0];

cars[0] refers to the first element in the array. Counting starts at 0.
cars[2] would be BMW, the third element in the array.
You can read about it here.
Knowing that, you can have one object with all of your materials assigned to it. Then m_mat.get_materials_names(library_object) will get you an array containing all of your materials.

Please register or log in to leave a reply.