Function definition

Hi,

does any body know how it could be possible to define a function in a code block that gets a variable and return this as a project parameter something like this:

At the moment it’s impossible as you see that the function pps() returns null!!
Thank you!

If you’re not obligated to define functions in DS, check out the Prorubim packages, they have some functionality for this:

1 Like

but unfortunately I’m reading an excel file. And I noticed as long as the variable you are setting is a number it works perfectly, but in case of a variable between the number and SetVar the variable doesn’t appear in GetVar-List anymore. look at this:

:frowning:

I believe that you haven’t given your function a variable yet. Kinda like a node with no input.
Try changing the second code block to:
pps(1)

1 Like
1 Like

Thank you both. I’ll give it a try.

@Dimitar_Venkov Sorry but I just can’t get what you mean. Would you please give me a hint?

Thank you.

I have a similar problem and I tried to give a default value to a parameter of the function.
It works…

but only if the parameter is not a variable.

Is it something not possible or is it a bug?

3 Likes

I have exactly the same problem as you… Anybody got the answer for this?

Currently not possible.

1 Like

@ramoon.bandeira - what Dynamo version? I may be able to share a possible option if you’re in 2.0.

i’m using dynamo 1.3.3
But i think that i am not using any package nodes… only core nodes from dynamo, so i think your solution will work. Could you please share it with me?
Thanks!

Won’t work as 1.3 doesn’t support dictionaries natively. Can you upgrade?

EDIT:
If you only have a single variable my method could work with a few tweaks.

i can upgrade. I meant that i wont loose anything from my code if i use dynamo 2.0.
That was my innicial concern.
Could you please share this code? Thanks!

I’ll likely publish a package for it this weekend - have a bunch of naming changes to make first. If you want a sneak peak send me a PM and I’ll send you an advance copy of sorts.

1 Like

Decided against a package - too incomplete for what I want it to be just yet.

This idea was based off a post from @Dimitar_Venkov which he’s shown several times, but has the added benefit of maintaining numbers as numbers and strings as strings, and allowing you to define as many variable names and values as you like - well as many as excel will hold anyway…

The code is as follows:

//Update the return value to force a refresh of all variable nodes
/*I prefer the SpringsUI.Refresh node, but thought people might like a
built in alternative. */
def refresh()
	{
		return 0;
	};

def SourceFile()
//sets the source excel file to store the data in temporarily
//You will need to path this to an excel file of your choice manually.
/*This should likely be pathed to a directory which is common for all users,
not sure where is best just yet though*/
	{
		//set this path to your excel file!!!!
		return =
			"C:\\Users\\smallj\\AppData\\Roaming\\Dynamo\\Dynamo Core\\Variables.xlsx";
			//set this path to your excel file!!!
	};

def WriteVarFile(keys:var[], vals:var[])
//writes the dictionary to the source file.
	{
		refresh();
		values =
			[[List.Transpose([keys,vals])]];
		WriteFunction =
			Data.ExportExcel(
				SourceFile(),
				"Vars",
				0,
				0,
				[values]@L8<1>,
				true
			);
		return =
			"VarFile Updated at "+ DSCore.DateTime.Now;
	};
def GetAllVar()
//reads all variables stored in the file.
	{
		refresh();
		dataset =
			List.Transpose(
				Data.ImportExcel(
					FileSystem.FileFromPath(SourceFile()),
					"Vars",
					false,
					false
				)
			);
		return =
			Dictionary.ByKeysValues(
				List.FirstItem(dataset),
				List.LastItem(dataset)
			);
	};


def GetVarByName(x)
// reads a named variable from the source file.
/* The nodes which call this funciton must be placed
after the nodes which write the var file. */
	{
		refresh();
		dataset =
			List.Transpose(
				Data.ImportExcel(
					FileSystem.FileFromPath(SourceFile()),
					"Vars",
					false,
					false
				)
			);
		dctnry=
			Dictionary.ByKeysValues(
				List.FirstItem(dataset),
				List.LastItem(dataset)
			);
		return =
			Dictionary.ValueAtKey(
				dctnry,
				x
			);
	};

In action it looks like this:

Give it a shot and let me know what you think.

3 Likes

another solution to consider… just a static dictionary in a zero touch node.

Having a file output as well is nice though for debugging.

1 Like