Rebuilding mesh normals by python code

Hello, I want to rebuild mesh normals for a stripped morphology of meshes in grasshopper. I saw a script that used C# script to do this. Can I do the same in grasshopper python? I am writing a program, but I am new to python might be some mistakes. Can anyone help me whit how to do this with python?

my attempt in python

In Python, it is not very different from the C# example you show above.

  1. Right-click on the x input to the GhPython Script component.

  2. From the pop-up menu, select Type hint → Mesh.

  1. Connect you mesh to the x input of the GhPython Script component.

  2. Enter two lines of code:

x.RebuildNormals()
a = x

  1. Run the script and the mesh with rebuilt normals will be available at the a output of the GhPython Script component.

For writing Python scripts in Grasshopper, I would recommend using the RhinoCommon API instead of RhinoScriptSyntax.

Here’s the API reference for the Mesh Class:

https://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Geometry_Mesh.htm

Edit: corrected error in code above.

-Kevin

1 Like

Thank you Kevin. I was missing the type hint step along with the understanding that a method can be applied to a list directly. I was trying to take each mesh element through loop and apply this method.

You can also set the Type Hint to “No Type Hint”. Grasshopper will determine the object type from the input data.

The inputs on the script components also have settings for accessing input data as items, lists, or trees.

If you leave it set to Item Access and input a list or tree grasshopper will run your script once for each item iterating through the list or tree (this is the easiest method if you don’t need to operate on lists or trees).

If you set it to List Access you can iterate through the list as you were trying to do initially.

If you set it to Tree Access, you can use the DataTree methods to access your data. You can also use the ghpythonlib.treehelpers module to transform trees to nested lists, and vice versa.

More info here:

-Kevin

1 Like

@parametriccuriosity

Note that there was an error in my previous post. You need 2 lines in your script:

x.RebuildNormals()
a = x

Edited previous post to correct error.

-Kevin

Thank you. I was able to achieve what I wanted. By rebuilding normals I got to see vibrant colors in my stripped form.