How to load a model from url?

I want to load some models in my application and I’ve followed some examples, try some of them but I haven’t got any success. This is the script I’m trying right now, I know this isn’t the right way to load multiple assets in the future but it’s only a test to see if I can have success loading an asset from an url.

 TestS3.prototype.initialize = function() 
{
    // allow cross origin texture requests
    this.app.loader.getHandler("texture").crossOrigin = "anonymous";    
    this.app.loader.getHandler("model").crossOrigin = "anonymous";  
    this.loadPlayer();
    this.url = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/2666677/";
};

TestS3.prototype.loadPlayer = function()
{
    var entity = this.entity;
    entity.addComponent("model");
    this.app.assets.loadFromUrl(this.url + "FireFighter.fbx", "model", function (err, asset) 
    {   
        entity.model.model = asset.resource;
        pc.app.root.addChild(entity);
        entity.setLocalPosition(0, 0, 0);
    });
};

This is the error I got:

I followed this example:
https://playcanvas.com/editor/code/501253?tabs=8936727

Hi @Axel_Saucedo,

Your code is mostly ok, the issue is that you use the this.url property before setting its value. Swap the following two lines, and it should work:

    this.url = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/2666677/";
    this.loadPlayer();
1 Like

Unfortunately it didn’t work, I get the same error :frowning:

I’ve opened the link you shared but it seems it’s a different project. Can you post the link of the project in question?

Of course, the script is inside Player entity and the script name is testS3
https://playcanvas.com/editor/scene/1003779

1 Like

So, the fix above fixed your undefined error. Now you have a different error:

Error: GraphNode is already parented

The reason is the following line 20:

pc.app.root.addChild(entity);

You can’t use this method on an entity that is already in the hierarchy. And actually I don’t think you need to reparent this entity at all. Remove it and try again.

I’m really sorry, you’re right due to I’m adding the asset into the model of the entity. Now it seems to be loaded but it isn’t actually there, but I can deal with that. Thank you!!

1 Like