Skip to main content

Modify element with Python

ยท 2 min read
Marco Pellegrino

One use case where the API is leveraged is the modification of some of the properties of an object such as Slab, Bar, Axis ect ect.

The "Grasshopper way" to modify an object requires to create a new one starting from the properties of the deconstructed object and insert the new values where needed. The example below shows how to modify the thickness of a Slab.

construct-deconstruct

However, there is a better/improved/faster way to do modify properties. It only required few knowledge of programming and it is my favorite way to manipulate data inside the FEM-Design model.

Let's say that we want to modify the thickness of a slab and the identifier of a beam of an existing model.

The step to follow are:

1. Create a python component
2. Add the required input for your script. Zooming in the python component will allow you to add as many input as needed.
3. Right click on the generated input in order to change the name and set the `type`.
4. Write your code to modify the properties.

python

Bar exampleโ€‹

import copy

import clr
clr.AddReferenceToFileAndPath(r"C:\Users\Marco\AppData\Roaming\Grasshopper\Libraries\FemDesign\FemDesign.Core.dll")
#You must reference the FemDesign.Core.dll using the location on your machine


new_bar = copy.deepcopy(x)
new_bar.Identifier = identifier

One of common mistake when modifying object in Grasshopper is NOT doing the copy of the object that we want to modify. The copy will kind of detach the original object from the new one so that the modification will not be upstream.

After the copy, we need to find the properties to modify navigating through the object with the use of character .

new_bar.Identifier = identifier is selecting the Identifier of the new_bar and it is setting the identifier to it.

Slab exampleโ€‹

import copy

import clr
clr.AddReferenceToFileAndPath(r"C:\Users\Marco\AppData\Roaming\Grasshopper\Libraries\FemDesign\FemDesign.Core.dll")
#You must reference the FemDesign.Core.dll using the location on your machine

new_slab = copy.deepcopy(x)

new_slab.SlabPart.Thickness[0].Value = tck

The slab example is basically the same but, due to a different data structure, we need to select a list item and set the value to it.

You will end up in really clean workflow which will not scare people and easy to maintain :)

gh-workflow

You can download the Grasshopper definition used in this tutorial from here ๐Ÿ‘‰Grasshopper Definition