-
Notifications
You must be signed in to change notification settings - Fork 2
Runing Python Script from other file
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.
As for now, .dyn files are XML formatted so reading and parsing to json format is quite simple.
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.
In this step, retrieveing the python nodes is done in two steps:
- Getting the nested value
Elements
from the objectWorkspace
. thenesting
input istrue
by default. - Getting the value for the key
PythonNodeModels.PythonNode
which has a dot but is not nested. Thenesting
input is set tofalse
.
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.
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.
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.
Download the dataset.
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.