Dyn2py - Extract python code from Dynamo graphs

Hello everyone!

I created a command line tool and python module to export python code from Dynamo graphs:

Some excerpt from the readme:

  • Track changes in python nodes in source control systems like git
  • Work on python code in your favorite code editor outside Dynamo. dyn2py can also update Dynamo graphs from previously exported python files.

Installation

Windows portable and installer

Prebuilt portable exe and installer available from github releases: Release 0.4.1 · infeeeee/dyn2py · GitHub

With pip

pip install dyn2py

Usage

As a standalone command line program

> dyn2py --help
usage: dyn2py [-h] [-v] [-l LOGLEVEL] [-n] [-F] [-b] [-f {py,dyn}] [-u] [-p path/to/folder] [source ...]

Extract python code from Dynamo graphs

positional arguments:
  source                path to a Dynamo graph, a python script or a folder containing them

options:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -l LOGLEVEL, --loglevel LOGLEVEL
                        set log level, possible options: HEADLESS, CRITICAL, ERROR, WARNING, INFO, DEBUG
  -n, --dry-run         do not modify files, only show log
  -F, --force           overwrite even if the files are older
  -b, --backup          create a backup for updated files
  -f {py,dyn}, --filter {py,dyn}
                        only check python or Dynamo graphs, skip the others, useful for folders

dynamo options, only for processing Dynamo graphs:
  -u, --update          update Dynamo graph from python scripts in the same folder
  -p path/to/folder, --python-folder path/to/folder
                        extract python scripts to this folder, read python scripts from here with --update

The script by default overwrites older files with newer files.
Do not move the source Dynamo graphs, or update won't work with them later.
Multiple sources are supported, separate them by spaces.
HEADLESS loglevel only prints modified filenames.

Examples

Notes: In Windows cmd use backward slashes as path separators, in any other shells use forward slashes. Powershell accepts both of them. Wrap paths with spaces in double quotes.

# Extract all nodes next to a Dynamo file:
dyn2py path/to/dynamofile.dyn

# Update a Dynamo file from previously exported and modified python files:
dyn2py --update path/to/dynamofile.dyn

# Extract python nodes to a specific folder, process multiple Dynamo files:
dyn2py --python-folder path/to/pythonfiles path/to/dynamofile1.dyn path/to/dynamofile2.dyn

# Update Dynamo files from python files from a folder. Only check python files, create backups:
dyn2py --filter py --backup path/to/pythonfiles

Git hooks

Git hooks are a built-in feature of Git that allow developers to automate tasks throughout the Git workflow. Read more here: https://githooks.com/

With the pre-commit hook it’s possible to add more files to the currently initialized commit.

You can find an example pre-commit hook here: pre-commit. Copy this file to the .git/hooks folder of your repo of Dynamo graphs. This folder is hidden by default, but it should exist in all initialized git repo. Do not rename this file.

This script will go through staged .dyn files and export python scripts from them, and add them to the current commit. Now you can check changed lines in a diff tool, you can see changed python code in a PR!

As a python module

Full API documentation available here: https://infeeeee.github.io/dyn2py

Troubleshooting

If you have a problem, open an issue on Github

You can also ask about this project on Dynamo Forum, don’t forget to ping me: @infeeeee

5 Likes

Thos is awesome @infeeeee! Quick clarification - does this tool parse .dyn fules for puthon nodes, and pull put the python code, or does it convert the .dyn itself to a python equivalent? :blush:

3 Likes

It parses the dyn file file, and creates a separate .py file for each python nodes.

The name of the python files are generated from the name of the dyn file, the id of the python node and the name of the python node if it was renamed.

It adds some header info to python files (in a multiline comment, so it’s still a perfectly valid python file):

  • dyn2py version used
  • extraction date
  • uuid of the dyn file
  • name of the dyn file
  • relative path of the dyn file
  • the id of the python node
  • the python engine of the node

With this info it’s possible to update the dyn file with a changed python file. Another usecase maybe to lint python nodes outside dynamo, than they will look nice in the built in editor.


IIRC there were some plans with tighter integration with vscode. I checked the roadmap, and haven’t find anything related to this. What happened to this plans?

1 Like

We didn’t have any roadmap plans for a tighter integration with VSCode as of yet, but had briefly talked about it :blush:

Right now, we have other things that are of higher priority (VM Rewrite, Dynamo as a Service, connection to Design Automation etc.)

1 Like