2.5 material

How do you add Ztransp and some alpha value to the a material?

i added a mat like this

ob = bpy.context.scene.objects[name] # Get object named “Mesh” in scene
myMesh = ob.data
print (‘ob=’,ob)

Create a material

mat = bpy.data.materials.new(Materialname1)
mat.alpha = 0
rgbCol = [0.2,0.4,0.5]
mat.diffuse_color = (1.0, 0.0, 1.0) # This diffuse color will take a Magenta color

print (‘ob=’,ob)

but the alpha don’t see to do anything here !


mat.alpha = 0
mat.transparency = True

will do the trick.

According to http://www.blender.org/documentation/250PythonDoc/bpy.types.Material.html, the transparency_method defaults to Z_TRANSPARENCY.
So no need to add that too.

working nicely thanks

now how do you set up transp to see it in the viewport which is not part of the mat i guess
but more object or mesh may be ?

and how to make it smooth
do you have to do it for each face or the whole object ?


the material slots hold materials, the faces have an index that says which material slot they will use.

to assign your material to the faces you need to know the index of the slot it’s in and then set the face to use it I haven’t tested the following!

your mesh object is myMesh, your material is mat:

matindex= myMesh.materials.index(mat)

for f in myMesh.faces:
    f.material_index = matindex

not to certain about finding the material index, you may need to go through the texture slots on teh object instead…

once you know that the faces are using the material then just turn on glsl display…and set rendering to textured…

textures with alpha can be assigned to the faces in the UV editor and set mesh flags set to use alpha, this means you can see the transparency in solid view (when" solid tex" is enabled) but that’s a different story… I’m not sure the face flags can be set properly yet in 2.5

Visible transparency in the viewport:

Object panel, Display properties, Transparency checkbox

Ah, sorry… totally forgot, that this is about scripts.
Object.draw_transparent is the key.

still having problem to read this API - sorry i’m not a programmer but learning as i go along

on example and i should be able to use all the other methods in this API page for object

with the given ob and mesh as given in the first post

this object thing in that API page does it correspond to the object in blender or the mesh
this is still so confusing !

can you give one example to set the transp in viewport for this object and if you can also the smooth in python

then i should be able i guess to use all the other parameters in that object page!

thanks guys

Using your example code:

ob = bpy.context.scene.objects["Mesh"] # Get object named "Mesh" in scene
myMesh = ob.data

# Create a material
mat = bpy.data.materials.new(Materialname1)
mat.alpha = 0.2
mat.transparency = True
rgbCol = [0.2,0.4,0.5]
mat.diffuse_color = (1.0, 0.0, 1.0) # This diffuse color will take a Magenta color

# Add it

# viewport transparency
ob.draw_transparent = True

Smooth shading:


Selecting the faces is a different story, I guess.
But since I’m pretty new to scripting, I can’t help you with that.

by the way i think the RGB color line is not needed
or you use this vector and apply it to the next line!

smooth and transp in viewport seems to be working nicely


Totally did not see, that it’s not beeing used.

If you want the complete object smooth, you might as well use just one line:
and get rid of switching to/from editmode.

but in first example there was no faces selection?

so how is it applied here ?


Well I was a bit sloppy there.
New objects will have all verts selected when switching to editmode.
But of course you can’t assume that this is always the case (everything selected).

So, with the editmode-example, you’d have to do:

bpy.ops.mesh.select_all( action = 'SELECT' )   #select everything

(only problem left: what if we were already in editmode)

As I said, I don’t know (yet) how to select verts/faces that belong to a specific material.
Everything so far I’ve learnt mostly by switching to the scripting-layout in blender and looking at the commands from the Report console while messing around.

if bpy.context.mode == 'OBJECT': print("in object mode")
elif bpy.context.mode == 'EDIT_MESH': print(" what did you spec?")