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:
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:
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!
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.
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
);
};