Assign new material for each unique name in loop

   5470   4   1
User Avatar
Member
3 posts
Joined: Sept. 2019
Offline
Is there a way to loop through objects that have a material name attribute and create a new material (within a material network in an HDA) for each of them that is unique? I need to be able to output an HDA for import into a game engine and have enough material slots for each of the generated objects.

What ends up happening is instead of creating new materials, it renames the existing one and assigns it to each of the objects that same material.

I asked a similar question in the UE4 forum, but no one there knew.

-Jeff

Attachments:
BoxesHelp.hip (280.5 KB)

User Avatar
Member
201 posts
Joined: July 2015
Offline
You cannot create a new material in a foreach loop. Materials live outside of SOPs so you cannot creatr them there, you can only assign them.

You can however create a materials with python.

You can have a python sop that loops over the input primitives, collects the unique “shop_materialpath” attributes and creates materials for it.

I hope this helps.
Manuel Köster - Senior Technical Artist @Remedy Entertainment

https://www.remedygames.com/ [www.remedygames.com]
http://shadesoforange.de/ [shadesoforange.de]
https://twitter.com/ShadesOfOrange_ [twitter.com]
User Avatar
Member
149 posts
Joined: Aug. 2014
Offline
I can't speak for HDA-> ue4 but I do know that if you export to FBX you don't actually need to create a material in houdini, it just needs to be assigned @shop_materialpath in primitives and when exported and imported into ue4 those sub materials will show up (they will be default materials of course).
shadesoforange is right though if you actually need the materials in houdini then python is the right path.
Edited by electricalpencil - Oct. 3, 2019 12:12:52
User Avatar
Member
3 posts
Joined: Sept. 2019
Offline
Thanks.

I've not worked with Python Nodes yet, but that seems to be the way to go. I don't suppose anyone has an example that I could crib from?

-Hawke
User Avatar
Member
149 posts
Joined: Aug. 2014
Offline
really simple but

contextNode = hou.node(“/mat”)
mat = contextNode.createNode(“principledshader”, “matname”)

I'd recommend looking through the houdini python docs,
for example you can read what parameters the createNode expects:
http://www.sidefx.com/docs/houdini/hom/hou/Node.html [www.sidefx.com]

edit just tried to put my code above into the python script node and get an error, not sure why taking a look but I prefer to run python scripts using the method shown here, http://www.tokeru.com/cgwiki/index.php?title=HoudiniPython [www.tokeru.com]

basically you create a null, edit the parameter interface to add a run button, and link the run button to a code field in the node, that way its not running every time you select the node or change anything down/upstream, the code I posted above ran without error with this method.

double edit: ok for some reason the quotation marks above don't paste correctly into houdini, retype those in houdini and should work in python node too
Edited by electricalpencil - Oct. 3, 2019 16:14:19
  • Quick Links