|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" Opens a corresponding GH file (if existing) when opening a Rhino file. |
| 4 | + It is recommended to have this script in your Rhino startup command list: |
| 5 | + _-RunPythonScript AutoLoadGrasshopperDef.py |
| 6 | +
|
| 7 | + Caveat! This script can't do anything when Rhino is not already open |
| 8 | + and you double-click open a Rhino file. |
| 9 | + Also just works when opening .3dm files. |
| 10 | +""" |
| 11 | +import Rhino |
| 12 | +import scriptcontext |
| 13 | +import os.path, logging, sys |
| 14 | + |
| 15 | +logging.basicConfig(stream=sys.stderr, level=logging.INFO) |
| 16 | + |
| 17 | +""" |
| 18 | +https://developer.rhino3d.com/samples/rhinopython/current-model-info/ |
| 19 | + which was originally not working, since the script gets started in the |
| 20 | + document context of the previous document, which not longer exists, when |
| 21 | + the user is opening a new Rhino file |
| 22 | + this is also the reason why we don't use |
| 23 | + rs.Command("! _-Grasshopper _Document _Open {} _Enter".format(new_path)) |
| 24 | + in the delegate – it will simply not work |
| 25 | + |
| 26 | +https://discourse.mcneel.com/t/execute-script-on-file-opening |
| 27 | + in this thread @clement sheds some light on it and has some fantastic |
| 28 | + workarounds up his sleeve |
| 29 | + this Script wouldn't be possible without his help! |
| 30 | +""" |
| 31 | + |
| 32 | +def OpenDefinition(file_path): |
| 33 | + """ Another workaround because we can't use rs.Command() """ |
| 34 | + logging.debug("OpenDefinition: {}".format(file_path)) |
| 35 | + |
| 36 | + Grasshopper = Rhino.RhinoApp.GetPlugInObject("Grasshopper") |
| 37 | + if not Grasshopper: return False |
| 38 | + |
| 39 | + Grasshopper.OpenDocument(file_path) |
| 40 | + |
| 41 | +def AfterLoadEvent(sender, e): |
| 42 | + """ event handler / delegate """ |
| 43 | + path = e.FileName # can't use rs.DocumentPath() |
| 44 | + if path[-3:] == "3dm": |
| 45 | + new_path = path[:-4] + ".gh" # switch ".3dm" with ".gh" |
| 46 | + # Windows and its backslash paths:... |
| 47 | + new_path = new_path.split("\\") |
| 48 | + new_path = "\\".join(new_path) |
| 49 | + # not using |
| 50 | + # os.path.join(*new_path) ### splat operator * for list unpacking |
| 51 | + # because it's faulty with absolute Win paths (drive letters, duh!) |
| 52 | + exists = os.path.isfile(new_path) |
| 53 | + if exists: |
| 54 | + OpenDefinition(new_path) |
| 55 | + else: |
| 56 | + print "No corresponding GH file found." |
| 57 | + |
| 58 | +def AutoLoadGrasshopperDef(): |
| 59 | + """ Subscribe to the EndOpenDocument event so we know when it's okay |
| 60 | + to fire the Grasshopper definition load |
| 61 | + """ |
| 62 | + key_after_load = "AfterLoadEvent" |
| 63 | + if scriptcontext.sticky.has_key(key_after_load): |
| 64 | + logging.debug("GH autoload deactivated") |
| 65 | + Rhino.RhinoDoc.EndOpenDocument -= scriptcontext.sticky[key_after_load] |
| 66 | + scriptcontext.sticky.Remove(key_after_load) |
| 67 | + else: |
| 68 | + logging.debug("GH autoload activated") |
| 69 | + scriptcontext.sticky[key_after_load] = eval(key_after_load) |
| 70 | + Rhino.RhinoDoc.EndOpenDocument += eval(key_after_load) |
| 71 | + |
| 72 | +######################## |
| 73 | +if __name__=="__main__": |
| 74 | + AutoLoadGrasshopperDef() |
0 commit comments