How to add a texture to an object with Python in Blender 2.5 ?

Just a very simple problem I think, but how can I make a little piece of
Python 2.5 script for this :

  • Tell me which is the active object ?
  • Create a new material
  • Create a new texture : type Image or Movie
  • Load an image in the texture

Thanks for help

Tonpy :rolleyes:

Someone should really make this video a sticky for this forum.

Every one should watch this video. It shows you how to help yourself. Basically Blender 2.5 has a feature to show you what python code you just executed for any action in the GUI. So if you want to know how to create a texture just put the console in report mode, create an actual texture in the interface and watch Blender 2.5 spit out the code needed to perform such an action. If you need an armature, just do the same thing.

So you can see how important it is to watch this video. It shows you how to setup the console to report back the code that your latest actions just generated.

not certain about this one ?

i tested that yesterday for a script i;m working on 25363
and i added an plane and tried to add amaterial to i

but there was nothing in script window!

and i was in scrpt scene

so why the mat are nt shown there ?

i’ll recheck that video it’s a good one anyway !

Thanks

Looks like you are correct. I can see the code for creating a new texture or material, but not changes to the texture (like switching from cloud to musgrave).

In those cases, I guess that is where the bubble help python code comes in handy. You can always put the outliner in Datablock mode and examine the entire scene structure. Hover over an element in the tree and the popup help will give you a brief code syntax for accessing that element/property. This popup help also appears in the Properties window as well.

You can also derive what methods and properties are availabe from any given variable with the print(dir()) function combination.

So I see that the console spits out.

bpy.ops.texture.new()

I create script like this.


myTex = bpy.ops.texture.new()
print(dir(myTex))

Now, if I examine the Blender window I can see what further options I can use to operate on “myTex”. This is just how I work. I do pop in and read the docs on occasion, but with popup help, the console and print(dir()) I can ususally figure out what the syntax is for the next step in my script. This is a great improvement over 2.49 (IMHO).

i got 2600 version

seems to be better a little

now i look at the mat API page
http://www.blender.org/documentation/250PythonDoc/bpy.types.Material.html#bpy.types.Material

this begins with
bpy.types.material (ID)

now this is not adding a new mat
but seems to be used for changing the parameters for a specific mat !
but how do you use this ?
where do you put it - is it just after adding a new mat or how do you select a mat ?

is is after adding a new mat in the scene
like in bpy.ops.material.new() ?

there is no example on how to write it
if i have to i’ll write a new page for wiki to show this ounce i know how to use it !

note: understanding the formal desription is not easy for non programmer like me
and the best way i guess is to give a minimum qty of examples
then it becomes a lot easier to understand and use it !

for the outliner can you show what you mean

causei don’t see bpy command
i can extend and see mat and texture ect…
but no equivalent bpy command shown as a tool tiop for instance

Thanks

import bpy

def loadImage(imgName):
    img = bpy.data.add_image(imgName)
    tex = bpy.data.textures.new('TexName')
    tex.type = 'IMAGE' 
    print("Recast", tex, tex.type)
    tex = tex.recast_type()
    print("Done", tex, tex.type)
    tex.image = img
    mat = bpy.data.materials.new('MatName')
    mat.add_texture(texture = tex, texture_coordinates = 'ORCO', map_to = 'COLOR') 
    ob = bpy.context.object
    bpy.ops.object.material_slot_remove()
    me = ob.data
    me.add_material(mat)
    return

loadImage('/home/thomas/picture.jpg')

and many thanks to ThomasL…
It’s exactly what I want…

Tonpy

i tested it on vista

but get an error on the path name the last line for the file location

any special way of doing it for windows?

also when you execute this script
where would you see the texture?
i mean on the selected cube
as a texture or a UV texture ?

Thanks

You have to change the pathname in the last line to where the picture is located, of course. Btw, I have created a C:\home homas folder on my windows box to be able to move files between windows and ubuntu.

The texture shows up as an ORCO texture in the color channel on the default cube. I assume that the selected object is a mesh, and that it has exactly one material already.

very interetsing

i whish there was more example like this
it would go much faster to learn 2.5 scripting

ok but is there any example showng how to add
more then one material and slot for it

but also is there some example on how to select faces for one mat
and add some texture like cloud or mable ect to it

that would be very usefull
to complete this example and be able to add whatever mat or text you can with 2.5

keep up the good work

Thanks

import bpy

def createMaterials():
    # Image texture
    imgPath = '/home/thomas/picture.jpg'
    img = bpy.data.add_image(imgPath)
    imtex = bpy.data.textures.new('ImageTex')
    imtex.type = 'IMAGE' 
    imtex = imtex.recast_type()
    imtex.image = img

    # Marble texture
    mbtex = bpy.data.textures.new('MarbleTex')
    mbtex.type = 'MARBLE' 
    mbtex = mbtex.recast_type()
    mbtex.noise_depth = 1
    mbtex.noise_size = 1.6
    mbtex.noisebasis2 = 'SIN'
    mbtex.turbulence = 5

    # Cloud texture
    cltex = bpy.data.textures.new('CloudsTex')
    cltex.type = 'CLOUDS'
    # Cloud texture by default, don't need to recast
    cltex.noise_basis = 'BLENDER_ORIGINAL'
    cltex.noise_size = 1.05
    cltex.noise_type = 'SOFT_NOISE'

    # Create new material
    mat = bpy.data.materials.new('TexMat')
    mat.alpha = 0

    # Map image to color, this is the default
    mat.add_texture(texture = imtex, texture_coordinates = 'UV')
    im_mtex = mat.textures[0]
    
    # Map marble to specularity
    mat.add_texture(texture = mbtex, texture_coordinates = 'UV', map_to = 'SPECULARITY')
    mb_mtex = mat.textures[1]

    # Map cloud to alpha, reflection and normal, but not diffuse
    mat.add_texture(texture = cltex, texture_coordinates = 'UV', map_to = 'ALPHA')
    cl_mtex = mat.textures[2]
    cl_mtex.map_reflection = True
    cl_mtex.map_normal = True

    # Create new material
    mat2 = bpy.data.materials.new('Blue')
    mat2.diffuse_color = (0.0, 0.0, 1.0)
    mat2.specular_color = (1.0, 1.0, 0.0)
    
    # Pick active object, remove its old material (assume exactly one old material).
    ob = bpy.context.object
    bpy.ops.object.material_slot_remove()

    # Add the two materials to mesh
    me = ob.data
    me.add_material(mat)
    me.add_material(mat2)

    # Assign mat2 to all faces to the left, with x coordinate > 0
    for f in me.faces:
        left = True
        for v in f.verts:
            vert = me.verts[v]
            if vert.co.x < 0:
                left = False
        if left:
            f.material_index = 0
        else:
            f.material_index = 1
    return

createMaterials()

i whish i could put a pic here!

very nice thanks for this more complete example

and i change that the path name for the file
now i ran the script and got no errors on console
if i used your filepath name

but if i put my own path on my disk
i get an error like
cannot decode unicode from 2 to 4
i did use Tab at the beginning so i guess
it did not like this tab but all my script are done with tab
so not certain why i get this error on this line

now on the render i did not see the image texture on the cube

any idea why this is not seen

but i did see 2 mat and on the first mat there was 3 textures
so i guess this part is working nicely

note: to see pic ic an open a new WIP where i can add pic anytime!

this is the first time i see good example for mat and text
don’t know where you’v seen this but it’s very good doc

so Thanks very much for this intro

i did a new script to make another example for a wood texture

and mostly working but still get the problem that the render does not show the wood texture

here this simple script for wood texture

###############

def MakeMaterials():

Wood texture

texturename1=‘WoodTex’

text1 = bpy.data.textures.new(texturename1)
text1.type = ‘WOOD’ # Texture type = Wood
text1 = text1.recast_type()
text1.noise_type = ‘SOFT_NOISE’ # ‘SOFT_NOISE’ ‘HARD_NOISE’
text1.noise_size = 1.1 #

text1.noisebasis2 = ‘TRI’ # ‘TRI’ ’ SAW’ ‘SIN’
text1.turbulence = 25 # 0.0001 to inf
text1.stype = ‘RINGNOISE’

Create new material

Materialname1=‘Mat1’
mat = bpy.data.materials.new(Materialname1)
mat.alpha = 0.8
mat.diffuse_color = (1.0, 0.0, 1.0) # This diffuse color will take a Magenta color

Map Wood texture to specularity

mat.add_texture(texture = text1, texture_coordinates = ‘UV’, map_to = ‘SPECULARITY’)
MAT1_mtex = mat.textures[0] # This the second material in list 0 ,1

Create new material

Pick active object, remove its old material (assume exactly one old material).

ob = bpy.context.object
print (’ Object name The Cube remove mat =’,ob.name)

bpy.ops.object.material_slot_remove()

Add the two materials to mesh

me = ob.data
me.add_material(mat)

MakeMaterials()

##################

i can make the wood text t appears if i manually set the color in text panel

any idea why this wood text does not appear in render?

also i’m trying to locate all the vars for text in intensity panel at the bottom of texr panel
i think it is on this API page
http://www.blender.org/documentation/250PythonDoc/bpy.types.MaterialTextureSlot.html#bpy.types.MaterialTextureSlot.diffuse_factor
but not certain how to write these for the mat?

any idea would be welcome !

there are lot’s more vars i think in 2.5 then old 2.4x
but it’s fun to learn this new way in 2.5

Thanks
happy 2.5