Hi all,
Straight to the problem. I have a script that creates a text file containing the names of meshes and the names of their materials. Then the script removes all the materials from all the meshes. Now, after removing all the materials, I need to add the materials back to the meshes based on the names in the text file.
here’s a small bit of the code:
# get text file - 'obj-mat_list' - that contains the object's names and
# materials assigned to it
input_filename = "obj-mat_list"
input = bpy.data.texts.get(input_filename)
print("
", input)
for index in input.lines:
if index.body:
if index.body[0] != "[":
print("
object =", index.body)
current_obj = index.body
bpy.context.scene.objects.active = bpy.data.objects[index.body]
bpy.ops.object.material_slot_add()
else:
print("
material =", index.body)
This code reads the text file, and if the line in the text file is the name of a mesh, it makes it the active mesh then creates a new material slot for the mesh, the next line of the text file is the name of the material to be assigned to the mesh.
The text file that my script produces looks like this:
Cube
['Blue']
Icosphere
['Green']
Sphere
['Red']
So the first bit of code goes thru the text file, gets the mesh object’s name and makes it the active object, then it adds a new material slot. Now I need to assign the material to the object’s new material slot.
For example, the first line of the text file is the name of the mesh object ‘Cube’, the second line is the name of the material (it’s a python list in case there are multiple materials) as seen in the material panel.
So how do I assign the material named ‘Blue’ to the mesh ‘Cube’ after I’ve created the material slot? I need to do the assignment in the ‘else’ statement in the first bit of code.
Thanks for any help, and if any more information needed, I can supply that.
Randy