Grasshopper

algorithmic modeling for Rhino

Hi,

I recently succeeded in customizing a toolbar button to have it import a drawing to the canvas, open Grasshopper, and finally - with some Python scripting - open a specific GH file in Grasshopper. Refering to an absolute path is easy, but I would like to have some more flexibility because I would like to have a copy of the GH file in the folder where my Rhino model is stored.

Is there a way to retrieve the full path to the current folder (that is, where the current 3dm file is located)?

These are the commands and scripts I use to refer to files (absolute paths):

_import "C:/Users/LetoLab/RhinoTest/drawing.3dm"
-_RunPythonScript (
import rhinoscriptsyntax as rs
import Rhino
gh = Rhino.RhinoApp.GetPlugInObject("Grasshopper")
gh.ShowEditor()
filename = "C:/Users/RhinoTest/manipulator.gh"
gh.OpenDocument(filename)
)

I found out that Rhino.RhinoDoc.Path does not properly return the path of the current 3dm file like it is supposed to do..

Any suggestions?

Thanks!

Views: 2597

Replies to This Discussion

Thanks djordje, I did try that one but I wasn't able to strip it down to return only the file's path. Certainly this method seems to be the way to go - might even be the only way..

But that's exactly what LastFileSuccessfullyRead() function returns - the imported/opened file path.

What do you get as a result?

Well, no.. I might have been unclear about this.

The method reads the sentence which goes as follows:

File "C:\Models\model_01.3dm" succesfully read

..and then subtracts the first and last part of it so that not only the path but also the file is returned. What I would like to get is only

C:\Models (so that I can form a new string to refer to a known gh-file in that same folder.)

regardless of the file name. I only worked with Java so I'm not sure how to change the code so that it can delete the part of the string after the last occurrence of "/". That might do the trick..

Aha, I get it.
Try this:

# modified version of retrieve folderPath - fileName, by Clement Greiner

import Rhino

def LastFileSuccessfullyRead():

    # get command history text
    ch_text = Rhino.RhinoApp.CommandHistoryWindowText
    if not ch_text: return None

    # no file yet
    file = None

    # command history text as line list
    lines = [line for line in (ch_text.split('\n'))]

    # reverse lines in text to search backwards
    lines.reverse()

    # find "File ..... successfully read" string
    for line in lines:
        a = line.find("File")
        b = line.find("successfully read")
        if a > -1 and b > 5:
            file = line[a+6:b-2]
            folderFileName= file.split("\\")
            folderPath = file.split(folderFileName[-1])
            break

    return folderPath[0]


file = LastFileSuccessfullyRead()
if file:
    print  file
else:
    print "Error getting last imported file"

Fantastic! That's exactly what I need.. I'm starting to like Python.

Thank you very much!

just a note: It is important to start the button macro with

_Noecho

If you don't, he python code is added to the CommandHistory each time it is executed, which is exactly where you are trying to search for the path. The script would 'bite its own tail'. Took me a while to find out (:

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service