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!