Hello everyone, I have a Python script that modifies coordinates of a shape through a UI interface. The script works correctly on the first run, but since Dynamo caches results when inputs haven’t changed, I can’t run it again without modifying something in the nodes or code.
Current issue:
Script runs successfully first time
To run it again, I need to modify an input or the code
This is problematic because my script needs to run multiple times with the same inputs to process different user interactions
I considered using a Boolean toggle, but manually switching it between true/false for each run isn’t practical for my use case.
Questions:
Is there a way to automatically trigger the script to run without requiring input changes?
If using a Boolean, is it possible to have it automatically reset to false after each run, so I only need to click true when I want to run the script?
What’s the best practice for handling scripts that need to run multiple times with the same inputs?
What are the different user interactions? Your output is only going to change if an input has changed. If an input has changed then you can get that value and use it to “update” the python node and re-execute.
Ok, step 1 is take the graph off of automatic - this will doom loop you otherwise.
Step 2, is to make a path to a text file using the File Path node. This can be an empty file for the first run. Intent is to use it as a temp document that we won’t need to keep. Build a file from the path and provide the result as an input to the Python node (no need to modify the Python, it just needs to be wired in).
Step 3 is to take the final result (make sure EVERYTHING is done calculating by joining things into a list and using a waitfor method to return the number 1. Then use that to generate a random number using Math.RandomList(count)[0]+""; or the nodes to do the same. Write the resulting random double as a string to the same file which was passed into the Python node.
Step 4 is to hit ‘run’ (you took things off auto to prevent the doom loop, right?). Then hit it again. And again. Again. Over and over. As the file is ‘dirty’ from the write out at the end, the File From Path node re-executes, which causes the node to re-execute, which tells the Python node it has new data (even though your code doesn’t use it)
Non-Python files can be triggered the same way by using a waitfor method with the file as the wait and the required data as the passthrough.
***EDIT:*** Here’s an animation of it in action:
Notice the Python returning the current time updates on each press, and that the random number fed into the write text node alters on each run. Well for a few seconds until the gif loops anyway.
I am changing the input with the UI that I created with the python code that I run on Dynamo. So to run python code I need to change something but I can adjust “real” inputs after running the python code.
Thanks a lot Jocob. I will try it now. Do you know any method to define boolean as False by default. So I want to see it false each time after run even though I choose true at the beginning.
Not without writing your own view extension, and the UX on that sounds like a absolute nightmare as you’d have a value changing after each execution and the user would have to ‘hunt down’ each input as needed to set up the configuration.