Python for loop with variables Find unused materials

   8674   7   2
User Avatar
Member
218 posts
Joined: March 2006
Offline
Hi,
not sure if it's OK to ask questions about Python…
I make a script to find all unused materials and I think I'm on the right way but trying to loop through the possible numbers of materials in a material sop drives me crazy;o)

the variable “o” comes from a loop above and works fine.



for number in range(1,10):
tmpstr = “shop_materialpath”+str(number)
print bytearray(o.parm(tmpstr).eval())



brings up the error “AttributeError: ‘NoneType’ object has no attribute ‘eval’”

How can I pass “shop_materialpath1”, “shop_materialpathn” to the for loop?

Thanks for help

Detlef
Edited by - Dec. 18, 2014 11:29:56
User Avatar
Staff
4565 posts
Joined: July 2005
Offline
Your code works fine for me, except my Material SOP only has 3 materials, so when number gets to 4, I get the same error you showed in your post (because the Material SOP has no shop_materialpath4 parameter, thus the None return value from the o.parm() function). The solution was to change the for loop to:

num_mats = o.parm(“num_materials”).eval()
for number in range(1,num_mats+1):
tmpstr = “shop_materialpath”+str(number)
print bytearray(o.parm(tmpstr).eval())

Hope that helps,
Mark
User Avatar
Member
218 posts
Joined: March 2006
Offline
thanks… this will work.
BTW I am new in python and can't find a function like in PHP “in_array” to search inside an array.

Detlef
User Avatar
Member
218 posts
Joined: March 2006
Offline
Thanks for help

here is my script to find all unused materials in a job.
Attention! I will guarantee for nothing and maybe one can optimize it. I can't find in python “unset” or “array_unique” from PHP, this can speed up a little bit and maybe a “break” can help;o) def UnusedMaterial():
shop = hou.node(“/shop”).children()
matArray =
foundArray =
for s in shop:
s_path = ‘/shop/’ + str(s)
matArray.append(s_path)

object = hou.node(“/obj”).allSubChildren()
for o in object:
if o.type().name() == “geo”:
if bytearray(o.parm(“shop_materialpath”).eval()) != '':
if bytearray(o.parm(“shop_materialpath”).eval()) in matArray:
foundArray.append(bytearray(o.parm(“shop_materialpath”).eval()))

elif o.type().name() == “material”:
num_mats = o.parm(“num_materials”).eval()
for number in range(1,num_mats+1):
tmpstr = “shop_materialpath”+str(number)
if bytearray(o.parm(tmpstr).eval()) in matArray:
foundArray.append(bytearray(o.parm(tmpstr).eval()))

for value in matArray:
if value not in foundArray:
hou.node(value).setColor(hou.Color((5,0.0,0)))

have fun

Detlef
User Avatar
Member
218 posts
Joined: March 2006
Offline
UPS…

this num_mats = o.parm(“num_materials”).eval()
gave me an error in other files than tested:

AttributeError: ‘NoneType’ object has no attribute ‘eval’
User Avatar
Member
624 posts
Joined: Aug. 2008
Offline
since we know that we only put the materials in the geo node, we don't use material sops.

was easy to make a script that make a list with all the string from the geo node /material parm.

make a set of that so we remove repeated items in the list,
and then compare that list to the materials in the shops.
User Avatar
Staff
4565 posts
Joined: July 2005
Offline
winkel
UPS…

this num_mats = o.parm(“num_materials”).eval()
gave me an error in other files than tested:

AttributeError: ‘NoneType’ object has no attribute ‘eval’

Reading your code I'm not sure why that would happen. I'd start by printing out information about “o” (full path to the node, node type, etc) so you can see which node is causing this error. Once you know that you should be able to modify the code to avoid the problem.

Mark
User Avatar
Member
218 posts
Joined: March 2006
Offline
Hi Mark,

I found out, that it was a problem with exiting Shopnets with the name “materals”
But as I read before maybe I'm the only one who uses material-sops in case of geo->material.

So forget the thread… I'm happy with the script because I have tons of old hip-files and often merge them with new ones, so I run into the problem with a lot of materials I don't need and now I can delete them easily…

And thanks again for the code!

Detlef
should I never use material sops? why?
  • Quick Links