Custom 'Save_As' - would like to add my Grasshopper to script

Hi Guys,

For version control of my work flow I’ve written the attached python script.

The code is pretty simple - it adds today’s date in yymmdd_ format to the name of the file and saves it to my desktop.

What I would like to do/have happen is if a Grasshopper script is also open along with the Rhino file that the script saves it in the same format at the same time.

Any heads-up on the best place to look for methods of seeing if a GH is open and how to call a save_as action?

Cheers

DK180401_save with date format to desktop.py (553 Bytes)

Hi Guys,

Any hints for me as to what the missing argument is on this rather basic piece of code:

Image%2020190819001

Cheers

DK

Hey

First off: help(Grasshopper.Plugin.GH_RhinoScriptInterface.SaveDocumentAs) tells you what inputs the method expects. In this case:
SaveDocumentAs(self: GH_RhinoScriptInterface, filename: str) -> bool

However, even on the official documentation the first parameter isn’t documented, but looking at the example gives a hint. This works for me:

import rhinoscriptsyntax as rs
import Grasshopper
import Rhino

path = "C:\\Users\\robin\\Documents\\test.gh"
gh = Rhino.RhinoApp.GetPlugInObject("Grasshopper")
a = Grasshopper.Plugin.GH_RhinoScriptInterface.SaveDocumentAs(gh, path)

~robin

Robin, thank you so much, I would not have ever guessed that was the solution. Have got my test code working, now to add it into my save-as script.

Cheers

DK

SORTED! Very happy to have both my Rhino and GH documents saved together with the new file name/date with one click.

import rhinoscriptsyntax as rs
import System
import time
import Grasshopper
import Rhino

GH = Rhino.RhinoApp.GetPlugInObject("Grasshopper")

dateandtimenow = time.strftime("%Y%m%d")
save_date = dateandtimenow[2:]

desktop_path = "C:\\Users\\David Kay\\Desktop\\"

current_name = rs.DocumentName()

if ('_') in current_name:
	current_name = current_name.split('_',1)[1]

rhino_save_file_name = chr(34) + desktop_path + save_date + '_' + current_name + chr(34)

cmd = '-_SaveAs ' + rhino_save_file_name 

rs.Command(cmd, True)

if GH.IsEditorLoaded():
	
	gh_save_file_name = desktop_path + save_date + '_' + current_name.split('.')[0] + '.gh'
	
	Grasshopper.Plugin.GH_RhinoScriptInterface.SaveDocumentAs(GH, gh_save_file_name)
1 Like