Jump to content
Jason

Using python's numpy/scipy in SOPs

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

Share this post


Link to post
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

Share this post


Link to post
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? :)

Share this post


Link to post
Share on other sites

There are also some nice scipy.spatial.KDTree and Delauney routines available; for those who can't wait until SESI create HOM functions for UT_KDTree and such.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Sweet!

Are there R-Trees as well?

Share this post


Link to post
Share on other sites

i thought ( or even read , esp about numpy ) that this wasn't possible , ..

.. which results to be pretty easy in Ubuntu .

.cheers

Share this post


Link to post
Share on other sites

numpy/scipy are external python libraries not included in houdini

Share this post


Link to post
Share on other sites

I don't think it was ever shipped with Houdini. Has your system python configuration changed since you last tried (assuming you are on linux) ?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×