How to rotate as animation a loaded model by SceneLoader.Append

I load a model with SceneLoader.Append and i want to take screenshots from diferents angles while its moving

Heres my code

var createScene = function() {

        var scene = new BABYLON.Scene(engine);

        camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 0, BABYLON.Vector3.Zero(), scene);

        camera.setPosition(new BABYLON.Vector3(0, 0.5, -4));

        camera.attachControl(canvas, false);

        BABYLON.SceneLoader.Append("", "data:" + gltfData, scene,

            function() {

                scene.activeCamera.alpha += Math.PI / 2;

                SceneLoader.ShowLoadingScreen = false;

                scene.clearColor = new BABYLON.Color4;

            },

            function() {

                console.log("### Loading 3d models")

            },

            function(error) {

                console.error("### Error loading model")

            })

        scene.executeWhenReady(function() {

            console.log("Scene ready");

            BABYLON.Tools.CreateScreenshotUsingRenderTarget(engine, scene.activeCamera, { width: 500, height: 300 }, function(img) {

                console.log("New img")

            })

        })

        scene.registerBeforeRender(function() {

            // Rotate object?

        })

        return scene

    }

Sure :slight_smile: And what is the problem or the question? :slight_smile:

Also, it is better to provide a repro in the Playground

1 Like

Hi! Thanks for replaying

I want to make this effect
https://www.babylonjs-playground.com/#7DS5D4#3

I see it is using the meshes from SceneLoader.importMesh, but I need to use SceneLoader.Append because to load my model I have to pass it with this format:

BABYLON.SceneLoader.Append("", “data:” + gltfString, scene, function (scene) {
// do something with the scene
});

( gltfString is a .babylon )

I don’t know if there is a way to rotate the model o the camera.

well then in this case before calling Append you can save scene.meshes.length into a variable (say, named i)

then inside Append, all new meshes will be on scene.meshes[i] and onward :slight_smile:

Are there any differences between scene.beforeRender (inside Append) and scene.RegisterBeforeRender (outside Append) ? Because ( I don’t know why) I have an error when the models its loaded, and I think that that makes some functions not be called .

And when i make
scene.registerBeforeRender(function() {

            for (let i = 0; i < scene.meshes.length; i++) {

                scene.meshes[i].rotation.y += 0.005

            }

        })

(outside Append)

It rotates the environment

These models are downloaded from sketchfab as .gtlf , loaded on the sandbox and exported as .babylon

Maybe the easiest way to help you would be a repro in the Playground

Heres the repro in the playground, I put in a variable the loaded model

https://playground.babylonjs.com/#YQXWX2

Here we are:
https://playground.babylonjs.com/#YQXWX2#2

1 Like

Thanks a lot!

Hi! it’s me again haha.
This solution works for some cases, but for example, in this repro, I cant achieve what I want

https://playground.babylonjs.com/#DAUVD7

When I use scene.createDefaultCamera(true,true,true) and rotate the object, i looking for that “rotation effect”

This is because your object has a pivot which is not centered.
I picked the right object:
https://playground.babylonjs.com/#DAUVD7#1

Ahh, so in these cases, I can rotate the object itself, some are centered, others no…
Is there a way to rotate the camera around the object?
I making a WordPress plugin to show 3d models, and I need to pre-show the model and make it rotate to take snapshots. (Looking for the effect that has SketchFab on their “preview model”)

yes you can rotate the camera. If it is an arcrotate, you can simply set its target to the mesh and then change the camera.beta value :slight_smile: