mcneel / ghpython

A Grasshopper component for Rhino.Python
http://www.food4rhino.com/project/ghpython
118 stars 34 forks source link

doc switching, a way to crash things #9

Closed bengolder closed 13 years ago

bengolder commented 13 years ago

I think I broke something. If I open rhino 5 and grasshopper (after a fresh reinstall of both and a reboot), get out a new ghpython component and try to switch the target doc, Rhino crashes.

This started happening after I did the following:

I created a module (I'll call it module1) to be imported by the ghpython script. In this module I had a line like this:

# inside of module1
from scriptcontext import doc

I assumed that when I imported the module, it would use the same target doc as set by the ghpython component, but it does not. Instead each imported module targets only the gh doc, not the rhino doc.

So then I tried something that seems to have broken the ghpython component. In the imported module, I removed the line containing from scriptcontext import doc. But within the module, I had functions that still made use of a module-level global variable called doc that was not assigned within that module. So I tried to pass the doc object from the ghpython script into the module like so (as seen in the ghpython script):

# in the ghpython script
from scriptcontext import doc
import module1
module1.doc = doc # I think this was a bad idea, but I was curious
from module1 import myfunction # myfunction makes use of doc
myfunction()

The script ran once, correctly using the rhino doc as the target doc for both myfunction as well as in the ghpython script. Then Rhino crashed, and I haven't been able to set the target doc without crashing Rhino since.

I'm curious how and if I really broke something that could not be fixed with a reinstall and a reboot.

Thanks.

sbaer commented 13 years ago
from scriptcontext import doc

Is not a good thing to do in a separate module. Modules in python are loaded and executed once. This means that the doc variable will point to the correct doc the first time the module is run, but may be pointing to an incorrect doc when the module is run in the future. I would recommend always calling scriptcontext.doc instead.

bengolder commented 13 years ago

Thanks Steve.

also, I probably could have avoided this by calling RhinoDoc.ActiveDoc instead of scriptcontext.doc in my separate modules.

sbaer commented 13 years ago

I would avoid RhinoDoc.ActiveDoc if possible and always use scriptcontext.doc. Mac Rhino is a multi-doc application and the ActiveDoc can switch.

bengolder commented 13 years ago

Thanks Steve! will keep that in mind.

piac commented 13 years ago

Variable assignment (document switching) is something that should not be sticky between sessions in any way. Also, I just tried the steps described above now and nothing happens, so a first guess would be that the crash depends later from additional side effects in the script... it might be in different places.

Did you manage to isolate the provoking part in the script? If not, can you check if, after reducing the script to a minimum set of operations and restarting Rhino, the crash disappears?

bengolder commented 13 years ago

before I could find the steps to recreate it, I tried another reinstall of grasshopper in order to resolve it. That worked. I can switch target docs again.

Now I'll see if I can break it again.