Any Simple Loop Code using Design Script Examples?

Hi All,

Looking for a simple recursive loop code example using Design Script code block that i can send a list of objects though rotating some of them with each loop.

I have worked out how to roate select objects in the list but not return the changed list back to the start for the next round.

I have looked on the forum and for examples but not found any as yet.

I realise it maybe better to do loops in Python but the code has been written using Node to Code so don’t want to re-write it again in Python.

Thanks
Matt

Here is a nice little example from @Dimitar_Venkov that showcases a loop as an example of how to write it.

4 Likes

Thanks this has been a great help today, i have been able to edit it to work with one of code but when i change the process it does not work as expected.

The code on the left works but the code on the right fail to return an output, but both codes work as expected when coded outside the loop.

I need the code logic on the right to work as the move is only a simple example, when i get this to work i can add more complex transformations.

Broken Loop Code

def move_cubes_shift(cubes:var){
return = [Imperative]
{
cnt = List.Count(cubes);
ind = 0…cnt-2…1;
dist = {5,10,15};
for (i in ind)
{
// Portion of Code Changed//
cubes_A = List.TakeItems(cubes,i+1);
cubes_B = List.DropItems(cubes,i+1);
move_cube = cubes_B.Translate(Vector.XAxis(),dist[i]);
cubes = List.Join({cubes_A,move_cube});
// Portion of Code Changed//
}
return = cubes;
}
};

Thanks

In this line:

move_cube = cubes_B.Translate(Vector.XAxis(),dist[i]);

you’re trying to perform associative code inside an imperative block. You have to instead operate on each individual cube in the list like so:

def move_cubes_shift(cubes:var[]){
return = [Imperative]{
dist = 0.1..1..#Count(cubes);
for (i in GetKeys(cubes) )
{
    old = List.TakeItems(cubes,i+1);
    to_move = List.DropItems(cubes,i+1);
    moved = {};
    for (c in to_move)
    {
        c1 = c.Translate(Vector.XAxis(), dist[i]);
        moved = List.AddItemToEnd(c1, moved);
    }
    cubes = Flatten({old, moved});
}
return = cubes;
}};
3 Likes

Ah that makes sense, i can see how one code worked as it used only a single object per loop and the second one failed as i was passing multiple objects though it .

I see you have done some other interesting things in the code too, will look into those too.

Thanks Matt