 Hello, welcome to Scripting for Artists. My name is Sibirin and today we will look at for and while loops. On Twitter Roger asked, I see you have four loops, when is it better to use while loops instead? I think it's a very good question, so let's take a look at that. So in this video first we will look at four loops and while loops, how did it work, when what's the difference between them. We've seen four loops before, so we're going to take another look at while loops to see several reasons why you would use them and I will go over a few more ways to control your loops. Because the question is a bit more abstract, we will start in a more abstract way. In a few minutes we'll look at a concrete example that combines both a for and a while loop, but first let's take a look at both of them side by side and see how they differ. We've seen a bunch of four loops already, so they look like four thing and bunch of things and a while loop is slightly different, it's simpler and it looks a bit like an if. The for loop iterates over a bunch of things and iterating basically means asking next please. It works well when you have a sequence of things like the selected objects or the numbers 0 to 600 like we saw in the grid of monkeys. The while loop is more primitive instead of asking next please and expecting Python to understand and provide the next thing. It asks should I keep looping and this is very useful when you want to keep repeating until you are at a certain point. Another way to think of it as I mentioned already, it looks like an if and it's basically a repeating if statement. So you've already seen plenty of examples of four loops, so let's look at a while loop. Let's say you want to simplify the mesh until it has 10,000 vertices or more. So this will be one way to do it. We can first start looking at the mesh, which is given us to as what bpi context active object dot data and mesh has vertices and we can inspect the length of that to get the number of vertices. So in this case, Suzanne has 507 vertices. To simplify it we need to go to edit mode but object dot data is only updated after we go out of edit mode again. So this means our code has to do the following. First, we go to edit mode, then everything is selected now but just to be sure we select everything, then we subdivide and then we go back to object mode. And now when we look at length mesh dot vertices, we can see that it has increased because the edit data has been flushed back to the actual mesh. So let's put this in a script. Let's get rid of this. Start with import bpi and start by getting the mesh. And then what I always do, well not always but what I sometimes do when I'm not too sure about my while loop because there is a risk that you get an infinite loop. If you say well true, well true is always true and code here will just keep on repeating forever. That's not so nice because it will actually hang blender. So instead of while, write if. So if the number of vertices is less than 10,000, we need to do a subdivision. And for now we can just test one subdivision. And if that works, we can change the f to a while and have it loop until we need to stop. So we need to go to edit mode. So hover there, Ctrl C and Ctrl V. Then once in edit mode, we need to select everything just to be sure. Hover and Ctrl C. Then here Ctrl V. Then do the subdivision. Hover, Ctrl C, there, Ctrl V. And then we go back to object mode. And this should work. Famous last word. So we start with the object in object mode. And there you see it has done one level of subdivision and we can keep going. And now you can see that the code no longer works. And why is that? Well, the number of vertices has gone over 10,000 and it will stop subdividing. And this tells us that once we change the f to a while, it will also stop. As you can see, the code doesn't do anything. And that means this condition is checked first. So it's at the top of the loop. So it makes sense that is also checked first. So first is the check. If the check is true, then there's a loop iteration. Then again, there's a check. If the check is true, there's another loop iteration and so on. So let's throw out this Suzanne and add a new one. I will click once and immediately we end up with the final subdivision level. So this is how you can use while to loop until some condition is met. So I already mentioned that it's possible to create endless loops. And I will show you a way to control the number of iterations as a sort of safety net. And this may not be useful for your situation and maybe overkill. But it's a nice tool to have in your toolbox anyway. So what we want to limit is the number of subdivisions. So just let's keep count of them. We start counting at zero. We keep the code as it is for now. And then here at the end, we say sub divs plus equals one. And what this means is that after the subdivision is done, one is added to sub divs, and we keep looping. So far we only count, we don't respond to it yet. The loop doesn't break when sub divs get too big. So it needs to keep looping while the number of vertices is still too small, of course. And while we'll make this a little bit smaller, while sub divs is less than three. So now it will only subdivide three times. For sub divs is zero, one and two. All of those times sub divs is smaller than three. And once we've done the third, sub divs will be three, which means the loop will break. So now that we have a situation where we're counting things, we can actually rewrite this to a for loop. And much the same way we did when creating the monkey grid. So instead of keeping track of the number of subdivisions ourselves, we can say for sub divs in range three. And this will only do it three times. So the only thing we need to do now is stop early if the number of vertices is big enough. So instead of saying while the number of vertices is smaller than a certain number, we say if the number of vertices is bigger than or equal to that number. So we flip the condition, we break out of the loop. And break means just stop right now with that loop, don't execute anything else in that body, break out of it. And then we can remove this, we can remove this. And here we have our code again. So we replaced a while with a for we move the condition from the while into an if. And the for make sure that this only happens three times. So let's take a look at what that looks like. Let's start with a fresh user. And we can't reuse this mesh, because that's still refers to the old one. So let's say Len contacts dot object dot data dot vertices 507. And we execute this code now. And now we can see that the result is exactly the same, which are kind of expected, because it reached the limit of the number of vertices before it reached the limit of the number of subdivisions it was allowed to do. So let's try with a fresh Suzanne and set this limit to a million. It probably won't reach a million vertices in three subdivisions. So we should end up with something less. And there we are exactly the same result again. Let's increase the limit of the number of subdivisions. Start with a fresh Suzanne. And it has done one subdivision more, but it did not reach that million, because we had a limit on the number of subdivisions it was allowed to do. So this is another way you can rewrite your wine loop into a for loop and an if. And then you have a limit on the number of iterations that it does. So this is it for this episode of scripting for artists. If you have any questions or remarks, please leave a comment below and I will see you soon.