4

I have around 1500 objects. Each object has a material that is named exactly after the texture it corresponds to (e.g. "Tex_01.dds", "Tex_233.dds", etc). The objects and textures are both in the same folder. Is it possible for me to make an automated method that applies the correct textures to all objects in one go?

Edit: As the question appeared too vague, i've constructed a series of images to further explain the issue.

These are all the objects which i'm trying to add a texture to. They originate from a building system from a game. I wish to recreate this in Blender with a grid system.

enter image description here Each object has one material. The name of the material is the same as the name of the texture that connects to the model.

enter image description here

Lastly, these are the textures which i would like to put on the models.

enter image description here

Edit 2: All materials have no nodes in them. The manual procedure would be to add a material output, diffuse node, image texture node and optionally a texture coordinate for UV.

I've also included a .blend with some samples if anyone would be interested in checking it out.

https://www.dropbox.com/s/blvgx98lmp8n27o/MS2Props02.zip?dl=0

maplestick
  • 442
  • 4
  • 16
  • Please clarify your question. It is vague. Include an image. Please show a few objects and 2 or 3 to which you have applied an image. An improved question may get a bigger response. – atomicbezierslinger May 15 '18 at 04:57
  • Please state your level of experience with Blender and Python or similar tool. Small Medium Large. – atomicbezierslinger May 15 '18 at 04:58
  • 1
    I believe you can! Watch this video on YouTube https://youtu.be/i4ySFm4ey9U There he builds an entire city, with a real cool fast map cheat, and then applies textures to many buildings at once. You will learn some other cool stuff as well. I hope that helps. Yiuliana – Yiuliana May 15 '18 at 01:53
  • @atomicbezierslinger I appreciate your feedback! As you have suggested, i've added a few images to make it easier to understand. To further answer your questions, i've used Blender for roughly 4 years, but it's mostly self-taught bits and pieces. I've ran a few scripts but i have no knowledge on how to write them. – maplestick May 15 '18 at 08:12
  • Please place a texture on 3 meshes of maximum shape difference. If the UV coordinates are available please describe this. Count how many different mesh shapes you have. – atomicbezierslinger May 15 '18 at 15:40
  • Do your objects have a UV map? – batFINGER May 15 '18 at 15:47
  • @atomicbezierslinger As seen in the picture i can't really manually count all the meshes, but it's safe to assume there are at least a few hundred unique shapes. Since it's all low-poly the UV is relatively simple. As for the maximum shape difference, i don't think it's humanly possible to figure that out i'm afraid. – maplestick May 15 '18 at 17:23
  • @batFINGER Yes, all meshes have a proper UV map. I have done my fair share of unwrapping and texturing so we can skip the basics. My main concerns are mentioned in my post. Any help would be greatly appreciated. – maplestick May 15 '18 at 17:25
  • Assuming cycles, and each material has an image texture node then you want to D.materials['Material'].node_tree.nodes['Image Texture'].image = D.images['Material'] which assigns the image named "Material" to the image texture of material named "Material" (ie same name for material and image) for all materials. Yes? can't see that being too difficult, it's late here, will get back with answer .. unless someone beats me to it 8*) – batFINGER May 15 '18 at 17:58
  • @batFINGER Unfortunately the materials do not have any nodes, but i appreciate the help. Feel free to take a nap for now thouh. – maplestick May 15 '18 at 18:09
  • Add details of your material set up to question.Eg how you manually add an image to one material (can then be done with script multiple times) Maybe a small sample file with a couple of objects. – batFINGER May 15 '18 at 18:15
  • @batFINGER As to your request i've added more details and provided a sample .blend file. – maplestick May 15 '18 at 19:02

1 Answers1

4

Script.

Similarly to How can I connect a texture node to a material with Python? the script below, Looks for all materials whose name has a corresponding image (note the ugly check for "_dds" to ".dds") Then for all material, image pairs adds an image texture node and connects it to the BSDF node.

Script assumes you have already loaded in the images, a simple process of multiple selecting images when opening.

import bpy

for m in bpy.data.materials:
    img = bpy.data.images.get(m.name.replace("_dds", ".dds"))
    if not img:
        continue
    if m.use_nodes:
        # done this already??
        continue
    m.use_nodes = True

    nodes = m.node_tree.nodes
    bsdf = nodes.get('Diffuse BSDF')
    if bsdf:
        # add image texture
        teximage = nodes.new('ShaderNodeTexImage')
        teximage.image = img            
        #link to bsdf
        m.node_tree.links.new(bsdf.inputs['Color'], 
                teximage.outputs['Color'])
batFINGER
  • 84,216
  • 10
  • 108
  • 233