Conditional Statement in a Function

It’s been too long and it’s time I learn how to properly format conditional statements because they are so useful, espeically when they are brought outside the codeblock into their own little function. This seemed like such a simple conditional statement but after trying for hours I can’t even get this simple conditional statement to work and am now out of ideas.

I’m trying to take the number squence and if it’s only 1 pass it through or else pass the squence through while dropping the last number.

Hi,

  1. Your first return statement is missing a semicolon.

  2. If you want the “it3” variable to be treated as a list, you’ll need to define it as a “:var[]”

1 Like

In addition to Dimitar’s observations

  • Avoid the semi colon at the end of the if statement

  • While using conditional statements you’ll need an Imperative block

    def subcode(it2:var,it3:var)
    {
    return=[Imperative]
    {
    if(it==1)
    {
    return=it2;
    }
    else
    {
    return=List.DropItems(it3,-1);
    }
    }
    };

3 Likes

Much Appreciated! I guess you guys know this stuff because of your more extensive knowledge of python? I have a couple long days at work but will try to run these suggestions into the code soon and reply back.

This is Design Script
While Dimitar is a master I don’t know much Python myself. :slight_smile:

Tweeked the code a bit more. Everything is working now. Thanks.

1 Like

Continuing now with a more complex implementation of a conditional statement, I was pretty sure I had the main block of code in the right spot for the whole function to read it but after experimenting and troubleshooting I continue to encounter errors: “Null value cannot be cast to Double”

I like the flexibilty of bringing the code out into its own function becaues it helps accommodate the strange effects of the 1st tesselation before it reads corrects again.

Hi @PerfectArchCo
The return value of your else statement is created inside the if statement . There might me other problems but it’s the first one I see . Can you paste your code here ?

1 Like

Sorry I wasn’t able to run through the edits. Please see below:


def subcode(dai:var,n:var)
{
return=[Imperative]
{
if(n==1)
{
rad=dai/2;
p=(1+Math.Sqrt(5))/2;
a=Point.ByCoordinates({0,0,0,0,p,-p,-p,p,1,-1,-1,1},{1,-1,-1,1,0,0,0,0,p,p,-p,-p},{p,p,-p,-p,1,1,-1,-1,0,0,0,0});
b={{a[0],a[8],a[9]},{a[0],a[9],a[5]},{a[0],a[5],a[1]},{a[0],a[1],a[4]},{a[0],a[4],a[8]},
{a[1],a[5],a[10]},{a[1],a[10],a[11]},{a[1],a[11],a[4]},{a[2],a[3],a[7]},{a[2],a[7],a[11]},
{a[2],a[11],a[10]},{a[2],a[10],a[6]},{a[2],a[6],a[3]},{a[3],a[6],a[9]},{a[3],a[9],a[8]},
{a[3],a[8],a[7]},{a[4],a[11],a[7]},{a[4],a[7],a[8]},{a[5],a[9],a[6]},{a[5],a[6],a[10]}};
tri=Surface.ByPerimeterPoints(b);

crv1=tri.PerimeterCurves();
len1=List.GetItemAtIndex(crv1<1>,0).Length;
len2=len1/n;

pnt1=List.GetItemAtIndex(crv1<1>,0).StartPoint;
pnt2=List.GetItemAtIndex(crv1<1>,1).StartPoint;
pnt3=List.GetItemAtIndex(crv1<1>,2).StartPoint;

dir1=Vector.ByTwoPoints(pnt1,pnt2);
dir2=Vector.ByTwoPoints(pnt1,pnt3);
dir3=Vector.ByTwoPoints(pnt2,pnt3);

pnt4=pnt1.Translate(dir1,len2);
pnt5=pnt1.Translate(dir2,len2);
sub1=Polygon.ByPoints(Transpose({pnt1,pnt4,pnt5}));

n2=(0…(n-1))*len2<1>;
sub2=sub1.Translate(dir1,n2);
n3=List.TakeItems(n2<1>,1…n);
sub3=sub2.Translate(dir3,n3);

pnt6=pnt4.Translate(dir2,len2);
sub4=Polygon.ByPoints(Transpose({pnt4,pnt5,pnt6}));
n4=(0…(n-2))*len2<1>;
sub5=sub4.Translate(dir1,n4);
n5=List.TakeItems(n4<1>,1…n);
sub6=sub5.Translate(dir3,n5);

plPnt1=Flatten({sub3,sub6}).Points;
plDir1=Vector.ByTwoPoints(Point.Origin(),plPnt1);

sph=Sphere.ByCenterPointRadius(Point.Origin(),rad);
plPnt21=Flatten(sph.ProjectInputOnto(plPnt1,plDir1)<1>);
pl1=Polygon.ByPoints(plPnt21);
sr1=pl1.Patch();
{
return=crv1;
}
else
{
return=sr1;
}
}
};

There’s a lot of problems with your code:

  1. The IF statement has no closing bracket (see above return=sr1 that bracket should be removed)
  2. You declare sr1 in the TRUE statement block but also return it in the ELSE statement block (i.e. if IF is false), meaning you will return null. If you want to return a null object use return = null; otherwise declare sr1 outside of the IF statement
  3. In the TRUE statement bock you return crv1 which renders all of your code from dir1 up to your return statement redundant. Why?
  4. Usage of <1> replication guides in all cases is incorrect and causes the definition to fail
1 Like

There’s a lot of problems with your code:

Tell me about it. :smirk: But… trust me it’s matched by my perseverance!

I’m actually leaving work early today for a volunteer commitment all weekend so this isn’t quite top of mind. I plan to run in the changes next week and so in this short space I only wanted to say thanks!

1 Like

You declare sr1 in the TRUE statement block but also return it in the ELSE statement block (i.e. if IF is false), meaning you will return null. If you want to return a null object use return = null; otherwise declare sr1 outside of the IF statement

I assumed each conditional statement would just read off the same main core of code and return each sr1 and curv1 depending on the specified conditions. But it sounds like the function treats the code strictly in terms of true or false. Leading me to ask…

In the TRUE statement bock you return crv1 which renders all of your code from dir1 up to your return statement redundant. Why?

To the best of my ability that’s were I thought it should go. Where does code that both conditional statements refer to and use go? Earlier in the week I had experimented with various locations derived from the the sparse info online about advanced design script conditional statements.

I’ve posted code that resolves the first tesselation issue here