Grasshopper

algorithmic modeling for Rhino

# Using third party module in RhinoPython?

Is it possible? If so, how?

Views: 2338

### Replies to This Discussion

It is indeed.
You simply need to add a reference to the module directory and use the import statement (like with native modules). You can do this inside the script itself, or, using the standard Python script editor:

1) In Rhino type in the command "EditPythonScript".

2) In this Python editor go Tools -> Options -> Files.

3) Here you will see an overview of the directories which are currently referenced. Now either:

5) Move your module into one the existing directories.

You may need to restart Rhino after setting up the directories. After that you simply import the module and run the script to see if it worked. The name to import is usually the name of the top-level directory of the module folder structure, or, if it is just a single .py file, the name of this file.

Hope that helps,

/Anders

Ps. Not all modules are supported by IronPython by the way.

yeah Anders, that's possible indeed.

just managed to import and use numpy in rhinopython, following your instruction. and i also referred to this link in python.rhino3d.com

I mainly develop in Python straight in the GHPython editor, so I'm afraid I can't really help. That said I'm thinking it should be fairly straightforward to import a .dll assembly as long as its path is read by which ever environment/the script you're developing. Perhaps David or Giulio could suggest some approaches?

For completeness, I am adding the answer in case someone looks at this thread (2 years later). For GhPython.

1. You want to add a reference to an assembly (.dll):

You can you the clr module.

import clr clr.AddReferenceToFileAndPath(r'path\to\dll.dll')

There are a few other overloads. I suggest to stick to this one. You can see a couple of ways to obtain paths here. At best, save the .dll file aside the .gh definition (user-level), or in a system-level directory (machine installation).

2. You want to add an import to a module (.py):

You can use the sys module.

import sys my_mod_path = r'path\to\mymodule.py' if not my_mod_path in sys.path: sys.path.append(my_mod_path)

These two methods are reliable, consistent and will make working with modules Pythonic and fun!

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

from __future__ import thank_you

by June Lee

by June Lee

by June Lee

by June Lee

• View All