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