Jump to content

Using python's numpy/scipy in SOPs


Jason

Recommended Posts

Hi all,

Ever since I read about the functionality in scipy, I've wanted to see how it could enhance volumes in Houdini. So I suddenly realized our Systems people here at work installed it sometime or other and it was available within HOM! Woo!

Anyhow, please see how easy this is... and useful.

Here is the code of the SOP too:

import numpy
import scipy
import scipy.ndimage


# This code is called when instances of this SOP cook.
geo = hou.pwd().geometry()

# Add code to modify the contents of geo.
prim_number = hou.pwd().parm("prim_number").eval()
volume = geo.prims()[prim_number]
np = numpy.array(volume.allVoxels()).reshape(volume.resolution())
# Now we have our numpy 3d array... have fun!

if hou.pwd().parm("do_gaussian").eval():
  sigma= hou.pwd().parm("gaussian_sigma").eval()
  np = scipy.ndimage.filters.gaussian_filter( np, sigma  )

if hou.pwd().parm("do_uniform").eval():
  uniform_size = hou.pwd().parm("uniform_size").eval()
  np = scipy.ndimage.filters.uniform_filter( np, uniform_size  )

if hou.pwd().parm("do_sobel").eval():
  np = scipy.ndimage.filters.sobel( np  )

if hou.pwd().parm("do_prewitt").eval():
  np = scipy.ndimage.filters.prewitt( np  )

if hou.pwd().parm("do_abs").eval():
  np = abs(np)

# set the values of the volume back in Houdini
volume.setAllVoxels( np.flatten() )

I hope someone finds this useful in some way. In fact I was surprised at the speed of this thing: a 100^3 volume gets sent to scipy and filtered and returned to Houdini in about 1 second!

Take care,

Jason

post-4-130628178019_thumb.jpg

scipy_volumefilter_v1.otl

  • Like 2
Link to comment
Share on other sites

i thought ( or even read , esp about numpy ) that this wasn't possible , but hey .. glad to see it is 'working' .

thank you Jason ! looks fun and fast too .. =)

.cheers

Edited by zarti
Link to comment
Share on other sites

There's an example of using numpy on the HOM doc page about writing a COP operator.

http://www.sidefx.com/docs/houdini11.0/hom/pythoncop

Ah, that would have helped me figure out the ...AsString() method, had I known about this beforehand! ;)

Now wouldn't it be fun to write a Python VOPnet context in which we could process volumes, rasters, etc? Or does that exist somewhere already, Mr.Thompson? :)

Link to comment
Share on other sites

Attached is a HOM_PointTree class I created that wraps around the KDTree implementation. It's designed to be kind of similar to GEO_PointTree in that it takes a hou.Geometry input and builds a tree you can then query using a hou.Vector3 object to get the nearest N points, or points within some distance. It returns tuples of hou.Points as the result.

treeutils.zip

Link to comment
Share on other sites

  • 3 months later...
  • 4 weeks later...
  • 1 year later...

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