How to declare a "global" variable to be used across the script

Hi,

I want to declare a few variables that could hold a string for example, so that I can just type that variable in any code block without having to type the whole string every time. Is this possible, and if so, how?

Simply, in a code block somewhere…

def st1()
{
return=“Really long string i don’t want to type each time”;
};

Then use st1() in the code blocks throughout your script.

Have you looked at the design script language guide here :wink: plenty of good tips in there.

https://www.google.co.nz/url?sa=t&source=web&rct=j&url=http://dynamobim.org/wp-content/uploads/forum-assets/colin-mccroneautodesk-com/07/10/Dynamo_language_guide_version_1.pdf&ved=2ahUKEwi1h-zyso7ZAhWHLpQKHdTSDMEQFjACegQIExAB&usg=AOvVaw0ZCPyEz4ntO9i_nzsJbOGI

2 Likes

Thanks! I should really go through all the design script basics one of these days.

2 Likes

Yes replying to a 3 year old thread… @jacob.small - Ron Allen
Is there a way to assign a code block a variable with an input or a set then pick up those variables elsewhere like a traditional variable? I was following along with this <> on Youtube, and tried to store the “logo” and “Documents” into a variable but didn’t get values out from the code blocks on the other side. Looking at ways to reduce the looong connectors for ‘global’ variables…


pcoip_client_Dyc7dtpkyO

Currently, this is still not possible. The only thing you can call directly is a function. If your variable is constant (between projects/runs/whatever) you could technically write it to a function and call the function. However, this workaround does not allow you to define the “variable” with an input - it must be hard coded.
image

2 Likes

A few options:

  1. You can store the values by serializing them into the graph with a Data.Remember node. These can be picked up and put wherever. This would require you build the data to be remembered, connect the result to the input of a remember node for each use you’ll have, run the graph, disconnect the inputs, run the graph again, and save the graph. You will have to manage and deploy this package to all users acordingly, and any ‘save as’ before data was opened could cause loss of content.

  2. You can declare a definition as @Nick_Boyts showed above. Convert all of your nodes into design script and return the final result in the definition. This would re-execute the entirety of the code each time, which shouldn’t impact much for reading an image, but would be undesirable for reading the pixels.

  3. You could build a custom node which returns the data you want, input free. This would require deploying and maintaining it as a package, internally or on the package manager. That will be much easier than maintaining raw code in the graph IMO. This will also require executing the entirety in each run, but could include a Remember node and creative use of ‘frozen’ nodes in the custom node to combine the benefits of #1 & #2, with the easier maintenance, reduction of runtime and easier reuse. The package Generative Design at Hogwarts which I put together for AU 2020 did this a lot.

  4. You could serialize the data into an external source, and read that in after the fact by Design Script or Python. Deserialization can be tricky, and concurrent file access could be an issue.

Maybe Extensible Storage?

Would certainly work as a location to store the data, but it would require a custom node or Python to access the content.

I’ve always been hopeful we’ll see something like ‘Telepathy’ for Grasshopper built into wiring options one day. They use a send/receive node setup with a named list of variables. It would rely on a portion of Dynamo that can update components dynamically, so doubt a custom node setup would work. Grasshopper is ‘always on’ so I appreciate it’s quite different.

Sharing just in case it helps give the dev team ideas. Might be of interest to @solamour as well.

image

3 Likes

We have hidden wires in the Daily Builds now :slight_smile: Would this achieve what you are after?

3 Likes

Ah yes that’s pretty handy! Telepathy is a bit different in that you see the collected variable but hidden wires are a similar effective outcome.

1 Like

Coolio - Then this will be in the next release of Dynamo :raised_hands:

5 Likes

@solamour
While your’e at it, a workflow that integrates ML with point cloud Wall recognition, or Floor and so on, wouldn’t be too hard after Telepathy :slight_smile:
Sounds as easier to me

5 Likes

Hah… that one’s a little bigger than hiding a wire! :nerd_face: But duly noted it’s a want.

4 Likes

I think half the AEC developer community is trying to crack that currently (the other half gave up). There are recognition models out there but none of them make it an easy process - need ML ability to use them I think.

1 Like

I find a lot of those focusing on that problem are hung up on the wrong thing. Meshing the objects on demand (ie: what MR applications do) and processing that into a lightweight start to your project and is likely to produce a more useful base data set for ML applications.

3 Likes

Yes I expect just forming a 3D mesh will be the formal version 1 approach we see hit the mainstream to their expectations. Many people are focusing on generating native elements which is a can of worms for every single category of element (e.g. wall thicknesses needing wall types, and the realisation walls are variable thickness due to construction accuracy/bowing etc.).

2 Likes

I see you’re keeping the pretty dynamo looks hidden away in the Daily Builds :slight_smile:

On another note, it’s nice to see that hidden wires are being implemented. That should make the graphs much cleaner and easier to manage.

I hope there would also be a way to quickly check a list of connections to a specific node with hidden wires. Maybe it could be a separate list window when you right click on a node to “show list of connections” or it could be incorporated into a sidebar. Then clicking on the items in the list could zoom the screen to that node.

1 Like

Hi @sed07 - We are hard at work fixing a bunch of bugs and interactions with the Visual Refresh stuff… it won’t be stuck behind Daily Builds forever! :smiley: We have plans to let it all out into the wild in the next release of Dynamo, all going well.

We don’t currently have plans to ship in 2.13 an extension that does this, but what we are planning to come out of the box is as follows:

  • Selecting a node with hidden wires shows a “ghosted” version of those wires, which you can interact with
  • Output ports will have a context menu that enables you to interact with the hidden wires (Similar to the on-wire context menu we are adding)
  • On-wire right click context menu enables hiding/showing of wires and breaking of connections

The extension is a good idea though…could be a great addition from the community? :wink: If not, we can evaluate how the above lands and see if it’s needed.

Screenshot showing a ghosted wire ad the right-click wire menu:

Screenshot showing the output port context menu for said wire:

2 Likes

After my reply, I actually got excited and went ahead and downloaded the Daily Build to take a look at. It’s pretty neat! Is there somewhere you guys accept community feedback from Daily Build testers?

I was able to figure out the wire stuff, but didn’t notice that clicking on the arrow on the output did something.

Without going into an extension, I had a couple of ideas. I’m not sure if they are something that you are working on already, but I’ll mention them anyway.

  1. Maybe it would be nice to be able to select multiple wires in a more flexible way (like a window selection, but only if no nodes are also included in the selection), because right now if there’s a lot of connections it would be a hassle to control each one individually.

  2. Another idea would be to make the output menu have a second option to show wires, and maybe also add the option to select connections (which would show all the connections, not just the one for a single wire).

1 Like