Assigning Materials

Ok,
So i’v been pounding my head agnst blender and python for the last few hours and while i’v been making great progress on my first script for blender, and infact my first time useing python, I’v come to a point where i could use some help.
My script generates objects and places them around a scene, for each object it generates it assigns a material that has already been made. this is where i could use some help. I’m thinking that useing Blender.Object.SetMaterials() like so…

am = Blender.Object.Get(name_)
am.setMaterials([‘TestMat’])

this gives me an error.
“ValueError: Material list must be a list of valid materials!”
Any ideas on what i’m doing wrong? I’v defined (in the scene, not in python) TestMat as a material and have gone back to check the spelling… Thanks!
Neon

There are two things I’d like to point out.

  1. You’re providing the setMaterials function with a string, while it’s expecting a material object.
  2. Usually materials are linked to the mesh data, not the object data.

So that gives us the following:

import Blender
from Blender import *

ob = Object.Get("MyObject")   # MyObject is the name of your object, change it to anything you wish
me = ob.getData(False,True)   # Now we've retrieved the mesh data
mat = Material.Get("TestMat") # This is the material object I was talking about
me.materials = [mat]          # Link the material object to the mesh data

OK. that makes more sence to me.

Actually I was never hardcoding anything, I made this script up from a .blend file with 3 Bezier Curbs.
The Goal is to have 3 circles where you can change the size of all three in one step, making them look like a target.


import bpy
import Blender
from Blender import *
def ciblePara(r) :
	circle00=Object.Get("circle00") 
	circle01=Object.Get("circle01")
	circle02=Object.Get("circle02")
	mesh00=circle00.getData(False,True)
	mesh01=circle01.getData(False,True)
	mesh02=circle02.getData(False,True)
	circle00.setLocation(0,0,0.02)
	circle01.setLocation(0,0,0.01)
	circle02.setLocation(0,0,0)
	circle00.setSize([r,r,0])
	circle01.setSize([r+1,r+1,0])
	circle02.setSize([r+2,r+2,0])
	mat00=Material.Get("mat00")
	mat01=Material.Get("mat01")
	mat00.setRGBCol([255,0,0])
	mat01.setRGBCol([255,255,255])
	mesh00.materials=[mat00]
	mesh01.materials=[mat01]
	mesh02.materials=[mat00]
ciblePara(3)

Thanks for this reply, I’d never have figured out!