Difference between revisions of "HoudiniPython"

From cgwiki
Line 1: Line 1:
=== Make a general python thing ala maya pymel thing ===
+
=== Make a general python input window ===
The python console in houdini is a live command line, which if you're used to maya's script editor, feels a bit limiting. What you want is a sketchpad to write stuff, maybe define a function, all that. This is my best attempt at that. Fiddly, but it works.
 
  
In a nutshell, create a OTL with a button to call an internal python script.
 
  
==== Make a digital asset ====
+
The python console in Houdini is a live command line, which is great for some things, but doesn't allow you to enter multi-line code easily. Maya's script editor allows this, I wanted a similar thing in Houdini.
# Create a null
 
# Immediately subnet it
 
# R.click, create digital asset
 
# save n stuff
 
  
==== Make a button to call a script ====
+
The python SOP looks like its the answer, but don't be fooled. Its the python equivalent of a point wrangle, its designed to process geometry, not do general node or UI fiddling (which is really what you'd want python for).
# select the node, cog menu, 'edit parameter interface'
 
# on the right hand panel, shift click on the root node to collapse it
 
# regular click it again to expand it, but now the subfolders are collapsed
 
# click each one, make invisible
 
# create a button, label 'doit'
 
# set the callback script to python mode, and the command to 'kwargs['node'].hdaModule().click()'
 
# apply/accept
 
# r.click node, 'save operator type'
 
# r.click node, 'match current definition' to ensure its all clean and correct.
 
  
 +
With the help of Luke Gravett, here's a way to make something akin to maya's python script editor. This is version 2 of such a thing, it's just a null with a text editor and a button to execute the code.  An earlier version of this used an OTL/HDA, which was more fiddly than necessary.
  
That odd command there calls the current node, and finds the python module inside, and calls the click function. Time to set that up.
+
# Make a null sop, name it 'my_python_code'
 
+
# parameter pane, gear menu, 'Edit Parameter Interface...':
==== Define a click function ====
+
## add a string parameter, label 'Code', enable 'multi-line string', language 'Python'
# r.click the node, 'type properties'
+
## add a button, label 'Run', python callback is
# go to the scripts tab
+
  exec(kwargs['node'].parm('parm').eval())
# make a 'PythonModule' section
+
# Hit Accept
# add a basic click function, eg: 
 
 
 
 
 
<source lang="python">
 
def click():
 
  print 'hello world'
 
</source>
 
 
 
 
 
Once you hit apply/accept, you should now be able to click the button, and see output to the python prompt. You can now code away, after each change hit 'accept', and it'll do your bidding.
 
  
Other less fiddly ways to achieve this are via the python editor for shelf buttons, but this seemed good practice to make a button click interface hda thing.
+
Now you can type in code, click the button, make magic.
 
 
=== As above, but not as HDA ===
 
 
 
The supremely fast/lazy version:
 
 
 
# Make a null sop
 
# edit parameters:
 
## add a string, make it multi, language python
 
## add a button, python callback is
 
  exec(kwargs['node'].parm('parm').eval())
 
  
 
=== Get point attributes from a node ===
 
=== Get point attributes from a node ===

Revision as of 16:49, 10 December 2015

Make a general python input window

The python console in Houdini is a live command line, which is great for some things, but doesn't allow you to enter multi-line code easily. Maya's script editor allows this, I wanted a similar thing in Houdini.

The python SOP looks like its the answer, but don't be fooled. Its the python equivalent of a point wrangle, its designed to process geometry, not do general node or UI fiddling (which is really what you'd want python for).

With the help of Luke Gravett, here's a way to make something akin to maya's python script editor. This is version 2 of such a thing, it's just a null with a text editor and a button to execute the code. An earlier version of this used an OTL/HDA, which was more fiddly than necessary.

  1. Make a null sop, name it 'my_python_code'
  2. parameter pane, gear menu, 'Edit Parameter Interface...':
    1. add a string parameter, label 'Code', enable 'multi-line string', language 'Python'
    2. add a button, label 'Run', python callback is
 exec(kwargs['node'].parm('parm').eval())
  1. Hit Accept

Now you can type in code, click the button, make magic.

Get point attributes from a node

From the node get its geometry, then its point attributes, then the short names of those attributes.

[ x.name() for x in hou.node('/obj/mygeo/mysop').geometry().pointAttribs() ]


To go one further and make a nice list to feed to an attribute delete node, use a join() with a single space, prepend with ^'s, and stick an asterisk on the front:

print '*',' '.join([ '^'+x.name() for x in hou.node('/obj/geo1/mysop').geometry().pointAttribs()])


will return

* ^id ^Cd ^Alpha ^center ^orient ^P ^uniqueId ^materialId

Drag most things into the python window

Do this, and what you drag will be converted into the python text equivalent. This works for nodes, parameters, shelf buttons, most parts of the UI.

Write out mmb error text to file

Handy!

open('/tmp/error.txt','w').write(hou.node('/path/to/node').errors())