Forum

How to assign/switch materials?

01 June 2017 14:00
Hi guys,

I have just started to use Blend4Web and I have a problem with the materials and the API.

I have a blend file with 6 materials and 3 objects, these 3 objects have a material each (so I have 3 unassigned materials).

In Blender I can switch the material using this simple menu.

How can I do the same thing with Blend4web api?

First of all I have checked the Json file and inside there are only 3 materials (the assigned ones), is it possible to export all the material library?
Second I didn't find any api command to assign a material to an object, I have found only commands to edit the materials. The only command interesting is inherit_material but it moves a material from a object to another.

In the meanwhile I have also done also some tests, do you know why this command:
var object = b4w_scenes.get_object_by_name('test');
			var material_list = b4w_materials.get_materials_names(object);
			console.log(material_list);

return material_list an empty array? The object test has an assigned material.

Thanks.

H2K
01 June 2017 14:24
I am sometimes wrong on this stuff so one of the developer can correct me if needed but…
I believe you can change the texture but not the material. You can change the properties of the material, see the Material API
You can change the texture to a different image, see the Change Image API
01 June 2017 14:37
Reply to post of user Will Welker
I am sometimes wrong on this stuff so one of the developer can correct me if needed but…
I believe you can change the texture but not the material. You can change the properties of the material, see the Material API
You can change the texture to a different image, see the Change Image API

Thank you Will, I supposed that, but I wasn't sure.

I was also thinking to a different approach. If I assign 3 different materials (for example) to the same object. Do you think that it would be possible to hide show the materials?

Thanks
H2K
01 June 2017 14:56
Hi guys,

I have just started to use Blend4Web and I have a problem with the materials and the API.

I have a blend file with 6 materials and 3 objects, these 3 objects have a material each (so I have 3 unassigned materials).
Hello and welcome to the forum!

You were digging in the right direction)) inherit_material is used to dynamically change materials. Please take a look at this part of the documentation

I believe you can change the texture but not the material. You can change the properties of the material, see the Material API
You can change the texture to a different image, see the Change Image API
Absolutely right! ))
Blend4Web Team - developer
01 June 2017 15:00
Makes sense, you can adjust the Alpha Factor. When you look at the Code Snippets from your local SDK-Server, there is a button on the lower left to "Make Project" . It will copy the API example as a new project that you can play with. I am out of time today but since I am curious now, I will try it tomorrow.
02 June 2017 12:17
Hi guys

first of all thanks for the help.

I'm stuck with the material list, I can't get it.

This:
var object = b4w_scenes.get_object_by_name('Zeta_0');
var material_list = b4w_materials.get_materials_names(object);
console.log(material_list);

returns an empty array but I have 3 materials assigned to Zeta_0, you can download my files here:
json: http://d1mr8g7ihxhxdy.cloudfront.net/0222d970708f9ec80886058e78a2343ba4805a19.json
bin: http://d1mr8g7ihxhxdy.cloudfront.net/0222d970708f9ec80886058e78a2343ba4805a19.bin

I have did a console.log of the b4w_scenes.get_object_by_name('Zeta_0'); and it seems ok, it is full of data.

Am I missing something? Reading the Json I can see the 3 materials assigned to the mesh:


Do I have to do check something inside Blender before the export?

Thanks again.

H2K
07 June 2017 10:45
Hi,

Am I missing something? Reading the Json I can see the 3 materials assigned to the mesh:
I suppose that this object isn't a dynamic one, hence the method doesn't work as expected - this is inconvenient for now. You should check the "Force Dynamic Object" option on the "Object->Rendering Properties" panel.
07 June 2017 12:43
Thanks Ivan, it works.
 
Please register or log in to leave a reply.