Python Nodes
Last updated
Last updated
Why would you use textual programming in Dynamo's visual programming environment? has many advantages. It allows you to create programs without learning special syntax in an intuitive visual interface. However, a visual program can become cluttered, and can at times fall short in functionality. For example, Python offers much more achievable methods for writing conditional statements (if/then) and looping. Python is a powerful tool that can extend the capabilities of Dynamo and allow you to replace many nodes with a few concise lines of code.
Visual Program:
Textual Program:
Like code blocks, Python nodes are a scripting interface within a visual programming environment. The Python node can be found under Script>Editor>Python Script in the library.
Double clicking the node opens the python script editor (you can also right click on the node and select Edit...). You’ll notice some boilerplate text at the top, which is meant to help you reference the libraries you’ll need. Inputs are stored in the IN array. Values are returned to Dynamo by assigning them to the OUT variable
Methods include constructors such as ByCoordinates, actions like Add, and queries like X, Y and Z coordinates.
Download the example file by clicking on the link below.
A full list of example files can be found in the Appendix.
In this example, we will write a python script that creates patterns from a solid module, and turn it into a custom node. First, let’s create our solid module using Dynamo nodes.
Rectangle.ByWidthLength: Create a rectangle that will be the base of our solid.
Surface.ByPatch: Connect the rectangle to the ‘closedCurve’ input to create the bottom surface.
Geometry.Translate: Connect the rectangle to the ‘geometry’ input to move it up, using a code block to specify the base thickness of our solid.
Polygon.Points: Query the translated rectangle to extract the corner points.
Geometry.Translate: Use a code block to create a list of four values corresponding to the four points, translating one corner of the solid up.
Polygon.ByPoints: Use the translated points to reconstruct the top polygon.
Surface.ByPatch: Connect the polygon to create the top surface.
Now that we have our top and bottom surfaces, let’s loft between the two profiles to create the sides of the solid.
List.Create: Connect the bottom rectangle and the top polygon to the index inputs.
Surface.ByLoft: Loft the two profiles to create the sides of the solid.
List.Create: Connect the top, side, and bottom surfaces to the index inputs to create a list of surfaces.
Solid.ByJoinedSurfaces: Join the surfaces to create the solid module.
Now that we have our solid, let’s drop a Python Script node onto the workspace.
To add additional inputs to the node, click the + icon on the node. The inputs are named IN[0], IN[1], etc. to indicate that they represent items in a list.
Let’s start by defining our inputs and output. Double click the node to open the python editor. Follow the code below to modify the code in the editor.
This code will make more sense as we progress in the exercise. Next we need to think about what information is required in order to array our solid module. First, we will need to know the dimensions of the solid to determine the translation distance. Due to a bounding box bug, we will have to use the edge curve geometry to create a bounding box.
Take a look at the Python node in Dynamo. Notice that we're using the same syntax as we see in the titles of the nodes in Dynamo. Check out the commented code below.
Since we will be both translating and rotating the solid modules, let’s use the Geometry.Transform operation. By looking at the Geometry.Transform node, we know that we will need a source coordinate system and a target coordinate system to transform the solid. The source is the context coordinate system of our solid, while the target will be a different coordinate system for each arrayed module. That means we will have to loop through the x and y values to transform the coordinate system differently each time.
Click Run then Save the code. Connect the Python node with our existing script as following.
Connect the output from Solid.ByJoinedSurfaces as the first input for the Python Node and use a Code Block to define the other inputs.
Create a Topology.Edges node and use the output from Python node as its input.
Finally, create an Edge.CurveGeometry node and use the output from Topology.Edges as its input.
Try changing the seed value to create different patterns. You can also change the parameters of the solid module itself for different effects.
Now that we have created a useful python script, let’s save it as a custom node. Select the python script node, right-click on Workspace and select ‘Create Custom Node.’
Assign a name, description and category.
This will open a new workspace in which to edit the custom node.
Inputs: Change the input names to be more descriptive and add data types and default values.
Output: Change the output name
Save the node as a .dyf file and you should see the custom node reflects the changes we just made.
The Autodesk.DesignScript.Geometry library allows you to use dot notation similar to Code Blocks. For more information on Dynamo syntax, refer to as well as the (To download this PDF doc, please right-click on link and choose "Save link as..."). Typing a geometry type such as 'Point.' will bring up a list of methods for creating and querying points.