# Def using "While"

**URL:** <https://forum.dynamobim.com/t/def-using-while/16683>\
**Category:** Geometry\
**Tags:** designscript, python\
**Created:** [November 20, 2017, 11:15pm UTC](https://forum.dynamobim.com/t/def-using-while/16683 "2017-11-20T23:15:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Engel1](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mike_engel1/32/9075_2.png) [@Mike\_Engel1](https://forum.dynamobim.com/u/Mike_Engel1)\
**Post date:** [November 20, 2017, 11:15pm UTC](https://forum.dynamobim.com/t/def-using-while/16683/1 "2017-11-20T23:15:34Z")

</div>

As usuall it seams to be the simplest ones that are the hardest.

End End Goal is to have more use of loop / while functions to allow itterating through list of items.

End Goal today is simply to creat a def where once can take a close poly curve, and nest a copy of itself with in it rotated by a an amount.

In a one time process this works fine. - code block text below.

PC\_1 = PolyCurve.Curves(results);  
pts1 = Curve.PointAtParameter(PC\_1,0.2);  
pts2 =List.ShiftIndices(pts1,1);  
ln = Line.ByStartPointEndPoint(pts2,pts1);  
PC = PolyCurve.ByJoinedCurves(ln);

and i can string these together in series to nest ever smaller shapes and given amount.

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/1/6154b7696dad215b4dc3fdb4273d1cf402a90741.png)

this seams like a waste of steps. So I attempted a simple def when I could loop the process to a given amount of itterations.

I receive no errors, and it appears to finish (dynamo doesnt go into a endless loop) but after much editiing and trial all that appears to return is “null”

I have completed an approximation of this same defintion swaping all geometry oppertations for math and it will work. Is there a limitation to looping geomotric functions, or am i missing something else.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/9/f9ec1dff1a5f9920dd2e2a3ca8d8dda272f83670.png)

def test(Geom,Itt)  
{  
return = [Imperative]  
{  
//  
sequence = {Geom};  
i = List.Count(sequence);  
while (i != Itt)  
{  
results = sequence[0];  
PC\_1 = PolyCurve.Curves(results);  
pts1 = Curve.PointAtParameter(PC\_1,0.2);  
pts2 =List.ShiftIndices(pts1,1);  
ln = Line.ByStartPointEndPoint(pts2,pts1);  
PC = PolyCurve.ByJoinedCurves(ln);  
sequence = List.AddItemToEnd(PC,sequence);  
i=i+1;  
}  
return = {i,results};  
}  
};

---

<div class="post-metadata">

**Author:** ![Michael\_Kirschner2](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/michael_kirschner2/32/75576_2.png) [@Michael\_Kirschner2](https://forum.dynamobim.com/u/Michael_Kirschner2)\
**Post date:** [November 21, 2017, 1:14am UTC](https://forum.dynamobim.com/t/def-using-while/16683/2 "2017-11-21T01:14:32Z")

</div>

What version of Dynamo is this?

---

<div class="post-metadata">

**Author:** ![Mike\_Engel1](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mike_engel1/32/9075_2.png) [@Mike\_Engel1](https://forum.dynamobim.com/u/Mike_Engel1)\
**Post date:** [November 21, 2017, 1:41am UTC](https://forum.dynamobim.com/t/def-using-while/16683/3 "2017-11-21T01:41:29Z")

</div>

Dynamo Studio 1.3  
Dynamo Core 1.3.1  
Attempted in both studio and sandbox

---

<div class="post-metadata">

**Author:** ![Dimitar\_Venkov](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/dimitar_venkov/32/2068_2.png) [@Dimitar\_Venkov](https://forum.dynamobim.com/u/Dimitar_Venkov)\
**Post date:** [November 21, 2017, 2:39am UTC](https://forum.dynamobim.com/t/def-using-while/16683/4 "2017-11-21T02:39:42Z")

</div>

You have to remember that you can’t use associative calls such as “Curve.PointAtParameter” on a list of objects in imperative blocks. Try the below version instead:

```
def recursive(Geom, Itt){
return = [Imperative]{
sequence = {Geom};
i = 0;
while (i < Itt)
{
	curves = Geom.Curves();
	pts1 = {};
	for (j in GetKeys(curves) )
	{
		pts1[j] = curves[j].PointAtParameter(0.2);
	}
	Geom = PolyCurve.ByPoints(pts1, 1);
	sequence = List.AddItemToEnd(Geom, sequence);
	i = i + 1;
}
return = sequence;
}};

```

 ![DynamoSandbox_2017-11-21_10-20-01](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/9/29bfe94627e1a8b081bd53fa5e4df96b6a704f8f.png)

Imperative DS functions are more trouble than they’re worth, now more than ever. If anybody is considering using them, arm yourself with a ton of patience. These few lines of code took 5+ attempts. You’ve never gotten any feedback and debugging information but now it’s worse than ever…

- Using class methods (like PolyCurve.Curves(x) ) failed silently
- If you have any ZT packages that use the same names as the core namespaces and classes (List, Solid, Point, Vector), they become unusable in in DS
- I had to copy/paste the function multiple times because it didn’t properly detect code changes
- For some weird reason, all of a sudden the interpreter assumed that the function doesn’t work with “Rectangle” types any more and I got an error even tho I’ve never defined the argument type. I had to copy/paste the function into a new CBN yet again.
- You have to be careful with the definition of your variables. They now have a scope and if you don’t define a variable at the right place, it may be inaccessible in a different part of the code.  
~~- The performance of DS function calls has tanked compared to previous versions.~~

It feels to me like this functionality has really deteriorated over time.

---

<div class="post-metadata">

**Author:** ![Michael\_Kirschner2](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/michael_kirschner2/32/75576_2.png) [@Michael\_Kirschner2](https://forum.dynamobim.com/u/Michael_Kirschner2)\
**Post date:** [November 21, 2017, 3:02am UTC](https://forum.dynamobim.com/t/def-using-while/16683/5 "2017-11-21T03:02:15Z")

</div>

@Dimitar_Venkov thanks for finding the issue here with anticipating replication.

we should follow up to figure out how to improve the experience of using imperative blocks - are you finding performance decreases in 2.0 vs 1.3 in imperative blocks?

@Aparajit_Pratap @Zach_Kron FYI.

---

<div class="post-metadata">

**Author:** ![Dimitar\_Venkov](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/dimitar_venkov/32/2068_2.png) [@Dimitar\_Venkov](https://forum.dynamobim.com/u/Dimitar_Venkov)\
**Post date:** [November 21, 2017, 4:30am UTC](https://forum.dynamobim.com/t/def-using-while/16683/6 "2017-11-21T04:30:43Z")

</div>

@Michael_Kirschner2

I take that part back - I ran this by the numbers and the performance of calling custom DS functions in 1.32 and 2.0 has actually increased relative to older versions.

I imagine it has more to do with the fact that all the added tiny issues popping up over time have impacted my overall perceived experience with DS functions negatively - it definitely feels like it takes longer to get them running properly but the actual performance is indeed faster.

This goes to show that you should always measure things first, or else you’ll end up eating your words 🙂

Multiple function calls are still expensive tho and you should avoid them if possible:  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/d/5d97094e99046dd5e9cc17cc6891d137bea5128f.png)

[DS\_Func\_Perf\_1.3.dyn](https://forum.dynamobim.com/uploads/short-url/ezTvjcjT3mST0cklLicUAMMetv3.dyn) (14.2 KB)

---

<div class="post-metadata">

**Author:** ![Mike\_Engel1](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mike_engel1/32/9075_2.png) [@Mike\_Engel1](https://forum.dynamobim.com/u/Mike_Engel1)\
**Post date:** [November 21, 2017, 2:33pm UTC](https://forum.dynamobim.com/t/def-using-while/16683/7 "2017-11-21T14:33:36Z")

</div>

> [@Dimitar\_Venkov](#):
>
> arm yourself with a ton of patience.

Thank you all, information is invaluable.

“arm yourself with a ton of patience.” - this is absolutely true.

Any thoughts on doing this in Python vs Codeblock?

---

<div class="post-metadata">

**Author:** ![Yna\_Db](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/yna_db/32/30641_2.png) [@Yna\_Db](https://forum.dynamobim.com/u/Yna_Db)\
**Post date:** [November 21, 2017, 3:46pm UTC](https://forum.dynamobim.com/t/def-using-while/16683/8 "2017-11-21T15:46:01Z")

</div>

This discussion could help:

> [@Importing Curve methods to Python Script](https://forum.dynamobim.com/t/importing-curve-methods-to-python-script/7212):
>
> Hello all, I’m new in Dynamo and I already searched a lot on internet to find an answer to my question but unsuccessfully… I’m working on a Python Script and I want to use the “Curves” methods (Curve.StartPoint, Curve.Length, etc) that we can find in dynamo nodes. I added all the possible and inimaginable “import …” at the beginning of my Python Script and I can use other Design Script Methods but not the ones I need. Does someone have a solution for that ? Thank you for your time !

---

<div class="post-metadata">

**Author:** ![Aparajit\_Pratap](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Aparajit\_Pratap](https://forum.dynamobim.com/u/Aparajit_Pratap)\
**Post date:** [November 21, 2017, 5:49pm UTC](https://forum.dynamobim.com/t/def-using-while/16683/9 "2017-11-21T17:49:15Z")

</div>

@Mike_Engel1 @Dimitar_Venkov thanks for your findings. We are making changes/improvements to imperative blocks in 2.0 where we are addressing some of these issues. You can find a brief description on language changes here: [http://dynamobim.org/resuming-pre-release-builds/](http://dynamobim.org/resuming-pre-release-builds/). Regarding specific imperative language changes, there are new scoping rules, such that variables defined in an outer scope are readonly within an imperative scope and if modified within the imperative scope, will not affect values in the parent associative scope. Secondly statements like “PolyCurve.Curves(pc);” and “Curve.PointAtParameter(list\_of\_curves, 0.2);” will also work but not “list\_of\_curves.PointAtParameter(0.2);” as @Dimitar_Venkov rightly pointed out. The latter will only work within an “Associative” scope since it involves replication.

---

<div class="post-metadata">

**Author:** ![Mike\_Engel1](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mike_engel1/32/9075_2.png) [@Mike\_Engel1](https://forum.dynamobim.com/u/Mike_Engel1)\
**Post date:** [November 21, 2017, 6:08pm UTC](https://forum.dynamobim.com/t/def-using-while/16683/10 "2017-11-21T18:08:29Z")

</div>

Wonderful - my next thought was to test on 2.0 builds, but looks like you covered it.

---

<div class="post-metadata">

**Author:** ![Yna\_Db](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/yna_db/32/30641_2.png) [@Yna\_Db](https://forum.dynamobim.com/u/Yna_Db)\
**Post date:** [November 22, 2017, 5:00pm UTC](https://forum.dynamobim.com/t/def-using-while/16683/11 "2017-11-22T17:00:41Z")

</div>

Here is a Python version of “recursive”, for anyone interested in a quick comparison with DesignScript 🙂 :

```
import clr

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

Geom = IN[0]
itt = IN[1]
sequence = [Geom]
curvs = []
i = 0

while i < itt:
	curvs = Geom.Curves()
	pts = []
	for crv in curvs:
		pts.append(Curve.PointAtParameter(crv, 0.2))
	Geom = PolyCurve.ByPoints(pts, 1)
	sequence.append(Geom)
	i = i + 1

OUT = (pts, sequence)

```

 ![Recursive-Python-version](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/e/b/eb34cd60d9cbbc3f0432d7ffc26de1f639952c7f.PNG)
