WEBVTT

1
00:00:00.240 --> 00:00:02.190
Hello and welcome to scripting for artists.

2
00:00:02.200 --> 00:00:05.730
My name is Silicon and today we'll do another roast my add on this time.

3
00:00:05.730 --> 00:00:08.119
It's only about one add on called nature clicker.

4
00:00:08.130 --> 00:00:12.570
I'm changing the format a little bit and I will only be roasting one adult at the time.

5
00:00:12.580 --> 00:00:14.840
This makes videos a little bit shorter and makes

6
00:00:14.840 --> 00:00:16.760
it a little bit less production work for me,

7
00:00:16.770 --> 00:00:18.990
which means that I will be able to keep going for a longer

8
00:00:18.990 --> 00:00:22.560
time because it doesn't eat up so much of my blender development time

9
00:00:22.740 --> 00:00:26.230
on twitter, all the post writes. I love the last video.

10
00:00:26.240 --> 00:00:28.960
Thank you very much older post and as I only

11
00:00:28.960 --> 00:00:31.890
started coding recently I could really use a good roasting.

12
00:00:31.890 --> 00:00:33.360
Well that's all right.

13
00:00:33.360 --> 00:00:37.330
Then I released this free island last week it's replacing a random nature asset.

14
00:00:37.340 --> 00:00:41.360
Every time you click sounds useful let's take a look at the code.

15
00:00:41.440 --> 00:00:43.650
Here we are in good hope there is a nice description

16
00:00:43.650 --> 00:00:46.260
of free blender add on for placing objects with randomization,

17
00:00:46.340 --> 00:00:48.960
you select the asset you wanted place and every time you click during the

18
00:00:48.960 --> 00:00:53.010
model a random object from your selection is placed also has some more features.

19
00:00:53.010 --> 00:00:57.030
See the one minute youtube video. So that's nice. There is a nice video about this.

20
00:00:57.030 --> 00:00:59.660
I will link it in the description basically How it

21
00:00:59.660 --> 00:01:02.090
works is you do a selection you click on,

22
00:01:02.090 --> 00:01:03.890
start clicking and then you click around and then

23
00:01:03.890 --> 00:01:06.350
it places a random asset on the ground plane

24
00:01:06.620 --> 00:01:08.880
in a video. Let's take a look at the code.

25
00:01:09.240 --> 00:01:11.930
It's one file only. So that makes things simpler.

26
00:01:12.010 --> 00:01:14.460
This is really um that's nice but make sure that your

27
00:01:14.460 --> 00:01:17.100
lines are not so long that you can't read them anymore.

28
00:01:17.110 --> 00:01:22.060
And for readability I would always recommend typing a space after a common sign.

29
00:01:22.540 --> 00:01:25.930
Also in english we start our sentences with a capital letter.

30
00:01:25.940 --> 00:01:29.870
Also you ask for if you have any suggestions for the code, please let me know.

31
00:01:29.880 --> 00:01:35.240
Well strictly I can't because there is no contact information in the bill info.

32
00:01:35.250 --> 00:01:36.250
This is a bit weird.

33
00:01:36.260 --> 00:01:40.060
There's a lot of stuff being important but especially vector is imported twice.

34
00:01:42.540 --> 00:01:48.560
Uh there is a panel here um which is hard to read when you zoom in like this.

35
00:01:49.040 --> 00:01:53.660
Then we have functions again hard to read unless you zoomed out a lot.

36
00:01:55.040 --> 00:01:58.960
And then there is classes and

37
00:01:59.740 --> 00:02:00.360
you too,

38
00:02:00.840 --> 00:02:03.930
if you separate one python file into multiple sections.

39
00:02:03.930 --> 00:02:07.610
This usually means that you actually want to have multiple python files.

40
00:02:07.620 --> 00:02:09.300
I know we haven't covered this inscription for

41
00:02:09.300 --> 00:02:11.550
adults so I can't really roasted for that.

42
00:02:11.560 --> 00:02:12.330
However,

43
00:02:12.330 --> 00:02:17.660
I would suggest never to use a name called you tales helpers or that kind of stuff.

44
00:02:17.840 --> 00:02:20.080
I mean what's the difference between UTIL and

45
00:02:20.080 --> 00:02:22.440
a function where the stuff go naming something

46
00:02:22.440 --> 00:02:24.790
utilizes usually an indication that you haven't really

47
00:02:24.790 --> 00:02:27.350
thought about it and just plunked on things together

48
00:02:27.640 --> 00:02:30.310
in this case it's just one class called settings.

49
00:02:30.310 --> 00:02:33.000
So why not name it settings instead after all the

50
00:02:33.000 --> 00:02:36.030
rest is just regular blender stuff for registering and unregistered

51
00:02:36.260 --> 00:02:40.790
and yes I know there is also to be pilot you tails and no I'm not happy with that either.

52
00:02:40.800 --> 00:02:42.710
Let's go back to the top of the final whether

53
00:02:42.710 --> 00:02:45.550
panels already find and the naming is a bit weird.

54
00:02:45.640 --> 00:02:48.520
It's called clicker pT panel where I guess clicker

55
00:02:48.520 --> 00:02:51.460
is the category and then pT center panel type

56
00:02:51.640 --> 00:02:55.850
and then identify his panel again which is a bit double looking at the

57
00:02:55.850 --> 00:02:59.310
other panels there's a scene in front of the name here and there.

58
00:02:59.320 --> 00:03:03.920
So I guess that sounds for clicker but that means that the category all of a sudden is

59
00:03:03.920 --> 00:03:06.390
no longer the category and they still and impaneling

60
00:03:06.390 --> 00:03:09.570
the name so I would say name them clicker.

61
00:03:09.570 --> 00:03:13.560
Pt and then whatever their growth that panel is in this case

62
00:03:13.740 --> 00:03:17.780
start button because it only has to start clicking button you could have here

63
00:03:17.790 --> 00:03:21.970
Petey and the score ground because it has settings for the ground surface etcetera.

64
00:03:21.980 --> 00:03:24.030
One more thing that I notice here is that

65
00:03:24.030 --> 00:03:26.310
the layout of the code is not really consistent.

66
00:03:26.430 --> 00:03:28.190
Sometimes there is space equal space.

67
00:03:28.190 --> 00:03:30.860
Sometimes there is no space in front of the equal sign

68
00:03:31.040 --> 00:03:33.810
so I would recommend setting up your attitude

69
00:03:33.810 --> 00:03:35.870
to use auto formatting when you save it.

70
00:03:35.880 --> 00:03:37.360
I will show what that looks like.

71
00:03:38.140 --> 00:03:42.150
Here we are my editor, visual studio code and to set up format unsafe.

72
00:03:42.150 --> 00:03:47.290
You just press control comma and check this checkbox format unsafe.

73
00:03:47.300 --> 00:03:50.460
Let's take a look. I saved the file, you can watch what it's doing

74
00:03:51.140 --> 00:03:54.810
as you can see it moved around a little bit. It had all kinds of small changes.

75
00:03:54.820 --> 00:03:57.140
There's a space after the comment here,

76
00:03:57.150 --> 00:03:59.770
there is no space in front of this column and also

77
00:03:59.770 --> 00:04:01.940
here the assignments are all done in the same way,

78
00:04:01.950 --> 00:04:02.440
automating.

79
00:04:02.440 --> 00:04:04.710
This means you don't have to think about it and then

80
00:04:04.720 --> 00:04:07.060
it just works and it takes your mind off of it.

81
00:04:07.440 --> 00:04:09.960
Clean code is always good because you definitely

82
00:04:09.960 --> 00:04:12.150
read code more often than you write it.

83
00:04:12.150 --> 00:04:15.910
So taking care of that you called is clean and nice to read is very,

84
00:04:15.910 --> 00:04:18.860
very important for developers on any level.

85
00:04:19.440 --> 00:04:21.100
Now that we're my editor anyway,

86
00:04:21.100 --> 00:04:24.700
let's take a look at an improvement that I would definitely do here.

87
00:04:24.710 --> 00:04:27.880
As you can see there's a lot of repetition in this code.

88
00:04:27.940 --> 00:04:32.870
This part is repeated here as well and in the next panel and in the next panel

89
00:04:32.880 --> 00:04:36.890
to put all this code into one place and then use it in all the other panels.

90
00:04:36.890 --> 00:04:40.760
We can create what is called a mixing class. I will show you what that looks like.

91
00:04:43.340 --> 00:04:45.650
So I just create a class called clicker panel

92
00:04:45.650 --> 00:04:47.670
and that will have all the common information.

93
00:04:50.140 --> 00:04:50.340
Yeah.

94
00:04:51.140 --> 00:04:53.940
And this we can then use as a mixing class.

95
00:04:54.840 --> 00:04:55.160
Yeah.

96
00:04:56.040 --> 00:05:00.270
So what happens here is that if blender tries to find the bill category of clicker.

97
00:05:00.270 --> 00:05:03.660
Pt panel, it won't be able to find it on the panel itself.

98
00:05:03.670 --> 00:05:07.400
And python will then start asking the parent in order so it will

99
00:05:07.400 --> 00:05:10.950
go to clicker panel and it will ask their if it knows bill category

100
00:05:11.340 --> 00:05:12.470
in this case it does.

101
00:05:12.470 --> 00:05:17.160
So there the search ends and then it finds clicker as the value for that attribute.

102
00:05:17.260 --> 00:05:19.260
We can do this on every panel now.

103
00:05:20.740 --> 00:05:20.940
Yeah,

104
00:05:22.440 --> 00:05:22.650
Okay.

105
00:05:23.240 --> 00:05:24.560
And remove those,

106
00:05:25.540 --> 00:05:27.360
put the mixing class there

107
00:05:27.940 --> 00:05:28.960
on those

108
00:05:29.540 --> 00:05:30.110
mixing

109
00:05:30.300 --> 00:05:31.250
and there we are.

110
00:05:31.740 --> 00:05:34.210
So this has reduced quite a bit of code already.

111
00:05:34.220 --> 00:05:36.800
And if we ever want to change that category type,

112
00:05:36.810 --> 00:05:38.830
there's no one place where that is defined.

113
00:05:38.840 --> 00:05:40.770
This is another aspect of clean code.

114
00:05:40.780 --> 00:05:44.620
There should only be one bit of code responsible for one thing.

115
00:05:44.630 --> 00:05:47.310
So enough about the panels. Let's move through the functions.

116
00:05:47.320 --> 00:05:49.970
There's a function add to collection, object to move,

117
00:05:49.980 --> 00:05:52.690
which is already weird because is it added to

118
00:05:52.690 --> 00:05:54.990
a collection or is it moved to a collection?

119
00:05:54.990 --> 00:05:58.460
The function names as one thing. The parameter names has a different thing.

120
00:05:58.640 --> 00:06:01.430
That's a bit weird, checks if the clicker collection.

121
00:06:01.430 --> 00:06:01.900
Well,

122
00:06:01.910 --> 00:06:05.050
capitals checks if the clicker collection exists

123
00:06:05.060 --> 00:06:07.130
and moves the object to this collection.

124
00:06:07.140 --> 00:06:09.400
So this is doing two things,

125
00:06:09.400 --> 00:06:13.300
which is generally not a good idea for a function only when the code.

126
00:06:13.300 --> 00:06:13.630
When you

127
00:06:13.740 --> 00:06:16.280
check the checkbox FB part contact saying well,

128
00:06:16.290 --> 00:06:18.450
I have to stop you there never used to be

129
00:06:18.450 --> 00:06:21.170
part of context unless you really really have to.

130
00:06:21.180 --> 00:06:23.620
I'm guessing that this function is going to be called by

131
00:06:23.620 --> 00:06:27.210
an operator and that operator gets a context from blender.

132
00:06:27.280 --> 00:06:29.900
All the subsequent code should be using that

133
00:06:29.900 --> 00:06:32.730
context instead of using the global one here.

134
00:06:32.730 --> 00:06:35.250
I'll show you an example as to why that is

135
00:06:35.530 --> 00:06:38.520
every operator can have its own context and that doesn't

136
00:06:38.520 --> 00:06:40.610
have to be the same as the global context.

137
00:06:40.620 --> 00:06:41.640
For example,

138
00:06:41.640 --> 00:06:45.190
there's this pin here that allows me to in this object

139
00:06:45.190 --> 00:06:49.180
properties tab pin the tab to Suzanne the current active object.

140
00:06:49.190 --> 00:06:51.720
Now if I change the active object to the cube,

141
00:06:51.730 --> 00:06:54.260
you can see that this panel still show Suzanne.

142
00:06:54.290 --> 00:06:58.880
This is also the difference between context object and context of active object.

143
00:06:58.890 --> 00:07:02.200
This button here is an operator and for this operator

144
00:07:02.220 --> 00:07:05.140
context of object will be Suzanne the pinned object.

145
00:07:05.150 --> 00:07:08.060
Whereas for an operator that is called from a menu here,

146
00:07:08.140 --> 00:07:09.680
it doesn't have that object pin.

147
00:07:09.680 --> 00:07:14.540
So their context object is actually the cube. Note that in both cases contact.

148
00:07:14.550 --> 00:07:17.890
Active object is still the active object which is a cube.

149
00:07:17.900 --> 00:07:19.580
This is one of the ways in which an operator

150
00:07:19.580 --> 00:07:22.260
can get a context that's different from the global context.

151
00:07:22.270 --> 00:07:26.160
So just use the context that you're given and don't always go to the global one.

152
00:07:26.340 --> 00:07:28.360
There's more in this particular line

153
00:07:28.540 --> 00:07:31.490
comparison to True in an if statement is never necessary

154
00:07:31.490 --> 00:07:34.060
because the if statement already does a comparison to True.

155
00:07:34.070 --> 00:07:36.660
So you're just effectively doubling its work.

156
00:07:37.140 --> 00:07:40.530
The commons has only run the code when the U. S. Has checked the checkbox.

157
00:07:40.530 --> 00:07:44.360
And that means that if it doesn't return here there must be an else right?

158
00:07:45.340 --> 00:07:45.850
Oh

159
00:07:46.340 --> 00:07:47.560
no there is not

160
00:07:48.440 --> 00:07:49.250
return early,

161
00:07:49.640 --> 00:07:52.900
flip your conditions return early if not.

162
00:07:53.000 --> 00:07:54.950
Yeah. Yeah yada does click collection

163
00:07:55.340 --> 00:07:56.120
Return.

164
00:07:56.310 --> 00:07:59.860
That means that all of this code can be un indented one step

165
00:08:00.240 --> 00:08:01.990
and that means you free up your brain.

166
00:08:01.990 --> 00:08:04.750
You don't have to look at whether there is an else at the end of

167
00:08:04.760 --> 00:08:08.900
the function or whether it doesn't do anything when that check box isn't checked.

168
00:08:08.970 --> 00:08:12.830
Freeing up your brain I think is the most important part of coding.

169
00:08:13.040 --> 00:08:14.370
Stop confusing yourself.

170
00:08:14.370 --> 00:08:17.690
The function name says one thing, the parameter, the name says another thing.

171
00:08:18.000 --> 00:08:23.260
The comment here says a different thing and then there is an if that if you want to

172
00:08:23.260 --> 00:08:27.360
understand it you have to scroll all the way to the bottom to see that there is no else

173
00:08:27.640 --> 00:08:30.060
now in the next line you set collection named to the name

174
00:08:30.060 --> 00:08:32.710
of the collection that will be created if it doesn't exist yet.

175
00:08:32.720 --> 00:08:35.580
I think it's a good idea to put this name into a variable.

176
00:08:35.580 --> 00:08:37.480
But this is not the place to do that.

177
00:08:37.490 --> 00:08:40.980
This function moves an object into the collection so I wouldn't expect

178
00:08:40.980 --> 00:08:45.750
this function to be the responsible bit of code that names that collection

179
00:08:45.790 --> 00:08:49.030
just move it to a global variable next is to try except

180
00:08:49.040 --> 00:08:52.460
to get the collection if it doesn't exist yet and created otherwise,

181
00:08:52.630 --> 00:08:53.420
that's a big no,

182
00:08:53.420 --> 00:08:56.400
never use a bare except just catch whatever exception you

183
00:08:56.400 --> 00:08:58.450
want to catch in this case it's a key error

184
00:08:58.840 --> 00:08:59.930
without any exception.

185
00:08:59.930 --> 00:09:01.090
Behind the except class,

186
00:09:01.090 --> 00:09:04.040
you will catch each and every exception and that

187
00:09:04.040 --> 00:09:06.500
is not only from a technical point of view.

188
00:09:06.500 --> 00:09:10.040
The wrong thing to do here giving the actual name of the exception that

189
00:09:10.040 --> 00:09:14.090
you expect her again frees up your brain when you later read it.

190
00:09:14.090 --> 00:09:17.860
You know exactly what you were thinking at the time, whichever you were expecting

191
00:09:18.240 --> 00:09:19.830
then on with the next bit of code.

192
00:09:19.830 --> 00:09:23.260
This is a loop that removes the object from all the collections that it's in

193
00:09:23.270 --> 00:09:27.530
and then added to one specific collection namely the one that was just created.

194
00:09:27.540 --> 00:09:33.420
However, it's done in a very indirect weird way O b J is assigned object to move.

195
00:09:33.430 --> 00:09:35.880
Um So I guess your lines were getting too long.

196
00:09:35.890 --> 00:09:37.560
There's a different way of handling that.

197
00:09:37.840 --> 00:09:41.000
Then old collections is an empty list that is never used again.

198
00:09:41.140 --> 00:09:44.300
And then with a for loop that loops over object users

199
00:09:44.300 --> 00:09:48.220
collection while it's also manipulating the collection of the objects in.

200
00:09:48.220 --> 00:09:51.820
So you're in the loop you're manipulating whatever you're looking over,

201
00:09:51.820 --> 00:09:53.560
which is also a bad idea.

202
00:09:54.040 --> 00:09:56.460
So let's take a look at how we can improve this.

203
00:09:57.340 --> 00:10:00.360
Let's very quickly go over this code, See what we can improve.

204
00:10:00.840 --> 00:10:03.240
I start with the inside then move outward.

205
00:10:03.250 --> 00:10:06.940
Be part data collections collection dot name is very indirect

206
00:10:06.940 --> 00:10:10.170
way to get the collection collection is already the collection.

207
00:10:10.180 --> 00:10:15.690
So to get its name and then do a look up my name to get the collection is a bit weird.

208
00:10:15.700 --> 00:10:19.050
Also it may not work the way you think it will work

209
00:10:19.640 --> 00:10:20.180
also.

210
00:10:20.180 --> 00:10:25.540
Now instead of having to do that except and then be part contact seen collections.

211
00:10:25.540 --> 00:10:29.670
If it's maybe in there we can all remove it because we already have the collection.

212
00:10:29.670 --> 00:10:31.590
So there is nothing to fail.

213
00:10:31.600 --> 00:10:35.120
The second issue that we're looping over the collections

214
00:10:35.120 --> 00:10:37.360
and changing the collections at the same time.

215
00:10:37.440 --> 00:10:39.750
You can just create a copy of this list.

216
00:10:39.940 --> 00:10:42.890
So you do it like this in this notation.

217
00:10:42.890 --> 00:10:47.690
I could do something like from two up to but not including index number five.

218
00:10:47.700 --> 00:10:51.440
You can remove the first index to indicate from the start of the list and

219
00:10:51.440 --> 00:10:54.450
you can remove the last index to say up to the end of the list.

220
00:10:54.460 --> 00:10:56.990
So this takes a copy of the entire list.

221
00:10:57.000 --> 00:10:59.220
So now that we ever save copy we can remove

222
00:10:59.230 --> 00:11:01.280
this and now that our lines are much shorter.

223
00:11:01.280 --> 00:11:04.350
We can just use object to move again.

224
00:11:04.350 --> 00:11:08.250
This we have this weird thing of taking the collection name

225
00:11:08.340 --> 00:11:11.930
and then looking up the collection that belongs to that name.

226
00:11:11.940 --> 00:11:14.560
Whereas we already have the collection here.

227
00:11:15.440 --> 00:11:18.180
So there's a problem now that we get the collection here,

228
00:11:18.180 --> 00:11:20.100
then we use the collection name again.

229
00:11:20.110 --> 00:11:23.590
And then of course we no longer have the original

230
00:11:23.590 --> 00:11:26.260
collection because today the variables are named the same.

231
00:11:26.270 --> 00:11:29.260
So what we can do here is for example, name it

232
00:11:29.640 --> 00:11:33.160
click or call to indicate that this is the clicker collection,

233
00:11:33.940 --> 00:11:36.200
do that here as well, do that here as well.

234
00:11:36.200 --> 00:11:38.790
And then instead of this you can just say clicker call

235
00:11:38.790 --> 00:11:42.400
the objects dot link and this is much simpler personally,

236
00:11:42.400 --> 00:11:48.630
I would go one step further and move this and that code into two separate functions.

237
00:11:48.640 --> 00:11:50.370
The first bit of code just gets

238
00:11:50.370 --> 00:11:53.650
that particular collection ensuring that its existing

239
00:11:53.840 --> 00:11:57.290
this bit of code unlinked the object from all the collections that it's in.

240
00:11:57.300 --> 00:12:00.560
So that is completely separate from the bit of code above.

241
00:12:00.720 --> 00:12:03.220
And that means that it could be in a different function.

242
00:12:03.230 --> 00:12:07.120
The next function is called choose object and gets self as a parameter.

243
00:12:07.150 --> 00:12:10.110
I'm very skeptical about this because this basically

244
00:12:10.110 --> 00:12:12.810
means that it only gets self and it

245
00:12:12.810 --> 00:12:17.660
is very likely that this should be part of the class that self is pointing to.

246
00:12:18.640 --> 00:12:20.350
So let's see where it is used.

247
00:12:21.040 --> 00:12:21.960
Yes,

248
00:12:22.440 --> 00:12:22.990
yes,

249
00:12:22.990 --> 00:12:26.390
this is the click operators model function which

250
00:12:26.400 --> 00:12:30.370
is huge and it caused choose objects and victimization

251
00:12:30.470 --> 00:12:35.470
and passing self. This means that it really should be part of the operator.

252
00:12:35.470 --> 00:12:40.460
So just invented by one level blanket here inside the class

253
00:12:40.640 --> 00:12:44.360
and you can call self dot choose object and selfhood randomization

254
00:12:44.540 --> 00:12:47.060
and then it's much clearer what that code belongs to.

255
00:12:47.640 --> 00:12:50.660
Apart from what I said already, I think this code is fine.

256
00:12:50.670 --> 00:12:55.160
Um The randomization function is a bit weird again. Um

257
00:12:55.740 --> 00:12:57.690
Get a random positive or negative.

258
00:12:57.700 --> 00:13:01.930
Well you already show that you know random choice and this is basically random.

259
00:13:01.930 --> 00:13:04.560
The choice one or -1.

260
00:13:05.340 --> 00:13:05.960
But

261
00:13:06.840 --> 00:13:11.160
this formula and then where it's all used is a bit weird.

262
00:13:11.540 --> 00:13:16.120
Also I would avoid repeating, beep eye contact, seen clicker all the time.

263
00:13:16.120 --> 00:13:19.070
Just right clicker is beep eye contact seen clicker or even better.

264
00:13:19.070 --> 00:13:23.450
The context that you passed to this function don't seem not clicker. Um

265
00:13:23.940 --> 00:13:28.360
But apart from that you take a random then turn that into a plus or minus.

266
00:13:28.360 --> 00:13:32.590
But then here you have a random integer between zero and 10.

267
00:13:32.600 --> 00:13:38.660
You divide that by 10 so you have a zero somewhere between zero and one

268
00:13:39.140 --> 00:13:44.940
but only in steps of 1/10. And then you multiply it with positive or negative.

269
00:13:44.950 --> 00:13:47.090
But then also you add one.

270
00:13:47.100 --> 00:13:53.360
So now all of a sudden is one plus minus one times something random.

271
00:13:53.440 --> 00:13:56.130
It becomes very dodgy about what you exactly need.

272
00:13:56.130 --> 00:14:00.400
I think what would be more appropriate is to use random black uniform and then just

273
00:14:00.400 --> 00:14:03.770
give a minimum and a maximum value of the random number that you want to get.

274
00:14:03.770 --> 00:14:07.350
For example random uniform minus one comma one will

275
00:14:07.350 --> 00:14:09.360
just give you a random number in that range

276
00:14:09.740 --> 00:14:14.620
here you're using operators for resizing and for rotating, I would never do that.

277
00:14:14.620 --> 00:14:19.800
Just multiply the scale or multiply the rotation with the given values

278
00:14:19.800 --> 00:14:23.660
and that is much easier to read is also faster to execute.

279
00:14:24.440 --> 00:14:26.580
The last thing that I find a bit strange

280
00:14:26.590 --> 00:14:30.310
is that it operates on the selected object singular.

281
00:14:30.320 --> 00:14:35.840
If I remember your video correctly, every time you click that object is randomized.

282
00:14:35.850 --> 00:14:39.050
So this actually randomizes a very specific object.

283
00:14:39.050 --> 00:14:41.360
So past that object to this function

284
00:14:41.540 --> 00:14:44.260
and then manipulate its dot scaled or location.

285
00:14:44.260 --> 00:14:45.090
That rotation,

286
00:14:45.090 --> 00:14:47.220
whatever you want to manipulate that from

287
00:14:47.220 --> 00:14:49.240
a coding standpoint also makes it easier because

288
00:14:49.240 --> 00:14:51.390
it's clear that this function operates on the

289
00:14:51.390 --> 00:14:53.450
object that it receives from the color.

290
00:14:54.140 --> 00:14:56.920
Let's take a quick look at that clicker operator because that

291
00:14:56.920 --> 00:14:59.630
is basically the core of the add on for one.

292
00:14:59.630 --> 00:15:01.230
That model function is way too long.

293
00:15:01.240 --> 00:15:04.400
I think all the post agrees with me because there is some documentation here

294
00:15:04.400 --> 00:15:07.270
in the same style that you would find at the top of a function

295
00:15:07.380 --> 00:15:09.350
except that here is not in a function.

296
00:15:09.470 --> 00:15:12.260
So put it in a function, split up your code

297
00:15:12.440 --> 00:15:16.060
because it makes everything so much easier to understand and

298
00:15:16.240 --> 00:15:16.460
yeah,

299
00:15:17.040 --> 00:15:20.660
you otherwise you get this kind of stuff. Look if plane

300
00:15:21.240 --> 00:15:21.960
if it

301
00:15:22.540 --> 00:15:24.360
if clicker line is true.

302
00:15:24.840 --> 00:15:25.460
So

303
00:15:26.240 --> 00:15:30.370
when is this executed exactly putting the code into a separate function.

304
00:15:30.370 --> 00:15:32.970
Not only makes it easier to understand,

305
00:15:32.980 --> 00:15:36.110
but also it forces you to give a name to that function,

306
00:15:36.110 --> 00:15:39.240
it forces you to write a little bit of documentation for that function.

307
00:15:39.250 --> 00:15:41.450
Well, it doesn't force you but he should anyway.

308
00:15:41.740 --> 00:15:44.510
Um and as soon as you start doing something in

309
00:15:44.510 --> 00:15:47.050
that function that doesn't really adhere to that name.

310
00:15:47.270 --> 00:15:49.980
That is an indication that maybe you're doing the wrong thing there.

311
00:15:49.980 --> 00:15:52.240
Maybe that should be in a separate bit of code

312
00:15:52.550 --> 00:15:55.250
and it really helps you streamline your thoughts as well.

313
00:15:55.260 --> 00:15:59.360
For example this you could put into a function called a line

314
00:15:59.360 --> 00:16:03.490
with ground plane or place on ground plane or whatever it's doing.

315
00:16:03.570 --> 00:16:07.450
Of course you could have if not plain return in there immediately

316
00:16:07.640 --> 00:16:12.760
and then everything else will be an invented. And you get my point

317
00:16:13.340 --> 00:16:14.320
in the next bit of quote,

318
00:16:14.320 --> 00:16:18.460
we have the handling of the enter function which picks another random object.

319
00:16:18.470 --> 00:16:20.340
This jumps through a bunch of hoops.

320
00:16:20.350 --> 00:16:23.360
Most importantly here to try and make sure

321
00:16:23.360 --> 00:16:26.090
that it actually picks a different random object.

322
00:16:26.100 --> 00:16:30.410
So what's happening here is that the code tries to remove any subjects?

323
00:16:30.410 --> 00:16:32.660
I thought there was 01 dot too early or too

324
00:16:33.040 --> 00:16:35.560
tries to remove that from the name and then compare it

325
00:16:35.560 --> 00:16:38.250
to the newly selected objects to see if they differ.

326
00:16:38.260 --> 00:16:44.240
Well, but what if I already selected objects that were named 3.1 etcetera.

327
00:16:44.250 --> 00:16:46.530
Then all of a sudden this goes wrong.

328
00:16:46.540 --> 00:16:51.850
Rather what I would do here is not use random choice to pick random object

329
00:16:52.140 --> 00:16:56.300
but random dot rent range to select a random object index.

330
00:16:56.310 --> 00:16:59.150
Then if you remember the last index that you used

331
00:16:59.160 --> 00:17:01.440
you can just compare the industries with each other and

332
00:17:01.440 --> 00:17:04.010
then just keep selecting a random index until you have

333
00:17:04.010 --> 00:17:06.460
one that's different from the previously used to index.

334
00:17:06.470 --> 00:17:07.470
That saves you from a lot of

335
00:17:07.470 --> 00:17:10.460
headaches of comparing strings and manipulating strings

336
00:17:10.460 --> 00:17:12.849
to make the comparison work because that

337
00:17:12.849 --> 00:17:15.270
conceptually matches what you wanted to do.

338
00:17:15.740 --> 00:17:19.410
Then we have the invoke function if context area type of few three

339
00:17:19.410 --> 00:17:23.450
D do something and then all the way at the bottom there CLS

340
00:17:24.440 --> 00:17:26.800
just flip the condition, flip your conditions,

341
00:17:26.810 --> 00:17:29.810
move all of this and that all the way to the top.

342
00:17:29.810 --> 00:17:33.560
And then you can un invent the rest of the code. Also here be part of why?

343
00:17:33.560 --> 00:17:36.770
Why are you using the part of context when you get a context here,

344
00:17:37.440 --> 00:17:40.200
just right context selected objects it's the same thing

345
00:17:40.200 --> 00:17:42.590
but then it's correct and it's less typing.

346
00:17:42.600 --> 00:17:46.870
Now look at the structure of the rest of the code we have and if we have another if we

347
00:17:46.870 --> 00:17:49.350
have another block that test some things and then here

348
00:17:49.350 --> 00:17:52.050
again we have a block that tests some things.

349
00:17:52.060 --> 00:17:58.940
So just flip your like just flip your conditions if context area type is not feels

350
00:17:58.940 --> 00:18:06.770
really know if no if not be part selected object return if this goes wrong return if

351
00:18:07.140 --> 00:18:12.860
len selected objects is one and the object is a clear surface return

352
00:18:12.870 --> 00:18:16.650
that can all be put at the same level underneath each other.

353
00:18:16.660 --> 00:18:20.930
Right now what you have is a code that grows in three different directions.

354
00:18:20.940 --> 00:18:24.860
Let me show you what I mean with that let's say you have some function

355
00:18:25.040 --> 00:18:27.770
that takes some parameters

356
00:18:28.040 --> 00:18:32.360
and then it does its main thing like the thing it has to do,

357
00:18:33.140 --> 00:18:35.470
let's call it a thing with A B M. C.

358
00:18:35.480 --> 00:18:38.120
Like the way this code was made that we were just looking at

359
00:18:38.130 --> 00:18:40.840
if we want to have some error checking on A and B.

360
00:18:40.840 --> 00:18:43.780
N. C. Watch what happens with the thing.

361
00:18:43.790 --> 00:18:49.200
So if uh A is okay for a then the thing moves over.

362
00:18:49.200 --> 00:18:51.670
So now already we have some code above it.

363
00:18:51.840 --> 00:18:53.670
It moved to the right

364
00:18:53.840 --> 00:18:54.770
and then

365
00:18:55.440 --> 00:19:00.620
and or error A. So now what we get is we have some code above the thing.

366
00:19:00.630 --> 00:19:05.540
We have some code below it. And the main code actually invented one level.

367
00:19:05.550 --> 00:19:08.880
Now if you add another check on B then again we

368
00:19:08.880 --> 00:19:11.510
have to move the thing that way again we have some error

369
00:19:11.510 --> 00:19:15.200
handling code here and if you want to do this then

370
00:19:15.210 --> 00:19:19.050
again it keeps moving in three directions at the same time.

371
00:19:19.340 --> 00:19:22.470
Whereas if you flip your conditions when you get is this

372
00:19:22.790 --> 00:19:29.210
if not A. Is okay handle ever a return and now I can just copy paste this

373
00:19:30.420 --> 00:19:33.050
I say if not be is okay handle ever be

374
00:19:33.740 --> 00:19:39.710
and return and if if not she is okay for foresee and you'll ever see

375
00:19:39.870 --> 00:19:42.670
and where does the thing go now? Well

376
00:19:43.040 --> 00:19:47.590
the thing is still where it was like every error check just pushed

377
00:19:47.590 --> 00:19:51.610
it down but you could only grows in one direction instead of three.

378
00:19:51.620 --> 00:19:54.490
So this is it for this episode of scripting for artist,

379
00:19:54.500 --> 00:19:56.360
my attempt of keeping this video but sure to

380
00:19:56.360 --> 00:19:59.640
have failed but I think it was interesting nonetheless.

381
00:19:59.650 --> 00:20:01.570
I hope that you picked up some techniques on how

382
00:20:01.570 --> 00:20:04.200
to improve your code so that you can free your brain

383
00:20:04.200 --> 00:20:07.160
from frying too much if you have any questions or comments

384
00:20:07.160 --> 00:20:09.460
and leave them below and I will see you soon.


