Batch modifying scripts

I want to write a python script that will iterate through all my scripts, and add a python script node to each script. this node will need to have IN[0] connected to the OUT of a particular node that i have in all my scripts.

I thought maybe i can just insert text of the node manually in the .dyn script file, before i spend much time on attempting this i thought i should post here and ask…is this method possible or is there a better method?

It’s worth trying manually just to see. Setup one graph, copy from the DYN, then paste into a new one. See what happens.

Definitely doable.

In terms of difficulty…

Adding a standalone node is 3/10.
Adding a node at the right location is 9/10.
Adding a wire from an unknown node to a new node is 9/10.
Hacking adding a new node into a location relative to the entire graph and then making the connection is about a 7/10.

All in, if you can somehow get rid of removing the connection to an existing node you’ll have an easier time with it (ie: if the previous node was Python based, add the code for the new Python node into the existing Python node).

1 Like

Should have thought to link to this before… might give you a head start on deciphering the editing methods.

I would like to create the process to automate inserting a new python script node into a graph and making the node connections, but for my case that fastest solution does look like just adding the code into an existing python node.

The one piece that i will need to experiment with is the IDs. Each node, connection, input and output has a unique 32 character ID (guid with no hypens?). Do you know if I can just generate this ID, or does it need to be assigned by Dynamo?

if i can just generate a guid and use that, the rest seems just like doing the work writing the code to insert the Node, Connectors, and NodeViews objects for the new node into the dyn file.

I don’t know as I haven’t tried directly (but one of the tools I linked does allow inserting a node so I guess it works to some extent), but I don’t see why you couldn’t do this.

One thing you may have missed… It isn’t just the nodes though. Each input port. Output port. The connectors. Annotations. Basically everything. And depending on where in the graph structure you are the GUIDs are structured differently… because why would this be easy?

And a last bit of advice which (hopefully goes without saying): make sure you aren’t using this on a graph, directory (or anything really) that you care about. Sandbox it all, and test the graphs at frequent intervals. The sample I linked copies to a new directory for you to test this way.

1 Like

use the commands via an extension.commandExecutive:

2 Likes

How would I implement this method, would I need to create a Dynamo extension? My original thought was just to write a python script and run it from a terminal to modify each .dyn file in a directory.

yes an extension.