Ghpython importing self-created modules inconsistent

grasshopper

(Lando Schumpich) #1

Hello everyone,

i’m stuck hard on a problem with importing self-created modules in ghpython. When i run the exact same code in ‘Editpythonscript’ in Rhino and ‘ghpython’ in grasshopper i get different results. could this be happening because of some tolerance settings?

I observed this on another python module i created, too. As long as i keep my code either totally in grasshopper or rhino everything works fine, but for this project i wanted to do some simple physics simulations so i would really like to use import on some number of ghpython nodes instead iof copy-pasting and being screwed if i change something in the original module.

Some help or insight would be greatly appreciated.

EDIT:

i broke it down to a way simpler script which still fails:


(qythium) #2

Looks like it might be due to the way division works in Python 2.x rather than anything import related, you have 1/2 in your code which evaluates to 0 because of integer casting when both operands are integers.

You can write

from __future__ import division

at the top of your script to use the newer “true division” operator and prevent these sort of mistakes.
(I’m guessing EditPythonScript already sets this up for you by default, but not GhPython)


(Lando Schumpich) #3

wow thank you so much, works like a charm!

On another note: whenever i save changes to the module via ‘editpythonscript’ or an editor, because we no longer have ‘unloadgrasshopperplugin’ in rhino 6 i have to close and restart rhino to see my changes updated in ghpython.
You happen to know a more convenient way?
If there is none who cares it’s just a minor inconvience and not something nail-biting, script-breaking like not adding future division to your module :wink:


(qythium) #4

The python interpreter “caches” its modules so running the script again ignores the same import statement- You can use reload(module) right after the import, but this only does top level reloading so it won’t reload any sub-modules imported in the main one, you’ll have to also explicitly call reload() inside the module code for that to happen.

Yeah, division in python 2.x is one of those things that trips lots of people up for the first time… even on the new behaviour I still write e.g. 1/2.0 to be safe. (The same thing also applies to other languages)