Apply file to selected objects material color

Apply file to selected objects material color

Anonymous
Not applicable
985 Views
6 Replies
Message 1 of 7

Apply file to selected objects material color

Anonymous
Not applicable

Hello! I'm really new to mel scripting and I'm trying to make a shelf button where you select an object in your scene, click the shelf button, and it will apply a certain file onto the color.

 

Basically, I have a huge scene that is coloured, but not textured, so I want to click a buttons at the top which will assign textures with ease. Where do I even start??

 

 

0 Likes
986 Views
6 Replies
Replies (6)
Message 2 of 7

jonsX942W
Enthusiast
Enthusiast

Hi , ok let me get this strait. You want to select an object and hit your script witch will create a shader and add a filetexture to the color of that shader? is that right?  Also why would you only want this to work with one texture? 

0 Likes
Message 3 of 7

Anonymous
Not applicable

Correct! except the objects already have materials on them

 

One file texture so I can set up different shelf buttons depending on the texture. So basically add a predetermined file  onto the selected objects material at the press of a button

 

 

0 Likes
Message 4 of 7

jonsX942W
Enthusiast
Enthusiast

I see, I am confused on why you would need them all to have a different material. Like if there where 30 rocks on the ground but they all share the same texture I would just make one texture and apply it to the 30 rocks. 

 

If you need to keep them separate then I would first make a list based on selection with ls command and then with a for loop I would find what the material(hyperShade is on that object and assign(.connectAttr) a file texture (file node) of your choice to the Materials .color input. Two tricky things that would trip this up. If an object has multiple materials assigned to it. You would have to write an if statement to either bypass it or override it. Defining the image you want to use would either be a string of a file path you would run in the (fileNode)  or you could prompt yourself for a file every time but that could get annoying so I would just make like x number of shelf buttons. Is this helpfull?  I am not sure how much experience you have?

Cheers

Jon

0 Likes
Message 5 of 7

Anonymous
Not applicable

Hey! Sorry for the late response. I've tried looking into what you said to do but I'm a little bit confused.. Sorry I'm really new 😞

 

I imported .obj's that come with .mtl files but they don't seem to come with the actual textures themselves. So all the materials are already loaded, I just need to put the actual textures in. I'm trying to figure out an easy solution instead of going through 40 different materials through the hypershade every time, that can save me a LOT of time. Does this kind of clarify things?

 

EDIT: I've actually just realized that I'd need to add a file to diffuse not color since adding it to color would not keep it's original color.. 🤦‍♂️

0 Likes
Message 6 of 7

jonsX942W
Enthusiast
Enthusiast

Hey stefanegp.  Soo just to clarify, normally when you import an .obj and it has a mtl file and the textures are in the same folder as the .obj then you will see your textures. If you do not see your textures on the object but they are loaded in the scene the texture file should be correctly assigned to the diffuse or color. If they are in there but the texture path is wrong(usually because the actual file is not in the same folder as the object) you can go to file path editor and relink all the files to the correct folder. 

If you are importing .obj and the files are imported and the textures are not connected that would be odd. But if so you could write a script to connect the files but you would have to have a consistent naming convention. As an example if you had a bunch of fruit objects and matching materials and matching file names you could script it. For example object Orange, materal orange and file orange.jpg. My guess is that is not your case. 

Now what you could do is write a script that looks at all the texture files and then assigns them to new shaders. At least you would have the shaders to then assign to the correct objects. Let me know if this helps. 

0 Likes
Message 7 of 7

Anonymous
Not applicable

Thanks for your reply again Jon! I'm sorry if this is all so confusing. Yeah unfortunately for me, there are no textures provided for the .obj's, so what I am trying to do is add my own diffuse textures to the materials. No textures are connected, linked or loaded in the scene when imported.. 

 

The script like I said I don't even know where to start.

0 Likes