Jump to content

batch edit paths?


3__

Recommended Posts

Thanks everyone.

I think I should make this a generic shelf script that localizes maps.

So far I can harvest map paths using opextern, however it doesn't

return parameter names, only node names and paths.

What would be the best way to set the parameters given this information?

import os
ex = hou.hscript("opextern -gR /shop/")
ma = ex[0].split("\n")
for i in ma:
    if i != "":
        print ("    external file = " + i)
        ms = i.split(" ")
        anode = ms[0]
        apath = ms[1]
        afile = os.path.split(apath)[1]
        anewpath = ("$HIP/" + afile)
        print ("    node path = " + anode)
        print ("    node parm = ???")
        print ("    new path = " + anewpath + "\n")

Link to comment
Share on other sites

..

it doesn't

return parameter names, only node names and paths.

What would be the best way to set the parameters given this information?

..

yes, it only returns path to the node and evaluated value of that parameter

so until the similar function is available for python, you have to loop through all parameters of that node and if the parameter is string then compare its value to the one given by opextern and if it matches, append the parameter to your new list

then you will have list of parmeters containing external paths

be careful when you are trying to evaluate parameter's value it will expand all the local variables and expressions, so to get exactly what is typed in the field you need to use parm.unexpandedString() instead of just parm.evals()

so the python function will be something like this:

def getExternalParms():
    exPaths = hou.hscript("opextern -qR /")[0]
    exPaths = exPaths.split("\n")
    exPaths.pop()
    exParms = []
    for line in exPaths:
        pathPair = line.split("\t")
        node = hou.node(pathPair[0].strip())
        oldPath = pathPair[1]
        for parm in node.parms():
            if parm.parmTemplate().dataType() == hou.parmData.String:
                if parm.eval() == oldPath:
                    exParms.append(parm)
    return exParms

you can append following lines for debugging to print result to console

    for parm in exParms:
        print "%s -> %s" %(parm.path(), parm.unexpandedString())

Link to comment
Share on other sites

I actually rolled my own wrapper function around opextern using Python quite a while ago but couldn't find where I'd put it till now. It's a bit old, crude, and could probably use some improving, but it provides a way to get various specific bits of info out of opextern. You can get back lists of hou.Node objects, file paths, hou.Parm objects, filter for missing, get all file parms even ones that are empty, etc. You can then process these lists as you'd like.

I've also attached another little helper function that takes a list of parms, and another function + args. This lets you right a simple function to process a hou.Parm object and then call it on all the objects in a list generated from getExternalReferences.

As I said it's a bit old so I can't guarantee it won't have any bugs in it so if you find any please let me know.

Edit: Removed some redundant code.

def applyFunctionToParms(parms, function, args=()):
    """ Apply a function with specific args to each parameter in the list.

        The hou.Parm object is inserted into the beginning of the args list.
    """
    for parm in parms:
        function.__call__((parm,) + args)

def getExternalReferences(node_or_nodes, group=False, missing=False, only_nodes=False,
                          only_files=False, recurse=True, all_file_parms=False):
    """ Shows all/missing external references in a node.

        This function is a wrapper around opextern with a bit more control.

        node_or_nodes: A single instance, or a list of hou.Node objects.

        group: Group frame ranges together; files like butterfly$F.pic will be shown
               as 'bufferfly$F.pic [1-7]'.

        missing: Check each file to see if it exists, and only return ones with missing files.

        only_nodes: Return only the nodes with have external references (or have missing external
                    references, if missing=True.

        recurse: Recurse into networks and sub networks.

        only_files: Return only the external files.  If missing=True, this will only return
                    the missing files.


        all_file_parms: Return all file parameters of a node that is found to have an external
                        reference.  This includes all blank file parms.

    """

    # Get the function options.
    args = [""]

    if group:
        args.append("g")
    if missing:
        args.append("M")
    if only_nodes:
        args.append("n")
    if only_files:
        args.append("r")
    if recurse:
        args.append("R")

    # If just a single node was specified, convert it to a list.
    if isinstance(node_or_nodes, hou.Node):
        node_or_nodes = (node_or_nodes,)

    # Get paths to all the nodes.
    node_paths = map(hou.Node.path, node_or_nodes)

    # Call opextern with our args and paths to get the result.
    output = hou.hscript("opextern %s %s" % (" -".join(args), " ".join(node_paths)))[0]

    # If only showing missing, check to see if there are any or not.
    if missing:
        # Return an empty tuple if there are no missing references.
        if "No missing external" in output:
            return ()

    # Lists of parms to return.
    parms = []

    # Split the output string into lines, discarding the last.
    output = output.split("\n")[:-1]
    # For each string entry.

    # Return the file list.
    if only_files:
        return tuple(output)
    # Return a list of hou.Node objects from the node names.
    elif only_nodes:
        return tuple(map(hou.node, output))

    # Get the hou.Node objects.
    nodes = map(hou.node, [ref.split()[0] for ref in output])

    for node in nodes:
        for parm in node.parms():
            # If the parameter is a String parameter.
            if isinstance(parm.parmTemplate(), hou.StringParmTemplate):
                # If the String parameter is a file reference.
                if parm.parmTemplate().stringType() == hou.stringParmType.FileReference:
                    # If we want all the file parms, append it to the list.
                    if all_file_parms:
                        parms.append(parm)
                    # We only want used file parms.
                    else:
                        val = parm.eval()
                        # If the the parameter isn't empty.
                        if val != "":
                            # We are looking for missing so test if it
                            # exists.
                            if missing:
                                # It doesn't exist so add it.
                                if not os.path.exists(val):
                                    parms.append(parm)
                            # We want our used file parameter.
                            else:
                                parms.append(parm)
    # Return the list of parameters.
    return tuple(parms)

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...