# How can i load model on run time loading fine on static url

**URL:** <https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451>\
**Category:** Help & Support\
**Created:** [August 20, 2020, 11:46am UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451 "2020-08-20T11:46:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fahadi\_Nadeem](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/fahadi_nadeem/32/9459_2.png) [@Fahadi\_Nadeem](https://forum.playcanvas.com/u/Fahadi_Nadeem)\
**Post date:** [August 20, 2020, 11:46am UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/1 "2020-08-20T11:46:58Z")

</div>

Play canvas code:

```javascript
var Obj = pc.createScript('obj');
    console.log('132a');

window.onmessage = function(e){
Obj.attributes.add('material', { type: 'asset', assetType: 'material' });
    Obj.prototype.loadModel = function (objStr) {
            var objData = new OBJ.Mesh(objStr);
            var url;
            var options = {};
            if (objData.vertexNormals) options.normals = objData.vertexNormals;
            if (objData.textures) options.uvs = objData.textures;
            if (objData.indices) options.indices = objData.indices;
            if (objData.vertices && objData.vertexNormals && objData.textures && objData.indices) {
                var tangents = pc.calculateTangents(objData.vertices, objData.vertexNormals, objData.textures, objData.indices);
                options.tangents = tangents;
            }

            var mesh = pc.createMesh(this.app.graphicsDevice, objData.vertices, options);
            var node = new pc.GraphNode();
            var material = this.material ? this.material.resource : new pc.StandardMaterial();
            var meshInstance = new pc.MeshInstance(node, mesh, material);
            var model = new pc.Model();
            model.graph = node;
            model.meshInstances = [meshInstance];

            this.entity.addComponent('model');
            this.entity.model.model = model;
        };

        alert('http://network.daruldev.com/models');

        Obj.prototype.initialize = function() {
        var asset = new pc.Asset("obj", "text", {
            url: 'https://network.daruldev.com/models/eyeball.obj'
        });

        var self = this;
        asset.ready(function (asset) {
            self.loadModel(asset.resource);
        });

        this.app.assets.add(asset);
        this.app.assets.load(asset);
    };
        Obj.prototype.update = function(dt) {

    };
        // url = 'https://network.daruldev.com/models'+e.data;
        // Obj.attributes.add('url', { type: 'string' });
    console.log('132');
    
};

```

Web page code :

https://playcanv.as/e/p/EnuYGgVe/ Click me

```
                                            <script>
                                                function myFunction(){
                                                    var app = document.getElementById('app');
                                                    app.contentWindow.postMessage('{{ $models_3d->model }}', '*');
                                                 }
                                            </script>
                                    </div>
```

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [August 20, 2020, 11:50am UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/2 "2020-08-20T11:50:05Z")

</div>

Hi @Fahadi_Nadeem,

Loading an .obj model on runtime requires first adding to your project the obj-loader extension, from the Playcanvas repo.

You can find the script here:

> <https://github.com/playcanvas/engine/blob/master/examples/assets/scripts/parsers/obj-model.js>

And here is an example on how to add it to your project and then how to call the **loadFromUrl()** to load the remote file:

[https://playcanvas.github.io/#loaders/loader-obj.html](https://playcanvas.github.io/#loaders/loader-obj.html)

> <https://github.com/playcanvas/playcanvas.github.io/blob/master/loaders/loader-obj.html>

---

<div class="post-metadata">

**Author:** ![Fahadi\_Nadeem](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/fahadi_nadeem/32/9459_2.png) [@Fahadi\_Nadeem](https://forum.playcanvas.com/u/Fahadi_Nadeem)\
**Post date:** [August 20, 2020, 12:31pm UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/3 "2020-08-20T12:31:43Z")

</div>

@Leonidas  
We have worked on it its working fine when we code the static url but when use the postmessage its not working with iframe please let me know what to do with iframe

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [August 20, 2020, 12:33pm UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/4 "2020-08-20T12:33:17Z")

</div>

Sorry that’s on your parent website codebase, not Playcanvas.

You will have to search for a JavaScript tutorial on how to setup a communication between two windows using postMessage.

---

<div class="post-metadata">

**Author:** ![Fahadi\_Nadeem](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/fahadi_nadeem/32/9459_2.png) [@Fahadi\_Nadeem](https://forum.playcanvas.com/u/Fahadi_Nadeem)\
**Post date:** [August 20, 2020, 12:49pm UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/5 "2020-08-20T12:49:54Z")

</div>

@Leonidas we are already communicating the communication works fine i have debug the communication is working fine but the model is not load after i get the url from website that the problem

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [August 20, 2020, 12:51pm UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/6 "2020-08-20T12:51:42Z")

</div>

Try sharing here a url to your project to take a look, not sure what’s wrong with the code you shared.

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [August 20, 2020, 1:29pm UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/7 "2020-08-20T13:29:59Z")

</div>

What error message do you get as well?

Is `new OBJ.Mesh(objStr)` one of your classes/code?

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [August 20, 2020, 1:38pm UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/8 "2020-08-20T13:38:39Z")

</div>

Looking at the code of the project, you seem to have wrapped a bunch of code in the on window.message function.

Sounds like this is what you wanted to do and have to on message callback do something with the message string?

[https://playcanvas.com/project/712121/overview/obj-forum-project](https://playcanvas.com/project/712121/overview/obj-forum-project)

---

<div class="post-metadata">

**Author:** ![Fahadi\_Nadeem](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/fahadi_nadeem/32/9459_2.png) [@Fahadi\_Nadeem](https://forum.playcanvas.com/u/Fahadi_Nadeem)\
**Post date:** [August 22, 2020, 6:50am UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/9 "2020-08-22T06:50:31Z")

</div>

@yaustar link is not working !

---

<div class="post-metadata">

**Author:** ![Fahadi\_Nadeem](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/fahadi_nadeem/32/9459_2.png) [@Fahadi\_Nadeem](https://forum.playcanvas.com/u/Fahadi_Nadeem)\
**Post date:** [August 22, 2020, 7:20am UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/10 "2020-08-22T07:20:44Z")

</div>

@yaustar  
@Leonidas

[http://network.daruldev.com/test](http://network.daruldev.com/test)

this is the test link i have created for you !

---

<div class="post-metadata">

**Author:** ![Fahadi\_Nadeem](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/fahadi_nadeem/32/9459_2.png) [@Fahadi\_Nadeem](https://forum.playcanvas.com/u/Fahadi_Nadeem)\
**Post date:** [August 22, 2020, 7:37am UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/11 "2020-08-22T07:37:17Z")

</div>

> [@yaustar](#):
>
> new OBJ.Mesh(objStr)

yes !

---

<div class="post-metadata">

**Author:** ![Fahadi\_Nadeem](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/fahadi_nadeem/32/9459_2.png) [@Fahadi\_Nadeem](https://forum.playcanvas.com/u/Fahadi_Nadeem)\
**Post date:** [August 22, 2020, 7:38am UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/12 "2020-08-22T07:38:11Z")

</div>

> [@yaustar](#):
>
> callback do something with the message string?

yes exactly !

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [August 22, 2020, 7:45am UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/13 "2020-08-22T07:45:33Z")

</div>

> [@Fahadi\_Nadeem](#):
>
> @yaustar link is not working !

Oops. Left it on private. It’s now public.

---

<div class="post-metadata">

**Author:** ![Fahadi\_Nadeem](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/fahadi_nadeem/32/9459_2.png) [@Fahadi\_Nadeem](https://forum.playcanvas.com/u/Fahadi_Nadeem)\
**Post date:** [August 22, 2020, 7:51am UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/14 "2020-08-22T07:51:49Z")

</div>

> [@yaustar](#):
>
> Oops. Left it on private. It’s now public.

can you please check the link and suggest the code

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [August 22, 2020, 8:03am UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/15 "2020-08-22T08:03:43Z")

</div>

You can now access the project I posted with the code that I believe fixes your issue. You still need to tweak it should be pretty straightforward forward.

---

<div class="post-metadata">

**Author:** ![Fahadi\_Nadeem](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/fahadi_nadeem/32/9459_2.png) [@Fahadi\_Nadeem](https://forum.playcanvas.com/u/Fahadi_Nadeem)\
**Post date:** [August 22, 2020, 8:21am UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/16 "2020-08-22T08:21:35Z")

</div>

@yaustar  
[http://network.daruldev.com/test](http://network.daruldev.com/test)

their is an error of undefined asset

 ![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/a/a9111ef623f0f4ba21cc02df83a6277443b9b669.jpeg)

---

<div class="post-metadata">

**Author:** ![Fahadi\_Nadeem](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/fahadi_nadeem/32/9459_2.png) [@Fahadi\_Nadeem](https://forum.playcanvas.com/u/Fahadi_Nadeem)\
**Post date:** [August 22, 2020, 9:55am UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/17 "2020-08-22T09:55:28Z")

</div>

> [@yaustar](#):
>
> You can now access the project I posted with the code that I believe fixes your issue. You still need to tweak it should be pretty straightforward forward.

it is showing :Uncaught TypeError: Cannot read property ‘assets’ of undefined  
@yaustar

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [August 22, 2020, 11:17am UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/18 "2020-08-22T11:17:06Z")

</div>

I’ve modified the PlayCanvas project and set up an example on JSFiddle: [https://jsfiddle.net/2yzpdec9/5/](https://jsfiddle.net/2yzpdec9/5/)

[https://playcanvas.com/editor/scene/974566](https://playcanvas.com/editor/scene/974566)  
The code is just a modified version of what was given here: [Connect web App with Playcanvas iFrame](https://forum.playcanvas.com/t/connect-web-app-with-playcanvas-iframe/14411/4)

---

<div class="post-metadata">

**Author:** ![Fahadi\_Nadeem](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/fahadi_nadeem/32/9459_2.png) [@Fahadi\_Nadeem](https://forum.playcanvas.com/u/Fahadi_Nadeem)\
**Post date:** [August 22, 2020, 3:21pm UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/19 "2020-08-22T15:21:49Z")

</div>

What if we want to load on page load not on click when we should load it ?  
@Leonidas

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [August 22, 2020, 3:59pm UTC](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451/20 "2020-08-22T15:59:17Z")

</div>

1. You should send first a message from the Playcanvas side to your parent page, that Playcanvas loading has finished, app is ready.

2. As a response send back from the parent page to Playcanvas the message @yaustar prepared on jsfiddle:

```javascript
app.contentWindow.postMessage('https://network.daruldev.com/models/eyeball.obj', '*');

```

[Next page](https://forum.playcanvas.com/t/how-can-i-load-model-on-run-time-loading-fine-on-static-url/14451.md?page=2)
