# How to rename active or first texture slot of a material from script

I have a tool that I've written that takes blend files and converts them into quake 3 maps. This automates a ton of stuff that saves me a lot of time, but there's still several tedious steps before getting to that. One of those steps is splitting complex meshes up and generating textures and materials for each object. To keep things simple for editing, I'd like to have object name, mesh name, material name and texture name be the same for each object.

To accomplish this, I've found and used the batch rename data blocks script located here. For how great this script is, there's one thing it lacks, that being the ability to rename the active or first texture for the materials. I've attempted to access material.texture_slots[n].name and change this value, but got an error that it was read-only.

What is the appropriate api/process to rename images attached to materials? For what it's worth, all images are packed, generated textures.

• While the script you mention doesn't appear to be updated you should look in the new contrib addons git repo instead of the old svn one. Sep 14 '15 at 13:01
• @sambler thanks for pointing that out. :)
– user1745
Sep 14 '15 at 13:24

I realize I just posted the question but I solved this myself not long after. You can also rename the texture names and texture image names with this script by modifying the block of code that renames materials like so:

        if self.rename_material:
if ob.material_slots:
for m_slot in ob.material_slots:
if m_slot.material:
if m_slot.material.users == 1:
if (self.rename_use_prefix
and self.prefix_material):
m_slot.material.name = self.rename_prefix + name
else:
m_slot.material.name = name
if m_slot.material.texture_slots:
if(len(m_slot.material.texture_slots) > 0):
m_tex = m_slot.material.texture_slots[0]
m_tex.texture.name = name
m_tex.texture.image.name = name


Obviously there's some checks that could be done such as number of users and such. I'll post back as I polish the script.

• "I'll post back as I polish the script." -- And yet, this never happens, as expected. :-) Mar 22 '17 at 20:49