Initial Help and pointers needed. Scripting for grasshopper & rhino on Mac

Setup & Context: Macbook M1; Rhino7; Python; Grasshopper

Hey Experts of Rhinoceros!
Im coming to you in a dark hour.
At the moment im trying to write a python script in rhino that will execute & bake a GH model,
run some validations and print the output. (This should happen in a loop many many times with different parameter inputs for the GH file)

The key challenge for me is finding information about syntax and fighting the Rhino Script Editor on Mac.

I have a Software development background and the editor seems like … lets say it does not seem suited for productive work. I feel like my first day of programming again. I cannot find any useful information or documentation or guides.
Sources like https://developer.rhino3d.com/ offer only very little very specific examples, and the missing autocompletion in the editor (I know there is some in like 1% of cases and I call that missing then) make it feel like its impossible to get more info.

Ive also tried to use an external editor but failed to get that setup, again due to me not being able to find the information I need. I tried to install the rhinoscriptsyntax package, which does not exist it seems not on GitHub not anywhere I found. I looked for “rhino-python” which runs only under 2.7 and is outdated and does not seem to work well either. So that path also feels like a dead end without help.

So I guess my main question is:
What am I missing is there a better source of information? - how do I get the rhino packages to run under macOS so that I can enjoy actual autocompletion? And where do I find meaningful guides on simple things like how to select, delete, export etc. objects that goes beyond the sources above?
(There I did find a little documentation to stuff like rs.DeleteObject, which sounds like it would delete smth, but seems to be a prompt for the user to click on the object to delete, which is not what I need, but the doc ends there and what call I need to actually delete things I dont know :frowning: )

Sorry for the long message, I realise its a bit of a vent, but its taken me hours just to get to bake and export an object which cannot be the way to go :confused:

someone maybe a pointer to a better source or can share some knowledge? - so far it really does not feel like python is meant for productive work but more an afterthought gimmick.

OK, that seems feasible. Could you explain a little more what you actually want to do?

That’s not a package to install with something like pip or download from the internet. It comes with your Rhino installation.

Generally, Rhino runs IronPython 2.7, not CPython. Let’s leave it at that!
It has nothing to do with your local Python installation in /usr/local/, usr/bin or wherever.

Not really! developer.rhino3d.com is the main source of information, but folks here in the forum tend to be very helpful.

You have to study the relevant documentations, rhinoscriptsyntax or RhinoCommon. Btw, rhinoscriptsyntax just wraps RhinoCommon.

Yes, you can see the underlying code by doing this:

import rhinoscriptsyntax as rs
import inspect

print inspect.getsource(rs.DeleteObject)

If you find the relevant deletion code, you can come up with your own function without the user prompt.

That’s a bit harsh.

1 Like

Hey Diff-Arch!
Thanks of all first for replying and taking care to address my different questions, and please forgive the obvious frustration that goes along with my post. Ive spend hours on trying to understand how exactly this works but it was really difficult for me despite or maybe because im coming from a background of regular python and mobile development. So now to your points and my follow up questions.

Im trying to validate if different combinations of input parameters for the GH model bake into a valid STL/Mesh, meaning they fulfil certain criteria like watertightness, no manifold edges or vertices, being a single object etc. - I would like to do that with an external library because its easy and nice to work with.
I understand from your post that I should stay inside of the script editor that rhino offers me? - Im on a Mac and the editor is really limited. I cannot meaningfully debug, it does not display logs properly and the autocomplete function is barely existent. Can you advise on how to script for Rhino on a Mac outside of Rhino? - or is this not meant to be? - Because I cannot import the relevant libraries outside it seems. Or can I somehow use the IronPython interpreter on my regular system…? im still confused around that part, cause im used to have a venv on my system and install packages there that I need.

That is good to understand thanks!

Okay, then I will try to make progress there, it to me seems not super straight forward a lot of times, but I will see - if this is the place to look I will look harder! :slight_smile:

Again, that is very helpful as well! - will def use that in the future.

Is there somewhere an example that is a bit like what I described above? - that you can point to?

Thanks a lot & Best,
Lukas

OK, I would do the validity checks before baking, since that seems to be more straightforward when working in Grasshopper. You can then for instance only bake valid meshes to Rhino.

Let’s say you connect a list of meshes into an input of a GHPython component and set its type hint to “Mesh”. This means that all the meshes - funnelled in through the same input -, now get interpreted as Rhino.Geometry.Mesh objects, and you can work with them as such in your Python script.
The tests you indent to do are really easy to do with the API, since the Rhino.Geometry.Mesh class already seems to have all the desired checks implemented. Look for IsSolid(), IsValid(), etc.

Doing it with an external library might be challenging, since Rhino uses IronPython 2.7. If you’re dealing with a CPython 3+ library you need to get creative to make it work with Rhino.

I’m also a macOS user. I tend to use PyCharm Community for bigger projects, and the limited Grasshopper editor for quick and dirty scripts. There are a couple of posts here on the forum on how to get autocomplete going in different external editors.

The GHPython component has the neat functionality to promote a Code input that it can read code from. This means that you can read and external .py file that your currently working on in an external editor, without needing to copy and paste code between the editor and the GHPython component.

Note that File (Read File) component that you need to read the .py file in the first place, is broken in Grasshopper on macOS and has been for some time now. I don’t know why the folks at McNeel have been unable to fix it thus far?
I’ve written a GHPython component that does the job, maybe even better, you can find it here.

Like mentioned before, Rhino uses IronPython 2.7 which is derived from .Net. It’s Python environment has nothing to do with your local CPython installation. It runs in an enclave within Rhino and you mostly can’t use CPython libraries, unless you have something that communicates between the two Pythons.

Probably not, no. And it wouldn’t help you much, if you could.

Hey Diff-arch,
thanks again for the detailed reply I know its tedious but it really helps me out and im working on a project to benefit others :slight_smile:

So my idea was to have an option to let a script run (over night) that should validate a whole lot of different combinations of input parameters that later will be chosen by the future users of the idea. So our goal is to “Test” the GH code to ensure that we always (as certain as possible) produce valid STLs. In the end I am not present while the code will run on the end-users machine. So that’s why my idea was to provide a list of calculated inputs (generated by python) to cover most of the ranges and let Rhino run over night or for an extended period to check hundreds of variants and report if any of them product invalid meshes. This then first can help me to gain confidence in my GH code, as well as helps us to discover bugs and parameter configurations that product invalid files at the moment. Does that make sense to you?

So I have my GH file that produces my mesh, would it be best to have a separate one to test parameters? - I think it might make sense, from what you describe to just build that automation with custom components inside my GH file? - Im fine with using the internal mechanisms if that is the best option (what you said makes sense to me).

So how can you use PyCharm (I love Jetbrains, they make awesome tools) for Rhino Scripting? - As far as I understood I cannot import IronPython (needed for scripting?) outside of Rhino? Or do you simply work without autocompletion and syntax checking?

Thanks so much that will be of great help I think!

got it!

Best,
Lukas

PS: Again thanks for taking the time and explaining the world to me, it means a lot :slight_smile: !

If I understand you correctly, you have some process that potentially runs a long time - over many iterations -, and that generates meshes?
If this is the case, why not implement the mesh validity checks here? This could potentially be a single GHPython component or Python script.
Once a mesh has been spit out from certain parameters, you immediately run the validation checks, and if they are successful, you for instance export an OBJ, FBX, or whatever, if not you discard the mesh object? There’s potentially no need to even bake objects, unless you need to do that for the export.

Yes, you can basically use any text editor, code editor, or IDE.

That’s not how it works. You don’t import IronPython, it’s an environment that you run the script in, an interpreter of your scripts! What you import from is rhinoscriptsyntax or RhinoCommon, which are libraries or APIs of code.

No, you can get it working. Check this out.

Hey diff-arch!
Again, I am so grateful for you taking the time to help me out! - It makes a great difference! :slight_smile:

Yes exactly, so our plan is now to build a small validation block in GH with a custom python module as u suggested :). Your idea makes a lot of sense to us. That means that we would have the custom block in GH und outside in Rhino only a small script that generates the different combinations of parameters that we wanna test the GH file with. Like this our idea is to then just be able to run the script which will set the parameters of the GH file and execute it. Is there a way to receive feedback from the GH file without exporting the object to rhino even maybe? - I would only need to know the result of the validation block, I do not need the actual mesh result.
Does that make sense to you or did I get my idea wrong?

Best,
Lukas

I guess you could streamline this idea a little more. Depending on what you want to do, you might not need Grasshopper at all.
You could possibly come up with a Rhino script that automatically goes through a folder on your computer, reads in files containing meshes, does something with them, analyses the result of the operation, and lastly writes out the validation result to some sort of file or database.

rhino phyton → get started here:

code completion - you can use atom editor or others…