Running Python script from shared parameter - Python Script From String use cases

TL;DR: Have you used the “Python Script From String” node or know some ways in which its been used?

Hey all,
Because I am always curious to hear what ChatGPT has to say I asked it what are some options for Dynamo script version control, and one of its suggestions was:

  1. Revit shared parameters: You could use shared parameters in Revit to store the Dynamo scripts. This would allow users to access the scripts directly from within Revit, and also ensure that everyone is using the same scripts. However, this may not be suitable if the scripts are large or complex.

I asked it how I would run a Dynamo script from a shared parameter, and it responded:

I don’t know anything about a “Dynamo.Execute” or “Dynamo.Run” node, but there is the “Python Script From String” node which would actually make running a python script from a shared parameter possible?

I don’t know if there is any practical use for this, but it got me wondering what use cases there may be out there for the “Python Script From String” node.

Have you used this node or know some ways in which its been used?

Dynamo.Execute and Dynamo.Run. Good old ChatGPT just making things up :rofl:

Python Script from String basically just turns a string into a Python script, so instead of editing your Python Script node, you just have your code in the form of a string, and feed that into Python Script from String node.

So yes, you could store a Python script inside a parameter in your project, and then have a Dynamo script that loads this parameter and feeds it into Python Script from string.

But that seems like a truly awful way to do things.

indeed it does seem like an awful way to do things. I’m familiar with how the “Python Script From String” node works but interested in hearing how people have used it, like instead just using the python node.

Have you used it or seen it used?

Sigh… I hate chat gpt as it gives some really bad advice.

First thing is first, and this has been pointed out already but I would be negligent to not say something: Chat GPT makes stuff up all the damn time. It’s also been said already many, many, many times that for the niche market that is AEC tech, I suggest using this crazy new technology called Google.com instead. I spent 30+ minutes trying to get code to make a wall in Revit, live tweeting as I went. After that 30 minute timer was done I gave up and did a Google. Functional code was there in under 2 minutes.

Second thing: if I ever see a Revit file with .dyn or .py code embedded in a shared parameter like this I will put the author into an extended time out so they can think about what it is they have done. If you had a valid reason for putting code into a rvt (you don’t, but assuming you did for the sake of argument) There are valid ways to embed external data into an rvt. Use those. But do not use parameter values to do so.

Ok, now onto the question…

The Python Script From String node allows you to read a string that runs your code and execute that. This means that you can paste the code into a string node and pass that into the Python Script From String node to get the code to run.

Why you would want to do that? Well you likely wouldn’t. That much text in a string node will generally cause graph navigation and loading to slow down. It’d literally be faster to place the code in the standard Python node. But you might want to load that Python code from an external file, say ‘MyScript.py’, which can facilitate loading code from an external library, which is both somewhat useful as a means of loading content (ie the path to the file could be derived from your Revit build to account for versioning changes) and cool.

However that too can be accomplished with the native Python node by adding a reference to the ‘MyScript.py’ file, which has the added benefit of allowing partial loads of that larger code base. So in the end, there isn’t a ‘must have’ reason for that feature, but it does have some intermittent uses.

Hope this helps, and please don’t put code into a parameter value. Please do stay curious as to how content can be leveraged in new ways. :slight_smile:

2 Likes

:laughing: ChatGPT really does make stuff up all the time, its terrible at writing code, but I do think its useful for brainstorming. Though much better, I haven’t tried Codeium or Copilot for generating code that interacts with RevitAPI but I’m guessing you are still better off just using a search engine.

This suggestion to use a shared parameter for storing a script was pretty funny and so out of left field it was interesting, I knew this part of the title of this thread would make ppl say WTF but thought it would be entertaining to share that part but it looks like its ChatGPT is quite a triggering topic lol.

I was hoping maybe there was an interesting/create use case out there for Python Script From String node that someone could share but sounds like its probably just a tool in the toolbox nobody has a good reason to use.

1 Like

This is a wild idea. I can’t decide if it’s insane or brilliant (not as-is, as-is it’s crazy, but the general idea is interesting).

1 Like

You’d essentially be blindly running Python code if you want to put a string into dynamo player, but it would be possible.

Can’t think of when or where this is a good idea really, as it would still depend on the inputs/output management in Dynamo as to what you do with that code outside of the python node.

Also chatGPT is an OK copilot, but an awful pilot.

1 Like