MahmoudAbdelRahman / GH_CPython

CPython plugin for Rhino-Grasshopper
BSD 2-Clause "Simplified" License
175 stars 38 forks source link

Accessing GH file location in GH_CPython? #14

Closed dantaeyoung closed 6 years ago

dantaeyoung commented 7 years ago

Hi @MahmoudAbdelRahman , thanks for such a wonderful project - really excited to have 'real' Python in Grasshopper.

Is there a good way to access the GH file location from within GH_CPython?

I'd like to write some Python in the same directory as the .gh file and import those files. However, os.path.dirname(os.path.realpath(__file__)) is returning C:\Gh_CPython.

Thanks!

MahmoudAbdelRahman commented 7 years ago

Hi @dantaeyoung , Thanks so much for the feedback, I'm glad that you like it.

C:\GH_CPython is the default path where temporary python files are saved. Should I add a save as and open options to GH_CPython to specify custom path or do you have other recommendation? I think it is time to start working on a Grasshopper and Rhino modules for GH_CPython to make the best of their features.

Thanks so much. Regards, Mahmoud

dantaeyoung commented 7 years ago

Ah hmm. My use case isn't saving temporary python files, but creating custom modules.

For example, if I may want to create a small helper module called helpers.py in the same directory as my .ghx file, so I can import and call from my GH_CPython module, keeping my code neat. It would be convenient to have the directory path exposed from GH_CPython so I can do add it to the sys.path -- this way, the .ghx and helpers.py file can move together as a bundle and users don't have to worry about keeping track of paths.

I've found a workaround, which is get the current directory from GHPython, and then pass it to GH_CPython! I can live with this for now.

Like you said, I think having GH/Rhino modules exposed would probably solve this problem - really looking forward to seeing GH_CPython grow!

MahmoudAbdelRahman commented 7 years ago

Thanks so much @dantaeyoung for this useful feedback.

I'll start working on this problem now, and give you the results as soon as possible.

Regards, Mahmoud Abdelrahman.

MahmoudAbdelRahman commented 7 years ago

Hi @dantaeyoung , I have added these two features, Grasshopper.doc.DisplayName Grasshopper.doc.FilePath

Available Here

for example:

import Grasshopper as gh

a = gh.doc.FilePath
b = gh.doc.DisplayName

image

MahmoudAbdelRahman commented 7 years ago

I noticed that the previous solution wasn't good so, I have modified the code as follows: Use ghenv.fileName to get file name, and ghenv.filePath to get file path if saved.

image

dantaeyoung commented 6 years ago

Wow, thank you! this works wonderfully.

MahmoudAbdelRahman commented 6 years ago

Thanks so much!.