Jump to content

Mimic "CreateAllLocalMaterialParameters"


kgoossens

Recommended Posts

Hi All,

I'd like to access or emulate the "Create All Local Material Parameters"

CreateAllLocalMaterailParameters.png

Resulting in

GeometryFolderAddedUnderMaterial.png

I'm still beginning to use Python, but I'm a bit lost on how I should approach this. I think I've got the beginning, but then...

shop = hou.node('/shop/material1').parmTemplateGroup()

hou.node('/obj/geo2').setParmTemplateGroup(shop)

spareFolder.png

It creates all the parameters under a "Spare" folder and puts the existing parameters under a "Standard" folder. Something that is totally different. Looking through the ParmTemplateGroup functions I'm unable to find/understand yet how I should approach this. :huh:

Putting me on the right track would be greatly appreciated by this derailed person.

Link to comment
Share on other sites

here's how you do it

shop = hou.node('/shop/clay')
shopptg = shop.parmTemplateGroup()
node = hou.node('/obj/box_object1')
ptg = node.parmTemplateGroup()
for i in shopptg.parmTemplates():
    ptg.appendToFolder(ptg.findFolder("Material"), i)

node.setParmTemplateGroup(ptg)

cheers,

Manuel

Thanks a lot Manuel :) It works in the sense that it adds the parameters on the right spot, but there seems more things going on with the delayed load. Because if I use the "Create All Local Material Parameters" then the instancing works.

If I add the parameters with the script it doesn't work. Has anybody have some ideas on this?

DelayedLoadInstance-01.png

Using script

DelayedLoadInstance-02.png

I've also attached the scene file.

DelayedLoadInstance.rar

Edited by kgoossens
Link to comment
Share on other sites

The problem is that material override parameters need to have a special parm tag to flag them as being material overrides: "material_spare" = "1". This needs to be set on every parameter that is being applied. The following function is what I use to handle this type of thing. Given the object node to apply the operation to, it will find the material node, ensure it's actually a material, then take care of modifying all the parameters and sticking them in the right place.

def applyLocalOverrides(objnode):
    """ Promote all material parameters from the Material SHOP
        to the node.
    """
    # Get the path to the material.
    material_path = objnode.evalParm("shop_materialpath")
    # Try to find the node.
    material_node = objnode.node(material_path)

    # The node type for the SHOP material node.
    material_type = hou.nodeType(hou.shopNodeTypeCategory(), "material")

    # If the material node doesn't exist we need to throw an error.
    if not material_node:
        msg = "{0} is not a valid node.".format(material_path)
        raise hou.OperationFailed(msg)
    # If the material node is not actually a material we need to throw
    # an error.
    if material_node.type() != material_type:
        msg = "{0} is not a material.".format(material_path)
        raise hou.OperationFailed(msg)

    # Grab the parm template groups for the material and object.
    material_template_group = material_node.parmTemplateGroup()
    template_group = objnode.parmTemplateGroup()

    # We need to loop over every entry in the material group and apply the
    # 'material_spare' tag with a value of '1'.
    for template in material_template_group.entriesWithoutFolders():
        # Grab any existing tags.
        tags = template.tags()
        # Apply the tag to signify it is a material spare property.
        tags["material_spare"] = "1"
        # Set the tags back to the template.
        template.setTags(tags)
        # Replace the original template with the modified one.
        material_template_group.replace(template.name(), template)

    # Find the folder to place the parameters into.
    material_folder = template_group.findFolder("Material")
    # For each entry in the template group, append it to the material 
    # folder.
    for template in material_template_group.entries():
        template_group.appendToFolder(material_folder, template)
    # Set the parm template group with the new parms to the object.
    objnode.setParmTemplateGroup(template_group)

Edited by graham
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...