Hello. Welcome to scripting for artists.
My name is civilian and today we will look at four and wild loops on twitter.
Roger asked, I see you have four loops. When is it better to use wild 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 oops, how did it work?
When what's the difference between them? Um We've seen four looks before.
So we're going to take another look at wild 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 the four and a wild lope.
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 looks already so they
look like four thing and a bunch of things.
Yeah.
And a Wild Hope is slightly different. It's simpler and it looks a bit like an F.
Mhm.
The for loop. Iterate over a bunch of things.
And iterating basically means asking next place
It works well,
we have a sequence of things like the selected objects or the numbers 0-600,
like we saw in the grid of monkeys.
The wild loop is more primitive. Instead of asking next please.
And expecting python to understand and provide the next thing
it asks should 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 F.
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 wild up.
Um let's say you want to simplify the mesh until it has 10,000 vortices or more.
So this would be one way to do it. We can first start looking at the mesh
which has given us to us but be pie context
active object dot data.
And mesh has virtus is
and we can inspect the length of that to get the number of vortices.
So in this case Suzanne has 507. Virtus is
to simplify it.
We need to go to edit mode but object of data
is only updated after we go out of edit out again.
So this means our code has to do the following.
First
you 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 land messed up Virtus is we can see that
it has increased because we edit data has been flashed back to the actual mesh.
So let's put this in a script.
Let's get rid of this.
Start with empathy, be pie and start by getting the mesh
right?
And then
what I was doing.
Well not always but what I sometimes do when I'm not too sure about my wild hope
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.
Um So instead of while right? If
So if the number of virtus is 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 a low until we need to stop.
So
we need to go to edit mode. So however their control C.
And controversy.
Then once in edit mode we need to select everything just to be sure.
Hover and control C. Then here control V.
Then
do the subdivision from our control say their control V.
And then we go back to
object mode
couple.
And this should work famous last words. So we start with the object in object mode.
And there is 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 purchases has gone over 10,000
and it will stop subdividing.
And this tells us that
once we change the F to a while,
okay
it will also stop
as you can see the coat doesn't do anything. Um And that means
this committee condition is checked first
so 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 is a check if the check is true,
there's another loop situation and so on.
So let's throw out this Suzanne and
At a new one
now click ones 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 um 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 may be 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 debs plus equals one.
And what this means is that after this application is done,
one is attitudes objectives
and we keep looping
so far. We on account we don't respond to it yet.
The look doesn't break when subjects get too big.
So it needs to keep looping while the number of Virtus is is still too small.
Of course.
And
well
we'll make this a little bit smaller while sub debs is less than three.
So now it will only subdivide three times for subject 01 and two.
All of those times substantive is smaller than three
and once we throw the third subjects will be three which means the look for 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 one creating the monkey grid.
So instead of keeping track of the number of subdivisions ourselves.
We can say for sub gives in range three.
Yeah
and this will only do it three times.
So the only thing we need to do now is stop early
if the number of Virtus is is big enough.
So instead of saying while the number of verses is smaller than a certain number,
we say
if the number of purchases is bigger than or equal to that number.
So we flipped the condition
we break out of the loop.
And break means just stop right now with that
lobe don't execute anything else in the body,
break out of it.
And then we can remove this, we can remove this
and here we have a code again.
So we replaced a while with before we moved the condition from the while into an F.
And the four. 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 Suzanne
and we can't reuse this mesh because that's still refers to the old one.
So let's say I lend context object data hub
Marty says 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 purchases 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 versus is in three subdivisions.
So we should end up with something less.
Mm
And there we are exactly the same result. Again,
let's increase the limit of the number of subdivisions.
Mhm.
Start with a fresh reason
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 then 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.