Grasshopper

algorithmic modeling for Rhino

I am a bit new to Python. I have a hang of doing ghpythonlib.component... but not so much rhino common and rhinoscryptsyntax. First I tried doing Rhino.Geometry.Mesh.Reduce(), but then I noticed that it only returns a Boolean. Then I searched the internet, found this discussion and the script at the bottom worksAndrew did this in C#:

private void RunScript(Mesh M, double P, ref object A)
{
if(M != null){
Guid meshObj = doc.Objects.AddMesh(M);
doc.Objects.UnselectAll();
doc.Objects.Select(meshObj);
Rhino.RhinoApp.RunScript("_-ReduceMesh _ReductionPercentage " + Convert.ToString(P) + " _Enter", false);
Rhino.DocObjects.RhinoObject MObj = doc.Objects.GetSelectedObjects(false, false).First();
doc.Objects.Delete(meshObj, true);
A = MObj.Geometry;
}

How do I do this in Python. I understand the concept behind Rhino.RhinoApp.RunScript(), but how do I aim that command at a mesh (especially one from grasshopper). And what is the deal with this command being destructive to the original geometry?

Thanks.

Views: 2085

Replies to This Discussion

The reason this script does some things in a seemingly strange way is to get around the fact that the Rhino SDK does not expose the reduce mesh functionality. In order to use it from Grasshopper, you have to 1. take the GH mesh and bake it into the rhino document, 2. Run the reducemesh command, 3. gather the results of the command back in to grasshopper, and 3. delete the temporary mesh you put in Rhino. This is by no means ideal, but it's the only way I know of. 

As far as doing it in python, the method is approximately the same - see the attached script. The only major difference is that you need to iterate over the results retrieved from GetSelectedObjects (rather than just grabbing the first item). 

Attachments:
It is too bad. Rhinos reduce mesh command is really great, especially letting you define exact number of faces. Hopefully it will be exposed soon.

Thanks for the response. It helps for me to see the code written in Python. The reason why I needed this component was to make the mesh lighter so that things calculated faster later in the definition. But right now, pyMeshReduce is taking longer to execute than expected. Is there any wayto use ghpythonlib.parallel to speed this component up? Would it work with the whole select and deselect situation?

You might get some minor benefit by writing the Python script to work on a loop instead of on single items, but multithreading will not help you here because it's so dependent on operations in rhino. My guess is the c# script is your best bet for performance.

Hi!

I learn python script, and I wanna study rhinoApp and rhinoDoc.

If we can use reference site or tutorial, please tell me URL.

Thanks.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service