materials on object

1- trying to add some basci material on one cube object

i tried the following code


 
from Blender import Material, Texture
 
 
 
create meshes
mesh1 = Mesh.Primitives.Cube(size)

mat = Material.New('Material')
 
mesh1.materials.append(mat)
#mat.rgbCol = [sliderR.val, sliderG.val, sliderB.val]
 
mat.setRGBCol([1.0,0.2,0.4])
mat.setSpecCol([1.0,1.0,0.8])
mat.setMirCol([0.8,0.9,0.7])
mat.setAlpha(1.0)
mat.setRef(0.8)
mat.setSpec(0.5)
mat.setHardness(50)
 
 

and it did not give any error but the color of the code is not changed?

hoe can this be corrected so that it work ok?

Thanks

Here is my experiments with adding materials to a python created cube.


import Blender
from Blender import *

def makeCube(x,y,name,passedMesh,passedScene):
    ob = Object.New("Mesh",name)
    ob.LocX=x
    ob.LocY=y
    
    ob.link(passedMesh)
    passedScene.link(ob)

    return ob
    
#Create cubes with materials.
localScene = Scene.GetCurrent()

matRed = Blender.Material.New('matRed')
matRed.rgbCol = 1,0,0

matBlue = Blender.Material.New('matBlue')
matBlue.rgbCol = 0,0,1

redMesh = Mesh.Primitives.Cube(1)
redCube = makeCube(0,0,"redCube",redMesh,localScene)
redMesh.materials = [matRed]

blueMesh = Mesh.Primitives.Cube(1)
blueCube = makeCube(2,0,"blueCube",blueMesh,localScene)
blueMesh.materials = [matBlue]

mixedMesh = Mesh.Primitives.Cube(1)
mixedCube = makeCube(4,0,"mixedCube",mixedMesh,localScene)

#Lets add to materials to this mesh.
mixedMesh.materials = [matRed, matBlue]

#Manually assign faces a material index. (We know the cube has 6 faces i.e. 0-5).
mixedMesh.faces[0].mat = 0    #Face #0 gets index #0 which is Red.
mixedMesh.faces[1].mat = 1    #Face #1 gets index #1 which is Blue.
mixedMesh.faces[2].mat = 0
mixedMesh.faces[3].mat = 1
mixedMesh.faces[4].mat = 0
mixedMesh.faces[5].mat = 1

Redraw(-1)
localScene.update(1)

why is it indicated as

mat.setRGBCol([1.0,0.2,0.4])
in the tut Jm soler

it’s not the same way as show in your script

ok you’v given me antoher way to do it

but i would like to understand with the set command too
and how it is done for the tut - i mean there are so many other variables in that tut

i’d like it to work cause it’s a good tut !

Thanks

You got a link to this tut?

http://www.linuxgraphic.org/pdf/

it’s called materiall lg 03

seems to be a good intro but may be out of date with the latest API!

i tried to copy the whole script from the tut
but there is a ref to an image and don’t know how to modify it so it can read into my own folder!

img = Image.Load(’/home/olivier/Documents/Articles/Linux Magazine/blender-lm-
03/files/tile1.jpg’)

how can thsi be chnge to work on windows current folder?

but ih any case the color for the set command should be working unless [email protected]

Thanks

here is a code with one cube only with colors

but the colors are not beign done on the cube

but there is no errors


 
import Blender
from Blender import Mesh, Scene
from Blender import Material,Texture
 
#print spacers

print
print
print
print "------------------------------------"
print
#set the mesh size - ex: .5 (small and distinct) or 1 (large and sigular)
size = .5
#create meshes
mesh1 = Mesh.Primitives.Cube(size)
mat = Material.New('Material')
mesh1.materials.append(mat)
#mat.rgbCol = [sliderR.val, sliderG.val, sliderB.val]
mat.setRGBCol([1.0,0.2,0.4])
#mat.setRGBCol=([1.0,0.2,0.4])
mat.setSpecCol([1.0,1.0,0.8])
mat.setMirCol([0.8,0.9,0.7])
mat.setAlpha(1.0)
mat.setRef(0.8)
mat.setSpec(0.5)
mat.setHardness(50)
 
#get the scene
scene = Scene.GetCurrent()
#link object to the scene
meshob = scene.objects.new(mesh1)
#set location
meshob.setLocation(0,0,0)
 
#all objects in the scene
oblist = list(scene.objects)
print "
All objects in the scene: 
", oblist
#only mesh objects in the scene
oblist = [ ob for ob in oblist if ob.type == 'Mesh']
#reorder the list - not required
oblist.reverse()
print "
Only mesh objects in the scene: 
", oblist
#join 1st object in list to remaining objects in list (slice)
oblist[0].join(oblist[1:])
print "
First object: 
", oblist[0]
print "
Remaining objects: 
", oblist[1:]
#unlink the "remaining" objects from the scene
for i in range(1, len(oblist)):
 scene.objects.unlink(oblist[i])
#update the scene
scene.update(0)
#redraw
Blender.Redraw()
 

try this one it is very simple and should work but is not!

Thanks

lg 03 is interesting cause there is a lot of other variables that you can set

like mirror trnasp reflec ect…

now i was able to make another one work with mesh


 
import Blender as B
from Blender import Mesh, Scene
from Blender import Material,Texture

# vertices
p1 = [0,0,0]
p2 = [1,0,0]
p3 = [0.5, 1, 1]
verts = [p1,p2,p3]
faces = [0,1,2]
# create Object
ob = B.Object.New('Mesh', 'MeshOb')
# create ObData
me = B.Mesh.New('myMesh')
# add vertices
me.verts.extend( verts )
# create faces
me.faces.extend( faces )
 
# first object in selection list
#ob = B.Object.GetSelected()[0]
# get the Mesh version of our cube, not NMesh
#me = B.Mesh.Get( ob.data.name )
# create some materials   this add 3 materials to the object  one  red  blue and green
red1 = B.Material.New('Red1')
red1.rgbCol = 1,0,0     # red, green, blue values
green1 = B.Material.New('Green1')
green1.rgbCol = 0,1,0    # red, green, blue values
blue1 = B.Material.New('Blue1')
blue1.rgbCol = 0,0,1    # red, green, blue values
# add the material list to our ObData
me.materials = [red1, green1, blue1 ]
# set the face materials of our cube
# we can set all faces the same color like this
for f in me.faces:
 f.mat = 0
 
################################################
#create new  meshes  cube
size=2
mesh1 = Mesh.Primitives.Cube(size)

#mat = Material.New('Material')
mat = Material.New('Material')
mesh1.materials.append(mat)
mesh1.materials = [red1, green1, blue1 ]
#for f in mesh1.faces:
# f.mat = 0
 
#  Different faces  color
for i in range(len(mesh1.faces)):
 mesh1.faces[i].mat = i % 3
 
 
 

#get the scene
scene = Scene.GetCurrent()
#link object to the scene
meshob = scene.objects.new(mesh1)
#set location
meshob.setLocation(4,4,0)

ob.link( me )
scene = B.Scene.GetCurrent()
scene.link( ob )
B.Redraw(-1)
 
 
 

now this wseems to work but
i dont’ have the name of other variables for the mirror tranps ect…!

Thanks

seems that lg-03 uses Nmesh to make new objects

and this has been deprecated and replace by Mesh object!

1 -
this seems to be an intneresting way to save space in a script i’m preparaing

in script example you’v given to me for color and materials textures
call to function is :
mixedCube = makeCube(4,0,“mixedCube”,mixedMesh,localScene)
then this function creates an object and ob
but use an internal name for it

def makeCube(x,y,name,passedMesh,passedScene):

ob = Object.New("Mesh",name)
ob.LocX=x
ob.LocY=y

ob.link(passedMesh)
passedScene.link(ob)
return ob

would it be possible to pass a name for the new object in parameters
or may be change ti afterward to another name?

2 - the "mesh1.setMaterials([mat_mat,mat_granit ]) command "
exist only for Nmesh which is deprecated and replaced by another Mesh object

Thanks