Jump to content

Solved - getChildren loop


Richard C
 Share

Recommended Posts

I have an imported blender scene. There are a number of objects. As I understand it, I can parent them to a 'pseudo' object and moving that object will move all the children as one

So I create the pseudo object, BoxP...............

             var boxP = BABYLON.Mesh.CreateBox("box0", 25, scene);

............create a variable........................ 

           var nMeshName

Loop through each of the imported objects and parent them one by one to BoxP
           
            for (var index = 0; index < scene.meshes.length; index++) {
                nMeshName = scene.meshes[index].name;
                     nMeshName.parent = boxP;
            }

But console log           
                console.log("children = "+boxP.getChildren().length);
returns 0

Therefore, in wanting to check what are the children of boxP .............               
           for(var i = 0; i < box0.getChildren().length; i++) {
            }

I of course get 0.

Could someone tell me what I have one wrong here please or tell me an alternative way of establishing the children of a parent.

I have also tried getDescendants which is no more successful

Thanks

Richard C

Link to comment
Share on other sites

you need to do a recursive loop function instead of a standard loop

*EDIT whooops reread, um just output the mesh to your console, and you will see there is an array called childrenmeshs or submeshs or something like that, find the one that has the info you need and use its length.

Link to comment
Share on other sites

Thanks Pryme8 and ArcticArca but neither of those suggestions work.

Playing a bit more with:

           var boxP = BABYLON.Mesh.CreateBox("box0", 25, scene);
                boxP.isVisible = false;

             var nMeshName
           
            for (var index = 0; index < scene.meshes.length; index++) {
                     nMeshName = scene.meshes[index].name;
                     nMeshName.parent = boxP;
            }

Going through the scene meshes a second shows that none of the meshes are being parented to boxP
                 for (var index = 0; index < scene.meshes.length; index++) {
                console.log("meshes = "+scene.meshes[index]);
            }

Any further suggestions anyone please.

Thanks again

Link to comment
Share on other sites

Problem resolved

A bit more research and I learned that objects assigned to an Empty in Blender will be exported with Parent (the Empty) and Children (all assigned objects)

The parent and it's children can be manipulated as necessary.

It would be good to know why my earlier post .............

var boxP = BABYLON.Mesh.CreateBox("box0", 25, scene);
                boxP.isVisible = false;

             var nMeshName
           
            for (var index = 0; index < scene.meshes.length; index++) {
                     nMeshName = scene.meshes[index].name;
                     nMeshName.parent = boxP;
            }

doesn't work though. Any thoughts anyone?

Thanks

Richard C

Link to comment
Share on other sites

@Richard C, @adam, @Pryme8 : I have never pretended to be anything more than a javascript wimp but I'm curious about the code.posted above

9 hours ago, Richard C said:

var boxP = BABYLON.Mesh.CreateBox("box0", 25, scene);

But does that not add a mesh to "scene.meshes" and therefore does the loop

for (var index = 0; index < scene.meshes.length; index++) {
     nMeshName = scene.meshes[index].name;
     nMeshName.parent = boxP;
}

not try to parent  boxP to itself - as boxP will have been added to "scene.meshes"? Should the loop not use "scene.meshes.length-1"

As I say just a comment from a javascript wimp :unsure:

cheers, gryff :)

 

Link to comment
Share on other sites

Problem resolved

I should have remembered that objects assigned to an Empty in Blender will be exported with Parent (the Empty) and Children (all assigned objects).The parent and it's children can be manipulated as necessary.

It would be good to know why my earlier post .............

var boxP = BABYLON.Mesh.CreateBox("boxP", 25, scene);
                boxP.isVisible = false;

             var nMeshName
           
            for (var index = 0; index < scene.meshes.length; index++) {
                     nMeshName = scene.meshes[index].name;
                     nMeshName.parent = boxP;
            }

doesn't work though. Any thoughts anyone?

Thanks

Richard C

Link to comment
Share on other sites

Gryff

I did have a statement in the loop to prevent boxP being included in the parenting - but because the whole loop didn't work, I can't be sure if it got included or not. In theory it shouldn't because I thought I was looping through imported Blender mesh.

Cheers

Richard C

Link to comment
Share on other sites

22 minutes ago, Richard C said:

for (var index = 0; index < scene.meshes.length; index++) {
                     nMeshName = scene.meshes[index].name;
                     nMeshName.parent = boxP;
            }

since scene.meshes[index].name is just the name of the mesh, ie a string, then it is no good trying to give a string  a parent.

use

scene.meshes[index].parent = boxP

Link to comment
Share on other sites

Gryff

I confirm that a prevention statement in the loop (in my case "boxP") does exclude it being parent. 

if (scene.meshes[index] == boxP) {
             } else {
               scene.meshes[index].parent = boxP;
               }
            }

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...