Skip to content

Runing Python Script from other file

alvpickmans edited this page Feb 10, 2018 · 5 revisions

After adding methods to parse, read and write XML format, I thought: What if I could read and re-use code from other files without the need of opening them?

This simple example does exactly that: parse a .dyn file into json format and by providing the right keys, the python script is retrieved and re-used.

Overall graph


1. Reading the file

As for now, .dyn files are XML formatted so reading and parsing to json format is quite simple.

Reafing the file

2. Getting all python nodes on file

JsonObjects have the ability to be created or queried by the use of nested keys. If a key has dots on it (i.e. "nested.key"), the package will identify this as nested by default and try to access that nested element if querying or build a nested structure.

Getting python nodes

In this step, retrieveing the python nodes is done in two steps:

  1. Getting the nested value Elements from the object Workspace. the nesting input is true by default.
  2. Getting the value for the key PythonNodeModels.PythonNode which has a dot but is not nested. The nesting input is set to false.

3. Filtering python scripts by @nickname

The @nickname key is the python sript node name. Although is not mandatory to rename your nodes, in this case is necessary to filter them in an easy way.

Filtering nodes

The FilterByKeyAndValue node works by comparing the values of all input JsonObjects. If the values are of type string, it will match if they contain even part of the searched parameter. Note on the example that value searched for is 'Greeting', but the node with name 'GreetingScript' is returned as matching.

Then, the script key is used to get the python script.

4. Using the Script

Finally, having the retrieved script as input to the node Python Script From String, and adding as many inputs as required, the script is run.

Using script

Download the dataset.

Notes

Currently Dynamo files are XML based format. With the coming release version 2.0, the Dynamo Team has made the decision of migrate to json structures. This workflow will work anyway, simply by replacing FromXMLFile to FromJsonFile and using the appropiate keys.