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!

Seb.

import scriptcontext as sc

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

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”)
m.CommitChanges()

Hi,

This script does not work for me unfortunately

Which one?
In what way doesn’t it work?
What are you trying to do?

Hi,

I was trying to rename all my materials, i want to add a Prefix/suffix to all my materials and i was trying to use the below script.

import scriptcontext as sc

mats = sc.doc.Materials
for m in mats:
m.Name = ‘01_’+m.Name
m.CommitChanges()

The script runs with no errors but the prefix does not get applied to some materials, or is applied multiple times for some materials, would be helpful if someone could help me with this

thanks

Hi @Akazwarman_Balachand,

There are a couple of issues with your script:

Here’s the revised version:
RenameMaterials.py (117 Bytes)

HTH
Jeremy

[Edit: I initially uploaded the wrong file - have now replaced it with right one]

These might be caused by pasting the script text directly into Discourse without using code formatting.

If I take this:

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

and just paste it in here without formatting it for code, I get this:

mats = sc.doc.Materials
for m in mats:
m.Name = ‘01_’+m.Name
m.CommitChanges()

Yup, that is because regular HTML rendering doesn’t preserve whitespace, with the exception of the <pre> tag, which is what is used to do code formatted bits.

Well true, but the script didn’t work for @Akazwarman_Balachand and does work once those errors are corrected so it’s reasonable to deduce that one or both were in the script before he posted it…

OK, I didn’t see a response to say that it’s now working…

I see the problem description has changed (the deleted post which I acted on said no material names were changed). The corrected script amended all the materials in a file I tested it on as expected. The revision has muddied the waters and we need the poster’s file (preferably a copy from before the script was run) to understand the problem better.

Hi,

Thank you, the script worked, but now i started having a different issue, few materials weren’t renamed and the other couple of materials had ‘01’ repated again and again. for example
" 01_01_01_01_Concrete". Any idea why this is happening ?

Akaz

Hello,

It sounds like the order of the materials list is being modified during iteration. You could try creating a new list before iterating:

mats = list(sc.doc.Materials)