In Dynamo code block, can i return a variable input by Functions

Hey everyone…

I am trying to define a function in this function i want to return an input x.
plz see the attached picture

1 Like

def selectedX(x){
return = x }

Michae, thank for replying, this will need the input every time i have to use the function. what i’m looking for is storing this variable in the function…

X is a variable in your function - you need to set it to be a real thing. See these examples:

In your case you’d need to change the code to be:

def selectedX()
{
return = 15432;
};
1 Like

I have not tested, it might be possible. But I don’t think you’ve described exactly what you want yet. Do you want to form a closure around some other variable? Do you want to set the variable the first time and then if it’s already set just use the existing value inside the function?

Seems like what you’re after is something along the lines of a class instance’s fields and properties that you can read from and write to. Design script functions don’t have any such “memory” however. Implementing such functionality and making it update dynamically is easier said than done. There’s a great package that can already do that, tho it hasn’t been updated in a while:

With a bit of creative thinking, you can make a “lowcost” version that uses files as buffers and the dynamic update functionality of the “File.FromPath” node, but you’ll also have to handle the serialization and deserialization of the data yourself:

2 Likes

@Dimitar_Venkov - what would you recommend to ensure that the write text always happens before the read text? I just built a test version which happens in the opposite order, resulting in a failure to read the right variable on run. It’s unlikely that it’d happen in a linear progression of graph development (you’d have written the variable before reading it), but something to keep in mind as I can see backtracking to re-work the nodes creating an issue.

i want to store the input in the function so whenever i call the function it will contain my variable input automatically without the need to connect it again.
in the last few examples you have shared i need to connect that variable everytime i want to use my function.
also i need to use a level inside my function, but getting the level in the code block is not possible as far as i know.

Your last node is failing as you defined v as an input for the var1 function, but didn’t give any inputs when you called var1.

Refreshing the graph in it’s entirety should allow you to call and recall the variable without issue, either though @Dimitar_Venkov’s method or mine (just be sure you write the code to write the file before you write the node which calls it). Springs.UI package has a node for this type of refresh.

If you want to narrow down the number of nodes which refresh, You could also code a refresh function and nest it in as an unused function. This would allow you to refresh the input for every one of your custom functions by changing one value in one code block.

I was wondering if there is a new alternative to what @Dimitar_Venkov shared (Prorubim package or File.FromPath). At the moment I’m trying to clean up my graph so I’m just looking for a way to have some “hidden wires” as in Grasshopper. I’m using functions to have most of my global parameters in one place. This works “fine-ish” for most variable types. But this solution comes short when I need to input materials for instance, which requires the drop-down list.
Thanks! :slight_smile:

Hello @Mohamad_Kayali
If you still interested to build your function with inputs inside in this function, may this will help you

1 Like

Hi,
Can you have a function that returns more than one variable?

I have tried to return to call the function prac that returns two lists, one with the x component of the vector while the other list with the y components of the same.

Kindly, assist.

Thanks.

Good evening,
here are 2 possibilities

def prac(t3:var[])
{
a=Autodesk.Geometry.Curve.TangentAtParameter(t3);
num1=Autodesk.Vector.X(a);
num2=Autodesk.Vector.Y(a);
return [[num1],[num2]];
};

with dictionnary

def prac2(t2:var[])
{
a1=Autodesk.Geometry.Curve.TangentAtParameter(t2);
dnum1=Autodesk.Vector.X(a1);
dnum2=Autodesk.Vector.Y(a1);
return {"Comp X":dnum1,"Comp Y":dnum2};
};

Cordially
christian.stan

2 Likes

hello @markochan2000
i wish that will hellp you

code:

def XYvector(x)
{
XYvect=Autodesk.Geometry.Line.Direction(x);
Xvect = Autodesk.Geometry.Vector.X(XYvect);
Yvect = Autodesk.Geometry.Vector.Y(XYvect);
return [Xvect,Yvect];

};

2 Likes

Thanks a lot. This works out too. Is it also possible to call a function within another function? In C++ programming this was possible.

1 Like

Both work perfectly fine. You are cool. Thanks a lot :slightly_smiling_face: :slightly_smiling_face:

1 Like

Dear Sam.
Greetings. I hit a snag somewhere. It works well but it seems the list itemizes the x coordinates then the Y coordinates. So how do I then create it in the fomr that I can create a true, false situation (boolean) that allows me to pick lines whose either x coordinate of the vector is zero or the y coordniate is zero? from this situation, the resulting items are doubled.
image

Hello @markochan2000

Check this to see if my script is correct
So to explain more my script I create a list of lines that I make a list of 2 pairs of them to have the same result as you already have in your project. After creating the list of lines, I create a function that returns the values Xvector and Yvector, then I compare them to see the position of the lines: if Xvector || or || Yvector ==0 Then the line is parallel to one of the axes (YAxis or XAxis)

Home2.dyn (73.3 KB)

code
def XYvector1(x)
{
XYvect=Autodesk.Geometry.Line.Direction(x);
Xvect = Autodesk.Geometry.Vector.X(XYvect);
Yvect = Autodesk.Geometry.Vector.Y(XYvect);

Compaire= Xvect || Yvect ==0?"Not_Parallel":"Yes_Parallel";

return [Compaire];

};

2 Likes

Hi Sam,
Thanks for your continous support. I think we are almost there. The information am getting is really valuable.
Is it possible now to return the the exact line from the function itself as I have highlighted here?
image

At the moment it returns the value either Not_Parallel or Yes_Parallel, of the comparison. Is it possible that we can come up with a mechanism of retrieving the actual line from the function so that whenever I call upon the function it retunrs the line? I am trying yo condense the program i.e. make it smaller. or maybe can we also condense the next section…this one

and this one

into another funtion so that we have a more compact canvas with less nodes.

I really appreciate you man.

THanks in advance.

1 Like

what i really needed is to define values one time as input and use them evreywhere in the script without the need to connect nodes with lines. and this was the solution for that:

there was also few other great solutions. thanks for everyone!

2 Likes