View RSS Feed

Steve Baer

Numpy and Scipy in RhinoPython

Rate this Entry
Disclaimer: I have very little experience with numpy and scipy so you are going to do better by searching google for support with these libraries. This blog is about getting these libraries to run in Rhino.
Yes it is possible now!!!
Okay, here's what I did to get numpy/scipy running in Rhino

1 - Install Rhino 5 (32bit version)
numpy/scipy uses C++ DLLs which need to be compiled for a specific platform. Currently numpy/scipy for IronPython will only run in 32bit applications on Windows. I contacted the guys at Enthought and they do plan on releasing a 64bit version for Windows, but they want to make sure everything is working on 32bit first. They have some doubts about being able to release a mono version for you Mac guys out there.

2 - Install numpy/scipy for IronPython
Follow the instructions on this site http://www.enthought.com/repo/.iron/

3 - Modify settings in RhinoPython
Start Rhino 5 - 32bit version and run "EditPythonScript" to bring up the editor. Go to the options dialog by selecting Tools->Options from the menu

Add site-packages and DLLs to the search paths (see image). This helps RhinoPython find the numpy/scipy packages and associated DLLs.
Click image for larger version

Name:	numpy_paths.png
Views:	521
Size:	35.5 KB
ID:	252

Check the "Frames Enabled" option since numpy/scipy requires this to be turned on.
Click image for larger version

Name:	numpy_frames.png
Views:	418
Size:	26.0 KB
ID:	250
I decided to make the "Frames Enabled" an optional engine feature since it does have a performance impact on scripts. Numpy/scipy requires this feature to be turned on.

4 - Run a test
Python Code:
    # For now, we need to manually load mtrand before using numpy or scipy
    # I'm still trying to figure out why mtrand is not automatically getting
    # loaded when numpy/scipy imports it.  If I can fix this, we won't need
    # the following two lines
    import clr
    clr.AddReference("mtrand")
     
    import numpy
    import rhinoscriptsyntax as rs
     
    x_coord = [  0123456]
    y_coord = [0.0,0.1,0.5,2.5,2.5,2.5,4.0]
    xyz = zip(x_coord,y_coord,[0]*len(x_coord))
    rs.AddPoints(xyz)
     
    degree = 5
    eq = numpy.polyfit(x_coord, y_coord, degree)
    fitfunc = numpy.poly1d(eq)
     
    fit_points = []
    for i in range(61):
        x = i/10.0
        y = fitfunc(x)
        fit_points.append((x, y, 0))
    rs.AddPolyline(fit_points)

If you get a polyline in rhino fit through a series of point then you are all all set; if not go back to step 1 and repeat.

This looks like a good place to start if you want to learn more about numpy/scipy
http://docs.scipy.org/doc/
Tags: None Add / Edit Tags
Categories
Uncategorized

Comments

  1. BenjaminGolder -
    BenjaminGolder's Avatar
    fyi, I'm pretty sure that the default scipy package contains qhull

    scipy.spatial.qhull
  2. dveenendaal -
    dveenendaal's Avatar
    I'm so happy right now.
  3. dveenendaal -
    dveenendaal's Avatar
    Does the suggested installation of scipy not contain scipy.sparse? At least, I'm not able to access it by importing it.
    Updated 07-28-2011 at 04:02 PM by dveenendaal
  4. Joel.Putnam -
    Joel.Putnam's Avatar
    Has anyone managed to get this working on Rhino 64 yet?
  5. Steve Baer -
    Steve Baer's Avatar
    I don't believe that enthought has released a 64 bit build yet.
  6. evertamador -
    evertamador's Avatar
    Hi Steve,

    I've followed your instructions above but keep getting the message "Could not add reference to assembly mtrand",
    if I try to append the lib, dlls and site pachages manually I get more errors.

    I'm running numpy - scipy 1.6.1 and Rhino 5 Version 5.0 (5.1.2011.1025, 25/10/2011)
    Beta SN: 4-1501-0106-100-0008870-64111

    any ideas?

    Cheers
    Evert

    Python Code:
      # For now, we need to manually load mtrand before using numpy or scipy
      # I'm still trying to figure out why mtrand is not automatically getting
      # loaded when numpy/scipy imports it.  If I can fix this, we won't need
      # the following two lines
       
      import sys
      sys.path.append('c:\Python27')
      sys.path.append('c:\Python27\DLLs')
      sys.path.append('c:\Python27\Lib')
      sys.path.append('c:\Python27\Lib\site-packages')
       
      import numpy
      import clr
      clr.AddReference("mtrand")
       
      import rhinoscriptsyntax as rs
       
      x_coord = [  0123456]
      y_coord = [0.0,0.1,0.5,2.5,2.5,2.5,4.0]
      xyz = zip(x_coord,y_coord,[0]*len(x_coord))
      rs.AddPoints(xyz)
       
      degree = 5
      eq = numpy.polyfit(x_coord, y_coord, degree)
      fitfunc = numpy.poly1d(eq)
       
      fit_points = []
      for i in range(61):
          x = i/10.0
          y = fitfunc(x)
          fit_points.append((x, y, 0))
      rs.AddPolyline(fit_points)
  7. Steve Baer -
    Steve Baer's Avatar
    Hi Evert,
    Make sure you have the same options set up as I describe in step 3. You need to be using libraries in IronPython instead of Python. This may fix the problem you are seeing.
    Thanks,
    -Steve
  8. evertamador -
    evertamador's Avatar
    Hi Steve,

    Thanks for pointing out I was using the wrong libraries, doh!!! Now I'm stuck on step 3, where do I save the ironpkg-1.0.0.py so I can bootstrat it. When I type >ipy ironpkg-1.0.0.py --install, it says file . . . . .does not exists.

    Many thansk for your help on this, it is very much appreciated.

    Cheers
    Evert
  9. Steve Baer -
    Steve Baer's Avatar
    You need to install ironpython. In step 2 above, there are instructions you need to follow to get numpy installed from enthought
    http://www.enthought.com/repo/.iron/
  10. evertamador -
    evertamador's Avatar
    Hi Steve, Sorry to be a nuisance!

    I meant step 3 of the instructions on the enthought website. About the bootstraping!
    I'll give it another go!

    cheers
    Evert
    Updated 10-31-2011 at 08:13 PM by evertamador
  11. christophklemmt -
    christophklemmt's Avatar
    Hello
    I only have Rhino5 64bit installed on my office machine and would like to use scipy. Do you know if this works by now, or if there are any plans to make it work for 64 bit?
    Thank you!
    Chris