Jump to content

Python set relative references shelftool


asnowcappedromance

Recommended Posts

hi guys,

I wrote a simple python script that allows copying/pasting relative references from one object to another.

Right now you can only use the tool in the active viewport, in the next step I want to make it work in the network editor.

As I'm a beginner with phython I'm curious what your suggestions to improve the tool would be. (or what you'd do differently)

here's the code:

import toolutils

viewer = toolutils.sceneViewer()
# viewer = toolutils.activePane(kwargs)

#----------- get the source object --------------

selected_object = viewer.selectObjects(prompt='select source object', quick_select=True, allow_multisel=False)
if len(selected_object) is 0:
    raise hou.OperationFailed("No source object selected")
# selectObjects() outputs a tuple of nodes that's why one has to specify the component: selected_object[0]
source = selected_object[0]
source.setSelected(False)

#----------- get the source object --------------

selected_object2 = viewer.selectObjects(prompt='select target object', quick_select=True, allow_multisel=False)
if len(selected_object2) is 0:
    hou.ui.displayMessage("No target object selected")
target = selected_object2[0]
target.setSelected(False)

#------------ test if target contains the same parameters ------------------

for i in source.parms():
    sourceP = i.name()
    for p in target.parms():
        targetP = p.name()
        if targetP in sourceP:
            active_parm = source.parm(sourceP)
            target.parm(targetP).set(active_parm)

It'd be nice to get some tips,

cheers,

Manuel

P.s. is there a way to keep the indentations while quoting ??

Edited by asnowcappedromance
Link to comment
Share on other sites

..

P.s. is there a way to keep the indentations while quoting ??

try with <> button in the text editor ; ( it is for code )

for i in source.parms():
    sourceP = i.name()

Edited by zarti
Link to comment
Share on other sites

The one thing that really stands out is that you are using nested loops and string comparisons which isn't ideal, especially with lots of parameters on the nodes. The best method would be to loop over one node's parameters and attempt to get the parameter of the same name on the other node. If that parameter exists a hou.Parm object is returned; if not, None. You can test for it not being None and then set accordingly.

See linkParmsTo() in this module: http://houdinitoolbox.com/houdini.php?asset=15

Link to comment
Share on other sites

The one thing that really stands out is that you are using nested loops and string comparisons which isn't ideal, especially with lots of parameters on the nodes. The best method would be to loop over one node's parameters and attempt to get the parameter of the same name on the other node. If that parameter exists a hou.Parm object is returned; if not, None. You can test for it not being None and then set accordingly.

See linkParmsTo() in this module: http://houdinitoolbox.com/houdini.php?asset=15

Hi Graham,

thanks for your reply! I see what you're saying, makes sense to me!

I had a look at your linkParmsTo() function. Can you please explain why you're skipping FolderSetParmTemplates?

Great website by the way!

regards,

Manuel

Link to comment
Share on other sites

I just skip folder parameters because there really isn't a point to linking folder switchers. Also, that code is a little old. FolderSetParmTuples aren't really used anymore as I recall but might show up so to be safe I leave them in there. The slightly newer code, and a slight name change is as follows. A newer version of parmutils is almost ready for release with a number of newer functions.

def linkParms(source_node, target_node):
    """ Link the parms of the source node to those of the target node if 
        a parameter with the same name exists.
    """
    # We want to skip folder parameters.
    types_to_skip = (hou.FolderParmTemplate, hou.FolderSetParmTemplate)

    for parm_to_link in source_node.parms():
        if isinstance(parm_to_link.parmTemplate(), types_to_skip):
            continue

        # Try to get this parameter on the other node.
        parm_to_link_to = target_node.parm(parm_to_link.name())
        # If the target parm exists then link it.
        if parm_to_link_to is not None:
            parm_to_link.set(parm_to_link_to)

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