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

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

@prisoner849 thank You sir, last comment is worked

Hello sir i need your help!
i want change the color of object of material not whole object. I tried all possible codes that are above mention and that i know but i get errors i could not get required result.

object.traverse( function ( child ) {
	if ( child instanceof THREE.Object3D  ) {
		// console.log(child.name +"test");

			
		console.log(child);
		console.log(object);
		
	if(child.name==='010002_Autoscale'){
		alert("010002_Autoscale");
		child.material.color.setHex(0xffff00);
		
		
	}
	}
} );

this code work but it’s work for whole object i want to change just color of that material of that object not whole

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

i also try this code with my object name but it’s not worked
Untitled
just what i need is that 'i want to change color of materials ’ of any object that also help me on other object or projects.
Very Thank full sir Please help me M beginner

Hello sir, your code in the last post is not properly formatted.

2 Likes

In which post brother. please can u tell me are u talking about my post?
please u can edit it again and tell me or format it correctly

Try to reproduce your problem in a small live example. That makes it easier to understand your issue.

3 Likes