Blender: Add same texture to all materials

If you are tired of adding the same texture to all materials one by one, the following Python script would save your time a lot. This script was tested in Blender 2.71.

import os
import bpy


# All the objects you want to add texture should have at least one material.
def add_envmap_texture_to_all_materials(img_fname):
    img_fname = os.path.expanduser(img_fname)
    try:
        cubemap_img = bpy.data.images.load(img_fname)
    except:
        raise IOError("Cannot load image %s" % img_fname)

    cubemap_tex = bpy.data.textures.new('CubeMap', type='ENVIRONMENT_MAP')
    cubemap_tex.environment_map.source = 'IMAGE_FILE'
    cubemap_tex.image = cubemap_img
    cubemap_tex.use_mipmap = True
    cubemap_tex.use_mipmap_gauss = True

    for m in bpy.data.materials:
        t = m.texture_slots.create(1)  # Always add to slot #1.
        t.texture = cubemap_tex
        t.texture_coords = 'REFLECTION'


if __name__ == '__main__':
    add_envmap_texture_to_all_materials('~/Downloads/miramar.jpg')
Advertisements
Blender: Add same texture to all materials

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s