How to fetch child mesh of a .obj? [Solved]

Hello Members,
I’ve loaded an object(Man Body) file. On that object I want load different texture, suppose on Shirt red color, and on pant blue color and on hair black color. So how to fetch specific part of an object(child ) . see example.

Thank You…!
Sandip

Here I fetch one Child object Torus, now How to apply manual color to that child object See Code

			 var loader = new THREE.OBJMTLLoader();
			loader.load( 'meshes/objNmtl/sign3.obj', 'meshes/objNmtl/sign3.mtl', function ( object ) {
				object.traverse( function ( child ) {
					if ( child instanceof THREE.Object3D  ) {
						
					if(child.name=='Torus'){
						alert("torus");//
						
						///here i need to add color
					}
						
					}
				} );
				scene.add( object );

			} );
1 Like

You can either access the geometry as an array of parent-child or use scene.getObjectByName(“name of object”)

@Waverider , I access by name , see code. But color is not applying.

child.material.color.setHex(0xffff00);

I tried above code not workded

1 Like

it won’t work if your geometry is all merged into one, try console.log(object)

Ok , I will try using geometry . Thank U @Waverider

1 Like

Log your “Torus” child object in the console and check what type of material it has.

@prisoner849, there is no specific information of material, Seeimage

1 Like

An instance of Object3D does not have a material property, so that’s why it isn’t working for you.

I can see in your screenshot that this Object3D instance has 2 Mesh children. Instances of THREE.Mesh have material.

:open_mouth: Don’t you really see the type of your object??
It’s not THREE.Mesh(), it’s THREE.Object3D(). Of course, you can’t apply any colour, because it has no material. You need to look in its children, they are of THREE.Mesh().

Yeh sorry I got now, but it changing all part I want change only Torus not sphere

see
image

1 Like

What do you do to the children objects to get such a result?

@prisoner849, I am working on 3d map navigation. for traffic light I will used this thing to change color dynamically.

1 Like

I mean, what did you do to set the same colour to all parts of the model?

See image, Suppose in this image three lights are there, 2 sphere and 1 Tours. Now I want to change color of torus grammatically. image.
I fetch the torus
var loader = new THREE.OBJMTLLoader();
loader.load( ‘meshes/objNmtl/sign3.obj’, ‘meshes/objNmtl/sign3.mtl’, function ( object ) {

				object.position.y = 5;
				object.position.z = 5;
				object.rotation.y = 4;
				object.scale.set(0.5,0.5,0.5);
				
				object.traverse( function ( child ) {
					if ( child instanceof THREE.Object3D  ) {
				//		console.log(child.name +"test")
					if(child.name=='Torus'){
						alert("torus");
						console.log(child)
					//	child.material.color.setHex(0xffff00);
					}
					}
				} );
				scene.add( object );
			} );
1 Like
  1. Do you really think your posts with poor-formatted code are readable?
  2. I already understood, what you want in the result. But could you, please, do something yourself in your project? Set different colour to a different child object of your model, so you will know what is what.

object.getObjectByName( "Torus" ).children[0].material.color.setHex( 0xff0000 );
object.getObjectByName( "Torus" ).children[1].material.color.setHex( 0x00ff00 );
and so on.

I am doing sir, but at loading time error is coming… now same color came to Torus and sphere , previous also I got this solution. But I need sphere and torus different color

            var loader = new THREE.OBJMTLLoader();
			loader.load( 'meshes/objNmtl/sign3.obj', 'meshes/objNmtl/sign3.mtl', function ( object ) {

				object.position.y = 5;
				object.position.z = 5;
				object.rotation.y = 4;
				object.scale.set(0.5,0.5,0.5);
				
				object.traverse( function ( child ) {
					if ( child instanceof THREE.Object3D  ) {
						 object.getObjectByName( "Torus" ).children[0].material.color.setHex( 0xffff00 );
						object.getObjectByName( "Torus" ).children[1].material.color.setHex( 0xaaff00 ); 
					if(child.name=='Torus'){
					}
					}
				} );
				scene.add( object );
			} ); 

image

1 Like

Possibly, they share whe same material, so you can set a new material with different colour to each child object.

can you me some sample code when I defined marerial1 it said color is not defined

2 Likes
object.getObjectByName( "Torus" ).children[0].material = new THREE.MeshBasicMaterial({color: "red"});
object.getObjectByName( "Torus" ).children[1].material = new THREE.MeshBasicMaterial({color: "green"});

Doesn’t it work?

1 Like