Get Custom Packages from .DYN files

Hi Everyone,

I am using a custom Python script I wrote which check the directory for .dyn files and will use the script to also get the custom node packages the script uses.

However I am having issues with the script returning the custom packages , it manages to collect the.dyn files but not the custom packages the definition uses. I have posted the script below and I have kept out the directory path as this is for work therefore I cant go sharing directory paths. so if you do test this make sure to add your testing directory in place of Insert Directory Path

Load the Python Standard and DesignScript Libraries

import sys
import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

The inputs to this node will be stored as a list in the IN variables.

dataEnteringNode = IN

Place your code below this line

import os
import xml.etree.ElementTree as ET

Set the directory path here

directory = r"Insert directory Path"

def get_dynamo_files(directory):
“”“Retrieve all Dynamo definition files from the directory.”“”
dynamo_files =
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(“.dyn”):
dynamo_files.append(os.path.join(root, file))
return dynamo_files

def extract_referenced_files(dynamo_file):
“”“Extract .xml and .json files referenced in the Dynamo definition file.”“”
try:
tree = ET.parse(dynamo_file)
root = tree.getroot()
referenced_files = set()

    print(f"Processing file: {dynamo_file}")

    for elem in root.iter():
        for attr in elem.attrib.values():
            if attr.endswith(".xml") or attr.endswith(".json"):
                referenced_files.add(attr)
                print(f"Found referenced file: {attr}")

    if not referenced_files:
        print("No .xml or .json references found in this file.")

    return referenced_files
except ET.ParseError:
    print(f"Error parsing file: {dynamo_file}")
    return set()

def main(directory):
“”“Main function to process all Dynamo definition files in the directory.”“”
dynamo_files = get_dynamo_files(directory)
all_referenced_files = {}
for dyn_file in dynamo_files:
referenced_files = extract_referenced_files(dyn_file)
all_referenced_files[dyn_file] = referenced_files

result = []
for dyn_file, referenced_files in all_referenced_files.items():
    file_info = {"File": dyn_file, "Referenced Files": list(referenced_files)}
    result.append(file_info)

return result

Execute the main function and store the result

result = main(directory)

For Dynamo output

OUT = result

If anyone could assist I would greatly appreciate you!

@Benji-BIM

paste your code
grafik

#Load the Python Standard and DesignScript Libraries

import sys
import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *
# The inputs to this node will be stored as a list in the IN variables.

dataEnteringNode = IN[0]
# Place your code below this line

import os
import xml.etree.ElementTree as ET
Set the directory path here

directory = r"Insert directory Path"

def get_dynamo_files(directory):
"""Retrieve all Dynamo definition files from the directory."""
    dynamo_files = for root, dirs, files in os.walk(directory):
    for file in files:
if file.endswith(“.dyn”):
dynamo_files.append(os.path.join(root, file))
return dynamo_files

def extract_referenced_files(dynamo_file):
"""Extract .xml and .json files referenced in the Dynamo definition file."""
    try:
        tree = ET.parse(dynamo_file)
        root = tree.getroot()
        referenced_files = set()

    return ({f"Processing file: {dynamo_file}")

    for elem in root.iter():
        for attr in elem.attrib.values():
            if attr.endswith(".xml") or attr.endswith(".json"):
                referenced_files.add(attr)
                print(f"Found referenced file: {attr}")

    if not referenced_files:
        print("No .xml or .json references found in this file.")

    return referenced_files
except ET.ParseError:
    print(f"Error parsing file: {dynamo_file}")
    return set()

def main(directory):
"""Main function to process all Dynamo definition files in the directory."""
    dynamo_files = get_dynamo_files(directory)
    all_referenced_files = {}
     for dyn_file in dynamo_files:
          referenced_files = extract_referenced_files(dyn_file)
          all_referenced_files[dyn_file] = referenced_files

result = []
for dyn_file, referenced_files in all_referenced_files.items():
    file_info = {"File": dyn_file, "Referenced Files": list(referenced_files)}
    result.append(file_info)

return result

# Execute the main function and store the result

result = main(directory)
# For Dynamo output

OUT = result

Current .dyn files are JSON, try this (CPython3)

import json
from pathlib import Path

filepaths = Path(IN[0]).glob("*.dyn")

parsed_dyn = [json.loads(fp.read_text(encoding="utf-8")) for fp in filepaths]

OUT = {
    data.get("Name"): [d.get("Name") for d in data.get("NodeLibraryDependencies")]
    for data in parsed_dyn
    if data.get("NodeLibraryDependencies")
}
1 Like

Thank you Draxl I tried pasting this into a python node and it come up with numerous errors. Im not to adept on Python yet hence why I cant seem to fix the errors but thank you for helping and taking time to write your response.

Thank you Mike this has worked for me.

I didn’t know how to merge the two as ive stated im just starting out on my Python Journey but I did use a custom GPT model called Code Copilot to merge them and it now works perfectly!

1 Like