asnowcappedromance Posted June 28, 2011 Share Posted June 28, 2011 (edited) 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 June 28, 2011 by asnowcappedromance Quote Link to comment Share on other sites More sharing options...
zarti Posted June 28, 2011 Share Posted June 28, 2011 (edited) .. 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 June 28, 2011 by zarti Quote Link to comment Share on other sites More sharing options...
graham Posted June 29, 2011 Share Posted June 29, 2011 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 Quote Link to comment Share on other sites More sharing options...
asnowcappedromance Posted June 30, 2011 Author Share Posted June 30, 2011 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 Quote Link to comment Share on other sites More sharing options...
graham Posted June 30, 2011 Share Posted June 30, 2011 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) Quote Link to comment Share on other sites More sharing options...
asnowcappedromance Posted July 2, 2011 Author Share Posted July 2, 2011 ok, now I get it. After watching Luke Moores latest masterclass where he explains parmtemplates I know what you're doing. Thanks for making it clear !!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.