Filtering for all InBuilt Material Names

CaptureDear All,
I have written the following script but it doesn’t seem to skip the creation of the material, if it already exists.
What is wrong about the following definition?

def create_set_material(mat_name):
Materials=Fec(doc).OfCategory(BuiltInCategory.OST_Materials).ToElements()
Elem=Fec(doc).OfClass(Extrusion).ToElements()
for m in Materials:
m_value=m.get_Parameter(BuiltInParameter.MATERIAL_NAME).AsString()
if mat_name in m_value:
continue
else:
NewMaterial= Material.Create(doc,mat_name)
for e in Elem:
El_Mat=e.LookupParameter(“Material”)
El_Mat.Set(NewMaterial)
return

Hi @PAnand,

Are you sure your Python code knows it already exist? Because it seems that your problem is that you forgot to build the entire list of material names before checking your material name!

Your current code checks the name of every material, one at a time. If I understand you correctly, your code should check every material at once.

Try to implement the following change to your code:

Materials=Fec(doc).OfCategory(BuiltInCategory.OST_Materials).ToElements()
Elem=Fec(doc).OfClass(Extrusion).ToElements()

m_value = list()
for mat in Materials:
m_value.append(mat.get_Parameter(BuiltInParameter.MATERIAL_NAME).AsString())
if mat_name in m_value:
INSERT REST OF YOUR CODE HERE

Hope it helps!

Thanks @lsmm, but even if I create a list of material values, it still doesn’t work the way it should.

@PAnand That sounds most puzzling…

Can you elaborate on what doesn’t work?
Is it solely the material creation or is it setting the new material?

I created a small script (for the sole purpose of creating the material), does that work on your machine?
FORUM_InbuiltMaterial.dyn (5.0 KB)

If the material is already in the environment, it wouldn’t skip to the next loop/step in the script. Which means, that setting the material to the extrusions wont work.
I get the following message " Exception: The given value for name is already in use as a material element name.
Parameter name: name"
As you can imagine, I have more parts in my script. They are all not executed because of this error message.

Try to change the indentation of the last part of the script. So that the for loop over materials finishes before the if-elif. See the dynamo script I posted earlier.

If it doesn’t make sense I will post a screenshot once I get home.

@PAnand try to remove one level of identation, as I stated in my previous comment.
I have attached an image (sorry for my poor paint skills), which hopefully explains what I mean adequately :blush:

Thanks @lsmm, I have changed the indentation as per your suggestion.
The only issue now is:
The material does not get assigned to the objects, if it already existed before I run the script, as I don’t get to loop through e in Elem…
How could I say in Python: Get the material which has name equaling “mat_name”?

@PAnand In order to do that, I would utilize that the order in the list of materials and material names are identical and use the built-in function “index” in Python to retrieve the material.

Somewhat like this
Capture

Thanks a lo @lsmm, it is working now…:):grinning:

@PAnand Glad that I could be of assistance :grinning: If the problem is resolved, could I get you to mark my last comment as the solution to your problem?

Done!