Add prefix/suffix to material names via script

Dear all,

i’m new to scripting in rhino and don’t know the right syntax yet.
Perhaps someone could help with one inconvenience. :wink:

I like to work with worksessions, but i am really bad in properly organizing the used materials in each file.
When it comes to combine them in a worksession there are multiple materials with names like “custom 01”. The first “custom 01” is a carpet, the next wood, the next simple black, and so on…

The problem is, that rhino assigns one material (texture, glossyness, whatever) to all “custom 01s”. So that now all “custom 01s” get a carpet texture.

Is there a simple way scripting a prefix to all file materials?

Means, i have file named 01_floor, 02_walls, 03_ceiling and i can rename all used materials in each file with the number prefix of the file, in order to get “01 custom 01” for the carpet, “02 custom 01” for the wood, “03 custom 01” for the simple black…

Anyone any idea?

Thank you very much!


import scriptcontext as sc

mats = sc.doc.Materials
for m in mats:
    m.Name = '01_'+m.Name

try this in python
if you need to revert back the changes replace first line of for loop with this:
m.Name = m.Name.strip('01_')

1 Like

Perfect! Thank you!

hi Will_Wang
how would the script look like if i want to replace a part of the material name?
that it works as search & replace solution?
in my case i need to delete the “/” at the first position of some material names.

thank you :slight_smile:

cheers, laurentz

i played something around and this script brings up an error message, but it do rename my materials. :man_shrugging:

import scriptcontext as sc

mats = sc.doc.Materials
for m in mats:
m.Name = m.Name.replace(“x”, “y”)