Forum

Inheriting a material, from a 'selected' object.

23 July 2017 23:05
Hey Guys,

I'm trying to have a 'selected' object inherit material from a 'named' object.

In the API reference, inherit.material uses an example of two 'named' instances, like:

var m_mat = require("material");
var m_scenes = require("scenes");

var cube = m_scenes.get_object_by_name("Cube");
var cube_001 = m_scenes.get_object_by_name("Cube.001");
m_mat.inherit_material(cube, "MyMaterial_1", cube_001, "MyMaterial_2");


This is fine, and I totally get it, but what if the 'target' object was dynamic or selected and it wasn't possible to 'name it' programatically?

I've identified my selected object thus (it's also mapped to a HTML function):

function top_link_click() {    
    document.getElementById("action1").addEventListener("click", function(e) {
        if (_selected_obj)
            var obj = _selected_obj;
console.log(m_scenes.get_object_name(obj));
console.log(m_mat.get_materials_names(obj));
    });
 }   


So if I wanted my 'picked' object to inherit a material from a pre-defined 'donor' object, I'm assuming the code would look something like this, but not EXACTLY like this, as it doesn't work…


function top_link_click() {    

    document.getElementById("action1").addEventListener("click", function(e) {
        if (_selected_obj)
            var obj = _selected_obj;

var donor = m_scenes.get_object_by_name("donorCube");
var targetObj = m_scenes.get_object_by_name(obj);
var targetMat = m_mat.get_materials_names(obj);        
        
m_mat.inherit_material(donor, "DonorMat", targetObj, targetMat);        
console.log(m_scenes.get_object_name(obj));
console.log(m_mat.get_materials_names(obj));
    });
 }   


I'm so close with my overall solution it's painful. Any pointers, as ever, gratefully received.

Regards

Charlie
24 July 2017 14:10
Hi Charlie,
Hope to see your finished project when it is done!
Your code looks good. In a project like this tracing a bug is hard without seeing the whole project. For example if you had an error in your HTML button element, it wouldn't be evident in your JavaScript file. Also the first thing I check is the browser console (even if it seems to work ok, I check this). If the console is not throwing any errors that tell you something, the next thing I would do is drop in console.log() functions at various stages in the code to see if everything is happening as I believe it to be.
function top_link_click() {    

    document.getElementById("action1").addEventListener("click", function(e) {
        if (_selected_obj)
            var obj = _selected_obj;
            console.log("obj is now " + obj);
var donor = m_scenes.get_object_by_name("donorCube");
console.log("donor is now " + donor);
var targetObj = m_scenes.get_object_by_name(obj);
console.log("targetObj is now " + targetObj);
var targetMat = m_mat.get_materials_names(obj); 
console.log("targetMat is now " + targetMat);       
        
m_mat.inherit_material(donor, "DonorMat", targetObj, targetMat);        
console.log(m_scenes.get_object_name(obj));
console.log(m_mat.get_materials_names(obj));
    });
 }   
24 July 2017 20:47
I'll probably start this off on a separate topic, as I suspect the 'problem' might actually be an interesting one… Here's a quick rundown on what I've discovered.

The first thing I did was map the standard inherit.material function to my button (when I say 'standard', I mean that I named all of the materials without inheriting them as variables). It worked fine, so I'm pretty sure it's not a button error.

So, I thought I'd take baby steps, by defining the objects (donor and receiving) as variables, but leaving the material names AS names, in speech marks, e.g; "myMaterial".

This also works fine.

The problem arises when I try to declare the material from the 'picked' object as a variable, so let's say the function looks like this:

function botton_controls() {    

    document.getElementById("action1").addEventListener("click", function(e) {
        if (_selected_obj)
            
var sel = _selected_obj;        
    
var tMat = m_mat.get_materials_names(sel); 
              
var c1 = m_scenes.get_object_by_name("donorCube");
var c2 = m_scenes.get_object_by_name("RecCube");
        
m_mat.inherit_material(c1, "donorMat", c2, tMat);        
        
        


    console.log(m_scenes.get_object_name(sel));
	console.log(m_mat.get_materials_names(sel));
    });
 }   


So, 'sel' is the 'selected object'. 'tMat' is the material on the selected object (or target object). - The console logs at the bottom, report the names of all objects without any issues, but when the function itself is executed, it fails with the error:

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

I know from this, that the declarations are fine, because 'RecMat' is the correct name of the material. The console log also identifies 'RecMat' as the correct material… So why does B4W not identify the presence of the material as the variable 'tMat', when it's inside the inherit.material function?

I'm not 100% that this can be done, I have a terrible feeling that I've hit an API brick wall. Tell me I'm wrong. Please.
 
Please register or log in to leave a reply.