Load material from assets through script

Hi

I want to map materials from a Revit model by letting playcanvas run through the json file and find the materials that are in the assetsfolder to replace with the ones that are originally from the model.
The code is :

var jsonMaterial = definition.Materials[definition.BuildingElements[id].MaterialId];
console.log(definition.Materials[definition.BuildingElements[id].MaterialId]);

                    var instance = null;
                    
                    var originalName = jsonMaterial.OriginalName;
                    var jsonMaterialMap = this.materialJSON.resource;
                    debugger;
                    var material;
                    
                    if(jsonMaterialMap.hasOwnProperty(originalName))
                    {
                        var targetMaterialName = jsonMaterialMap[originalName];
                        material = this.app.assets.find(targetMaterialName);
                       
                      // I can get the information of the material but here is were the error shows up
                         ERROR : Cannot read property 'push' of undefined
                        
                        console.log(material);
                        
                        
                    } else {
                        // create a custom material
                        material = new pc.scene.PhongMaterial();                  
                        material.diffuse.set(jsonMaterial.ColorR, jsonMaterial.ColorG, jsonMaterial.ColorB);
                        material.opacity= jsonMaterial.Opacity;
                        material.reflectivity = (jsonMaterial.Transparent ? 1 : 0.2);
                        material.update();
                    }
                    
                    
                     instance = new pc.MeshInstance(node, mesh,material);
                     instance.castShadow = true;
                     instance.receiveShadows = !jsonMaterial.Transparent;   

I think the problem is that it gets back an ASSET and not a material to implement.
Hopefully you can help me out, thanks .
Maybe a quick example on how to add a material to a object when retrieving it from the assetsfolder through a script.

Thanks in advance

AssetRegistry.find returns an Asset, to get it’s resource (material in this case), you should write:

var materialAsset = this.app.assets.find(targetMaterialName);
if (materialAsset) {
    var material = materialAsset.resource();
}

Asset - is not material, model or any specific resource, it is abstraction around any resources to manage loading, and meta information about them.

Hi thanks for the quick response,

I tried but it gives me the error : materialAsset.resource is not a function
Not sure were the error lies.

var originalName = jsonMaterial.OriginalName;
var jsonMaterialMap = this.materialJSON.resource;
var material;

                    if(jsonMaterialMap.hasOwnProperty(originalName))
                    {
                        var targetMaterialName = jsonMaterialMap[originalName];
                        var materialAsset = this.app.assets.find(targetMaterialName);
                        if (materialAsset) 
                        {
                            material = materialAsset.resource();
                        }                       
                        console.log(material);
                        
                        
                    } else {
                        // create a custom material
                        material = new pc.scene.PhongMaterial();                  
                        material.diffuse.set(jsonMaterial.ColorR, jsonMaterial.ColorG, jsonMaterial.ColorB);
                        material.opacity= jsonMaterial.Opacity;
                        material.reflectivity = (jsonMaterial.Transparent ? 1 : 0.2);
                        material.update();
                    }

Please use API Reference: http://developer.playcanvas.com/en/api/pc.Asset.html#resource
Asset.resource - is not a function.

As well please use very powerfull Dev Tools Debugging: http://developer.playcanvas.com/en/user-manual/scripting/debugging/ you can breakpoint on things, and inspect variables, and see what data is around, etc.

Okay will try it , thank you !