WEBVTT

1
00:00:00.440 --> 00:00:02.620
Hello and welcome to scripting for artist.

2
00:00:02.630 --> 00:00:06.860
My name is civilian and in this episode we will be looking at linking of assets

3
00:00:07.240 --> 00:00:10.920
because there is a lot more code in this episode than in previous ones.

4
00:00:10.920 --> 00:00:13.140
And the code is getting complexity as well.

5
00:00:13.150 --> 00:00:16.860
I've put the final add on in a link in the video description so you

6
00:00:16.860 --> 00:00:20.650
can download that and use that as a reference while you're watching this video.

7
00:00:20.940 --> 00:00:24.190
Now we'll go to blend a cloud to take a look at the settlers project.

8
00:00:24.190 --> 00:00:27.830
It's a public project so you don't need to blend a cloud subscription to access this

9
00:00:27.840 --> 00:00:31.860
and it will serve as a nice example for this episode of scripting for artists.

10
00:00:32.240 --> 00:00:35.360
I've also put the link to the project in the video description

11
00:00:35.840 --> 00:00:38.270
here is the animation of blender cloud of the settlers

12
00:00:38.270 --> 00:00:42.360
project as is typical for such animation projects it's seen

13
00:00:42.360 --> 00:00:45.550
has set and each set consists of assets in this

14
00:00:45.550 --> 00:00:48.890
case like rocks and plants here on the left.

15
00:00:48.890 --> 00:00:51.730
You can see in the set development different blend files

16
00:00:51.730 --> 00:00:54.460
for the rock assets and for the correct I assets.

17
00:00:54.640 --> 00:00:57.050
They're all linked into the environment file here.

18
00:00:57.050 --> 00:00:59.170
You can download the debtor trial.

19
00:00:59.180 --> 00:01:03.160
Just click on the download button here and this will give you the following files

20
00:01:04.140 --> 00:01:07.290
here. I've unpacked the files into Atlanta desert. It has an

21
00:01:07.560 --> 00:01:11.740
little notes and a sets directory. The ends directory has the environment assets.

22
00:01:11.740 --> 00:01:13.770
So the desert plants and rocks.

23
00:01:13.920 --> 00:01:15.910
Notes has some shade as we won't be looking to

24
00:01:15.910 --> 00:01:19.350
those and in sexual find Atlanta desert dot blend.

25
00:01:19.540 --> 00:01:22.040
I have already created the Jason file that we're going

26
00:01:22.040 --> 00:01:24.660
to use of course in the preparation for this video

27
00:01:24.940 --> 00:01:26.880
and we have a new setup plant which

28
00:01:26.880 --> 00:01:29.160
we're going to build up automatically from scratch.

29
00:01:29.540 --> 00:01:31.480
Now before we start Ultimate NG things,

30
00:01:31.480 --> 00:01:34.670
it's always good to take a look at blender itself and manually

31
00:01:34.670 --> 00:01:37.060
go through the steps that you want your script to perform.

32
00:01:37.070 --> 00:01:39.950
So typically you would go to a while and then link,

33
00:01:40.940 --> 00:01:44.450
you would go to the environment assets say rocks

34
00:01:44.840 --> 00:01:47.850
and there we have collections that we want to link in.

35
00:01:47.860 --> 00:01:49.060
The naming is as follows,

36
00:01:49.060 --> 00:01:51.290
you have E N dash which stands for environment

37
00:01:51.300 --> 00:01:53.750
asset and then there is a name and number.

38
00:01:53.760 --> 00:01:55.470
So we want to have all the N rocks,

39
00:01:55.470 --> 00:01:59.530
we want to have all the smooth in rocks is like the entire thing.

40
00:01:59.540 --> 00:02:00.080
Um,

41
00:02:00.080 --> 00:02:02.010
we don't want that 11 thing you can already

42
00:02:02.010 --> 00:02:04.160
see is that all the collections have a certain prefix

43
00:02:04.340 --> 00:02:07.960
this we can use in our script so that it will automatically pick up the right ones.

44
00:02:08.039 --> 00:02:12.660
So, and ash rock underscore s underscore. That will work for the first set

45
00:02:13.240 --> 00:02:17.690
and Ian dash rock underscore smooth underscore will work for the second set.

46
00:02:17.710 --> 00:02:19.660
Now let's just link in one of those.

47
00:02:20.540 --> 00:02:25.150
You click a link and automatically blender instances this into the scene.

48
00:02:25.160 --> 00:02:28.320
If you want to link this into our seen ourselves in our script,

49
00:02:28.330 --> 00:02:31.500
we have to investigate further into what this actually is.

50
00:02:31.510 --> 00:02:35.750
So what we have here is an object and is actually an empty object

51
00:02:36.140 --> 00:02:38.110
with instance ng set to collection

52
00:02:38.300 --> 00:02:41.110
incensing a collection from rock stop blend

53
00:02:41.280 --> 00:02:42.350
furthermore,

54
00:02:42.630 --> 00:02:45.010
the empty is of course part of the same collection

55
00:02:45.010 --> 00:02:47.010
and that is why it appears in our scene.

56
00:02:47.020 --> 00:02:48.540
So this is what we have to do.

57
00:02:48.720 --> 00:02:50.800
We have to Lincoln the collection from the

58
00:02:50.800 --> 00:02:53.210
rock stop blend into the current blend file.

59
00:02:53.220 --> 00:02:55.090
Then we have to create an empty and that

60
00:02:55.090 --> 00:02:58.160
empty will instance that collection into our scene.

61
00:02:58.340 --> 00:03:03.060
Now contrary to what we did before, we're not going to use as a link operator.

62
00:03:03.120 --> 00:03:05.880
It's very hard to use from python and there is a much

63
00:03:05.880 --> 00:03:08.570
better way to do this if you go to help python,

64
00:03:08.580 --> 00:03:09.760
api reference

65
00:03:10.440 --> 00:03:15.060
and then so for bP data libraries you find it's a library.

66
00:03:15.060 --> 00:03:18.560
Data blocks of type B pi data libraries, click on that

67
00:03:18.940 --> 00:03:22.010
and here you will get to the load function.

68
00:03:22.170 --> 00:03:26.250
So this is what we're going to use be pint of data to libraries that load

69
00:03:26.590 --> 00:03:27.920
this takes a file path,

70
00:03:28.010 --> 00:03:30.880
it takes a link parameter that can be set to true if

71
00:03:30.880 --> 00:03:32.770
you want to link which is what we want to do.

72
00:03:32.780 --> 00:03:34.960
Otherwise it would depend to the current file

73
00:03:35.440 --> 00:03:37.370
relatives should be true if the file path is

74
00:03:37.370 --> 00:03:39.960
to be interpreted as relative to the current blend file

75
00:03:40.440 --> 00:03:42.740
here are some examples of how to use this function.

76
00:03:42.930 --> 00:03:45.610
The function actually acts as a context manager,

77
00:03:45.620 --> 00:03:49.130
which means that you use it with the with statement.

78
00:03:49.150 --> 00:03:51.360
A with statement has an indented body

79
00:03:52.240 --> 00:03:55.030
so you have to with then you have an expression here then

80
00:03:55.030 --> 00:03:58.560
you have Colin again like we know from the while etcetera.

81
00:03:58.940 --> 00:04:01.620
And then again you have a bit of code that is

82
00:04:01.630 --> 00:04:04.660
indented that is called the body of the with statement.

83
00:04:05.040 --> 00:04:08.230
Now the with statement does something before it enters

84
00:04:08.230 --> 00:04:10.490
the body and then it does something else when it

85
00:04:10.500 --> 00:04:12.780
exits the body you can use this for all

86
00:04:12.780 --> 00:04:15.790
kinds of things like opening file and reading the content

87
00:04:15.930 --> 00:04:18.480
and then making sure that when that with statement

88
00:04:18.480 --> 00:04:21.630
is over the finals automatically close again cleaning up.

89
00:04:21.640 --> 00:04:22.460
I should go.

90
00:04:23.040 --> 00:04:25.740
In this case it loads the blend file and it

91
00:04:25.750 --> 00:04:29.840
exposes the contents of the blend file in data from

92
00:04:30.030 --> 00:04:32.780
and then you can assign to data to whatever data

93
00:04:32.780 --> 00:04:35.180
blocks you want to link from that blend file,

94
00:04:35.310 --> 00:04:37.260
the data from and data to.

95
00:04:37.440 --> 00:04:40.970
They look pretty similar to the b part of data that you already know.

96
00:04:40.970 --> 00:04:44.760
So it has a dot teens dot object of collections etcetera.

97
00:04:45.340 --> 00:04:49.400
The only difference is that they're not really objects or scenes or collections.

98
00:04:49.530 --> 00:04:51.160
They're just a list of names.

99
00:04:51.540 --> 00:04:55.000
So we can act based on the name of the scene. The name of the collection.

100
00:04:55.000 --> 00:04:56.060
The name of the object

101
00:04:56.440 --> 00:04:58.770
and whatever names we give to data to

102
00:04:58.780 --> 00:05:01.420
will be finally imported when the with statement is

103
00:05:01.430 --> 00:05:04.370
over here in this example it appends the

104
00:05:04.370 --> 00:05:06.520
scene called scene to the current blend file.

105
00:05:06.530 --> 00:05:10.550
It depends because the default for link is false. So that's the default behavior.

106
00:05:10.720 --> 00:05:13.070
There is no link parameter given. So it will append

107
00:05:13.360 --> 00:05:15.160
down here. You'll see an example

108
00:05:15.340 --> 00:05:18.360
with links. True. So that will link instead of appending

109
00:05:18.740 --> 00:05:22.750
the data to dot scenes. The measures that object etcetera are lists

110
00:05:22.940 --> 00:05:26.460
like we've seen before. So we can also append things to them.

111
00:05:26.470 --> 00:05:29.840
Similar with data from data from the collections is also a

112
00:05:29.840 --> 00:05:32.860
list of collection names so we can loop over those.

113
00:05:33.410 --> 00:05:36.680
This means that we can loop over the collections, look at their name,

114
00:05:36.770 --> 00:05:40.040
see if the prefixed with the prefix that we wanted to have and if.

115
00:05:40.040 --> 00:05:43.060
So we append them to the data to dot collections list.

116
00:05:43.440 --> 00:05:46.110
Now let's go back to blender and write some code to

117
00:05:46.110 --> 00:05:48.570
explore this a little bit and see how it behaves.

118
00:05:48.710 --> 00:05:51.830
So Rebecca blender and this will all look

119
00:05:51.830 --> 00:05:54.630
pretty familiar by now we're importing beep.

120
00:05:54.630 --> 00:05:55.090
I

121
00:05:55.280 --> 00:05:59.570
we set a file path and because I've saved this file inside the sets directory we

122
00:05:59.570 --> 00:06:03.860
have to go one directory up then two ends and then we have rock stop blend.

123
00:06:04.440 --> 00:06:07.620
The rest is pretty similar to the example that we've seen already.

124
00:06:07.630 --> 00:06:10.180
So we have with BP data libraries load

125
00:06:10.190 --> 00:06:12.450
then the final path and link equals true because

126
00:06:12.450 --> 00:06:17.250
we want to link and then we have as data from accommodated to as we expected.

127
00:06:17.940 --> 00:06:21.800
Now all we do here is print data from the collections just to

128
00:06:21.800 --> 00:06:25.310
get a peek on what is exactly in there for our particular file,

129
00:06:25.320 --> 00:06:29.250
click play and then we go to the terminal where we started blender.

130
00:06:29.640 --> 00:06:33.430
If you're on windows you can go to window and then total system console

131
00:06:33.570 --> 00:06:34.860
in order to see this

132
00:06:35.240 --> 00:06:37.900
here we see that list of collection names.

133
00:06:37.910 --> 00:06:41.770
Just like we saw in blender itself when we manually went through all the steps.

134
00:06:41.880 --> 00:06:45.750
Now, even though we didn't actually link anything from that blend file yet,

135
00:06:45.800 --> 00:06:47.250
it did have some effect.

136
00:06:47.340 --> 00:06:48.760
If you look at the outline er

137
00:06:50.540 --> 00:06:54.510
you can see that it did load the rock stop blend file that we wanted.

138
00:06:54.540 --> 00:06:56.360
So it's already doing something

139
00:06:56.940 --> 00:06:58.540
now we have our list of names,

140
00:06:58.590 --> 00:07:00.630
let's do some filtering on the names that we find

141
00:07:00.640 --> 00:07:04.350
interesting and actually link those into the current blend file

142
00:07:04.840 --> 00:07:10.460
for this. Let's first determine our prefix. So remember we had two sets of rocks.

143
00:07:10.470 --> 00:07:13.570
Let's just focus on one now and then we'll do the other later.

144
00:07:13.580 --> 00:07:17.950
And that started with the n dash rocks and score as underscore

145
00:07:18.440 --> 00:07:19.850
we can remove this print

146
00:07:20.240 --> 00:07:26.660
and just say for collection name in data from the collections

147
00:07:28.440 --> 00:07:33.760
if it doesn't start with our prefix, we're not interested.

148
00:07:33.760 --> 00:07:36.100
So we skip them by using the continue keyword.

149
00:07:36.110 --> 00:07:40.570
This will just tell the for loop to go to the next element and skip the rest of the body.

150
00:07:40.580 --> 00:07:43.990
In other cases though we have the prefix and that means we

151
00:07:43.990 --> 00:07:47.130
have to tell blender that we actually want to link this collection.

152
00:07:48.240 --> 00:07:51.320
This means we have to do something dot data to dot collections

153
00:07:51.500 --> 00:07:54.600
and says that list we can append to it.

154
00:07:54.820 --> 00:07:56.250
Let's give this a try.

155
00:07:59.240 --> 00:08:00.350
This will work fine

156
00:08:01.740 --> 00:08:05.060
and now we can open this file in the outline er

157
00:08:05.840 --> 00:08:09.200
and we can see that these collections have been added to it.

158
00:08:09.210 --> 00:08:12.050
These are exactly the collections that we wanted and all

159
00:08:12.050 --> 00:08:14.270
the collections that we didn't want are not here.

160
00:08:14.270 --> 00:08:16.060
So our code is doing the right thing.

161
00:08:16.410 --> 00:08:19.010
Now all that's left is to create an empty that instances

162
00:08:19.010 --> 00:08:22.960
these collections and then link those empties into a same collection.

163
00:08:23.440 --> 00:08:27.330
There's something special about this data to within the body of her.

164
00:08:27.330 --> 00:08:32.169
With data to dot collections is a list of names but once the with

165
00:08:32.169 --> 00:08:37.640
body is done and deploy data libraries that load has actually loaded what we needed

166
00:08:37.909 --> 00:08:41.500
data to dot collections is no longer a list of collection names.

167
00:08:41.919 --> 00:08:45.250
It's actually a list of the collections that were linked in.

168
00:08:45.540 --> 00:08:49.350
So what we can do is looped over them.

169
00:08:50.940 --> 00:08:55.050
Now here cole is first the first collection then the second collection etcetera.

170
00:08:55.540 --> 00:08:56.870
Now let's create an empty

171
00:09:00.840 --> 00:09:02.750
it should be named after their collection

172
00:09:03.080 --> 00:09:04.650
and it shouldn't have any data.

173
00:09:05.140 --> 00:09:08.130
This way blender understands that this object should be an

174
00:09:08.130 --> 00:09:10.940
empty and not a camera or measure anything else.

175
00:09:10.950 --> 00:09:15.060
Now we have to set some properties and let's take a look at what those are.

176
00:09:16.940 --> 00:09:19.460
So just to test I add an empty empty,

177
00:09:22.640 --> 00:09:26.990
I have to set Lisbon which is object of instance type.

178
00:09:27.000 --> 00:09:29.560
And of course we have to set the collection to

179
00:09:29.570 --> 00:09:31.740
one of the collections and there you have it.

180
00:09:31.740 --> 00:09:32.860
This is all we need to do.

181
00:09:33.340 --> 00:09:37.110
So however over here and you say it's instance collection. So we have to set

182
00:09:37.360 --> 00:09:38.650
instance underscored type

183
00:09:39.040 --> 00:09:40.930
and instance underscore collection.

184
00:09:41.230 --> 00:09:45.850
I'm guessing that instance underscore type will have to have the value collection

185
00:09:46.440 --> 00:09:48.360
but we can always look that up. Of course.

186
00:09:49.640 --> 00:09:49.790
Yeah.

187
00:09:50.470 --> 00:09:54.260
And there you have it. The instance type should be set to collection in capitals.

188
00:09:55.540 --> 00:09:57.550
So let's just copy this.

189
00:09:58.040 --> 00:09:58.240
Yeah.

190
00:10:00.230 --> 00:10:01.760
And then we have that

191
00:10:03.040 --> 00:10:06.030
and instance collection is of course the collection

192
00:10:06.030 --> 00:10:08.260
that we want to instance which is coal.

193
00:10:09.940 --> 00:10:13.060
This will create the empty but it's not yet part of our sane.

194
00:10:13.540 --> 00:10:16.980
Now I think it's nice to create a special collection to have

195
00:10:16.980 --> 00:10:20.160
all these empties rather than having them at the top level.

196
00:10:20.540 --> 00:10:24.860
So before we start doing this we want to make sure that that collection exists.

197
00:10:25.540 --> 00:10:25.750
Okay

198
00:10:26.840 --> 00:10:28.400
seen as beep.

199
00:10:28.400 --> 00:10:32.720
I context seen later when we change this into an operator that does all the work.

200
00:10:32.720 --> 00:10:36.860
Of course we we use context that scene and not be pint of context. That scene.

201
00:10:36.870 --> 00:10:38.660
But for now this will suffice

202
00:10:39.040 --> 00:10:44.310
let's say that the collection that we want

203
00:10:44.310 --> 00:10:47.050
to link everything to should be called environment.

204
00:10:47.440 --> 00:10:48.180
Now

205
00:10:48.310 --> 00:10:51.350
I want to create this collection but only if it doesn't exist yet.

206
00:10:51.510 --> 00:10:55.930
The easiest way to do this is just to act as if it already exists

207
00:10:56.070 --> 00:10:58.850
and then see what error occurs when it doesn't exist yet.

208
00:10:58.920 --> 00:11:00.910
And then simply handled that case.

209
00:11:01.840 --> 00:11:03.460
So let's do the simple thing first.

210
00:11:03.840 --> 00:11:06.360
Let's say it already exists.

211
00:11:06.940 --> 00:11:11.050
Then we can access this collection through the scene

212
00:11:13.940 --> 00:11:15.560
like this.

213
00:11:15.940 --> 00:11:17.650
And it would just give us a collection

214
00:11:18.440 --> 00:11:19.960
if it doesn't exist yet.

215
00:11:20.640 --> 00:11:20.850
Okay

216
00:11:21.240 --> 00:11:21.450
mm.

217
00:11:21.940 --> 00:11:23.650
This will raise a key error.

218
00:11:24.240 --> 00:11:25.840
Now we have the information that we need.

219
00:11:25.840 --> 00:11:30.660
Let's go back to the code as we've done before. We want to use try and accept

220
00:11:31.040 --> 00:11:37.060
so we tell python try this for us.

221
00:11:40.840 --> 00:11:48.160
And if this fails we'll have to create a new collection.

222
00:11:48.640 --> 00:11:48.950
Okay.

223
00:11:51.540 --> 00:11:56.160
And then once that collection is created we have to add it to the same collection.

224
00:11:59.540 --> 00:11:59.860
Okay.

225
00:12:01.140 --> 00:12:03.140
So now this code will just use the existing

226
00:12:03.140 --> 00:12:05.240
collection if it's there and create a new one.

227
00:12:05.240 --> 00:12:08.160
If it isn't now that we have the collection we want to link to.

228
00:12:08.170 --> 00:12:09.860
We can do they're just that.

229
00:12:10.240 --> 00:12:10.470
Yeah.

230
00:12:11.040 --> 00:12:11.240
Yeah.

231
00:12:13.880 --> 00:12:15.350
And this will linker empty.

232
00:12:18.940 --> 00:12:21.710
You can see that running now to considerably longer.

233
00:12:21.900 --> 00:12:24.850
And here we have all our assad's linked into the file.

234
00:12:26.770 --> 00:12:27.730
They're all empties

235
00:12:27.860 --> 00:12:29.660
they're all inside the environment collection.

236
00:12:31.240 --> 00:12:33.660
And we don't have anything we don't want to have

237
00:12:34.140 --> 00:12:35.230
now as a final touch.

238
00:12:35.230 --> 00:12:39.480
Let's line up all the empties in one line along the X axis so

239
00:12:39.480 --> 00:12:43.490
that they're nicely spread out and not all lumped in one spot for that.

240
00:12:43.490 --> 00:12:47.390
We have to keep track of the X. Location of the previous empty.

241
00:12:47.400 --> 00:12:50.660
So let's just start at zero

242
00:12:51.710 --> 00:12:54.290
and let's give every empty to blender units

243
00:12:54.470 --> 00:12:57.370
now that they're linked to a scene, we can set their location.

244
00:12:58.140 --> 00:12:58.350
Okay.

245
00:13:00.840 --> 00:13:03.950
And then we can increment location X by step X.

246
00:13:03.960 --> 00:13:06.640
So that the next empty will use the next location.

247
00:13:07.210 --> 00:13:12.160
That's delete all the MTs. Run the script again

248
00:13:18.540 --> 00:13:20.850
and there they are all neatly lined up.

249
00:13:22.140 --> 00:13:24.470
Our script is working but it's very limited.

250
00:13:24.640 --> 00:13:27.630
If we want to load assets with a different prefix we

251
00:13:27.630 --> 00:13:30.750
have to go and change this prefix in the source code.

252
00:13:30.750 --> 00:13:31.880
Run the script again,

253
00:13:32.060 --> 00:13:35.360
if we want to load from a different blend file we have to do that again.

254
00:13:35.550 --> 00:13:38.310
If you want to link the empties into a different scene collection

255
00:13:38.320 --> 00:13:40.780
again we have to edit the script and run it again.

256
00:13:41.340 --> 00:13:45.410
It will be much nicer if we had a file that contains all this information it

257
00:13:45.410 --> 00:13:47.960
contains which blend files to use which prefixes

258
00:13:47.960 --> 00:13:49.950
to use which collections to link into.

259
00:13:50.540 --> 00:13:54.050
We can then put our code into an add on that doesn't need to change

260
00:13:54.440 --> 00:13:57.620
and other projects specifics go into a separate file.

261
00:13:57.740 --> 00:14:00.380
Before we do that though, let's copy our code,

262
00:14:00.380 --> 00:14:02.330
put it into a separate python pile

263
00:14:02.340 --> 00:14:05.180
and shuffle some code around into separate functions

264
00:14:05.360 --> 00:14:08.200
so that we can work with some higher level building blocks in

265
00:14:08.200 --> 00:14:11.450
stata of looking at one big blob of code all the time.

266
00:14:11.940 --> 00:14:14.160
So let's copy what we have now

267
00:14:14.840 --> 00:14:18.740
and go to the add on. I already prepared we've seen this a bunch of times.

268
00:14:18.750 --> 00:14:21.460
It's just a python file with a bill info

269
00:14:22.040 --> 00:14:23.180
we improve beep. I

270
00:14:23.360 --> 00:14:25.650
I've already added the operator with an empty

271
00:14:25.650 --> 00:14:27.880
execute functions or will feel that out.

272
00:14:28.010 --> 00:14:30.220
It is added to the import menu so that we

273
00:14:30.220 --> 00:14:33.050
can just to file import and then load stuff from Jason

274
00:14:33.640 --> 00:14:36.210
and we have the blender classes where we have the operator

275
00:14:36.210 --> 00:14:39.850
class and the registered and unregistered functions that we've seen before.

276
00:14:40.240 --> 00:14:43.260
So above all of that let's paste the code with that

277
00:14:43.260 --> 00:14:46.450
we just copied and let's turn this into some functions.

278
00:14:46.940 --> 00:14:50.130
So there's a few things going on here at the same time.

279
00:14:50.250 --> 00:14:53.090
This first bit is linking data into the current blend file.

280
00:14:53.100 --> 00:14:56.690
Then we have some code that creates a collection for us if it doesn't exist yet.

281
00:14:56.700 --> 00:14:58.770
So actually that's quite a different thing so

282
00:14:58.770 --> 00:15:00.700
that can be moved into a different function.

283
00:15:01.100 --> 00:15:04.400
Then we look over all the collections that we linked into the blend file.

284
00:15:04.410 --> 00:15:06.760
So this actually belongs to the first bit of code.

285
00:15:08.640 --> 00:15:13.650
So let's take this part out and start writing a separate function for it.

286
00:15:14.140 --> 00:15:14.350
Mhm.

287
00:15:14.740 --> 00:15:19.050
Let's call it ensure collection because it ensures that the collection exists.

288
00:15:19.560 --> 00:15:21.250
It needs access to the scene.

289
00:15:22.250 --> 00:15:24.460
It needs to know the collection name of course.

290
00:15:26.280 --> 00:15:29.360
And then we declare that it will return the collection.

291
00:15:29.740 --> 00:15:31.960
Let's move our code in here

292
00:15:40.120 --> 00:15:42.360
and change that collection name.

293
00:15:42.940 --> 00:15:48.360
Now all we have to do is return the link to collection and then this function is done.

294
00:15:48.840 --> 00:15:50.850
Now we have the other bit of code to look into.

295
00:15:52.840 --> 00:15:53.250
Yeah

296
00:15:54.040 --> 00:15:56.060
let's call this function linked to a scene.

297
00:15:56.190 --> 00:16:00.460
It will take the file path as a parameter instead of having it hard coded here.

298
00:16:01.040 --> 00:16:04.760
Similarly, it will take a prefix as a parameter instead of hard coding in here.

299
00:16:05.340 --> 00:16:09.280
Then we have the link to which is a collection and then the location.

300
00:16:09.280 --> 00:16:13.190
Why later on we will be linking multiple sets of assets from multiple

301
00:16:13.190 --> 00:16:16.440
blend files and then having them in different rows is kind of nice.

302
00:16:16.440 --> 00:16:20.470
So that is why I added a location, Y, parameter here as well.

303
00:16:20.480 --> 00:16:21.970
Now the rest of the coat needs to be

304
00:16:21.980 --> 00:16:24.360
invented because it needs to be part of the function

305
00:16:24.740 --> 00:16:27.820
and of course now we also have to set the wild location

306
00:16:28.180 --> 00:16:29.860
so we can do that like this.

307
00:16:31.140 --> 00:16:35.160
Now that we have are two functions, we can just call these from our operator

308
00:16:35.540 --> 00:16:39.110
still with the same hard coded values that we used before

309
00:16:39.120 --> 00:16:41.470
so that at least we can see that they're working.

310
00:16:42.740 --> 00:16:48.260
So first of all we have to create the link to collection.

311
00:16:48.840 --> 00:16:49.980
Yeah.

312
00:16:51.700 --> 00:16:55.170
And then we can call linked to a scene with the

313
00:16:55.170 --> 00:16:58.810
same file path and the same prefix as we used before.

314
00:16:58.820 --> 00:17:01.960
Then the collection. Then the collection we want to link to.

315
00:17:03.140 --> 00:17:06.349
And let's just use zero as the wild location for now.

316
00:17:07.140 --> 00:17:10.349
Now of course you have to enable your I don't you know how that's done

317
00:17:10.940 --> 00:17:13.400
now you have to install and enable your adam.

318
00:17:13.410 --> 00:17:16.060
If you want to have a refresher on how that is done.

319
00:17:16.069 --> 00:17:19.510
Look for the scripting for artist chapter called from script to add on there.

320
00:17:19.510 --> 00:17:20.450
It's all explained.

321
00:17:20.839 --> 00:17:23.770
Now you got to file import link assets from Jason

322
00:17:23.780 --> 00:17:26.450
and you can see that everything works as expected.

323
00:17:27.740 --> 00:17:31.420
Now it's time to look at the Jason file that I've mentioned a couple of times already.

324
00:17:31.430 --> 00:17:36.050
Jason stands for javascript object notation and it's quite a common file format.

325
00:17:36.060 --> 00:17:40.750
It's not the easiest to work with but it is doable and it's not that complex.

326
00:17:40.750 --> 00:17:44.770
And python has built in support for parsing those files and for saving those files.

327
00:17:44.940 --> 00:17:48.170
So it's rather convenient file from a free to use for our case

328
00:17:48.340 --> 00:17:48.660
here.

329
00:17:48.660 --> 00:17:53.420
You see a high level overview of what that file looks like if curly braces want to open

330
00:17:53.420 --> 00:17:55.650
one to close the file and within the curly

331
00:17:55.650 --> 00:17:58.690
braces you have what is basically a python dictionary.

332
00:17:58.700 --> 00:18:02.560
So it's a name between quotes and that name has a value

333
00:18:03.140 --> 00:18:04.140
in this case

334
00:18:04.270 --> 00:18:05.560
it is another dictionary.

335
00:18:06.440 --> 00:18:10.280
So here we say these are our collections and we want to have to collections.

336
00:18:10.280 --> 00:18:12.770
We want to have rocks and we want to have plants

337
00:18:13.440 --> 00:18:14.760
And within each

338
00:18:15.140 --> 00:18:18.550
we say link which will contain a list of files and prefixes

339
00:18:18.550 --> 00:18:21.460
that we want to link into the scene into this collection.

340
00:18:21.470 --> 00:18:24.890
So here you see a list of things. It has the same notation as python.

341
00:18:24.890 --> 00:18:27.870
So with these blocky parentheses kind of things.

342
00:18:27.910 --> 00:18:32.060
And within it we have a list of in this case to dictionaries.

343
00:18:32.060 --> 00:18:32.450
Again,

344
00:18:32.460 --> 00:18:36.270
each dictionary has a file and it has a prefix

345
00:18:36.280 --> 00:18:38.930
of all the collections to link in from that file.

346
00:18:38.940 --> 00:18:40.860
We do something similar for the plants.

347
00:18:40.870 --> 00:18:45.350
We have a link and then again file, prefix file, prefix file,

348
00:18:45.350 --> 00:18:47.380
prefix in this case there are three of them.

349
00:18:47.380 --> 00:18:47.770
Even

350
00:18:48.940 --> 00:18:53.820
now we could have removed this link and put this information directly into plants.

351
00:18:53.830 --> 00:18:56.420
But I I always like to have things a little bit

352
00:18:56.420 --> 00:18:59.490
more explicit and this will also give you a chance later.

353
00:18:59.490 --> 00:19:01.430
If you want to add something extra to it.

354
00:19:01.440 --> 00:19:07.270
Maybe uh maybe you want to have something visible but default or not or maybe you

355
00:19:07.270 --> 00:19:11.720
want to enable only in certain view layers or not by having this under a link.

356
00:19:11.720 --> 00:19:12.060
K.

357
00:19:12.060 --> 00:19:15.450
You can add multiple keys later for your own workflow for now

358
00:19:15.450 --> 00:19:18.850
let's keep it simple and write some code that loads this file,

359
00:19:19.170 --> 00:19:22.190
then turns it into python data structure that we can use.

360
00:19:22.740 --> 00:19:24.560
So let's comment out these two lines.

361
00:19:25.050 --> 00:19:28.190
We'll just leave them sticking around as a reference for us. Later,

362
00:19:28.740 --> 00:19:32.850
first moved to load the Jason file and for that we need to have the Jason module.

363
00:19:33.240 --> 00:19:38.840
This is built into python so we can just do import Jason and then we have access to it.

364
00:19:38.850 --> 00:19:42.250
I think it's a nice touch to have the Jason file sitting next to the blend file,

365
00:19:42.250 --> 00:19:45.220
which means that we want to write it in slash slash notation,

366
00:19:45.230 --> 00:19:47.730
which means we have to translate the file first.

367
00:19:47.740 --> 00:19:51.860
This can be done by passing the slash slash ss dot Jason file name

368
00:19:52.040 --> 00:19:53.960
through the pilot passed its path

369
00:19:54.140 --> 00:19:56.270
and this will take care of that.

370
00:19:56.640 --> 00:20:00.960
So now all we need to do is open the file and feed it to the Jason library.

371
00:20:01.540 --> 00:20:05.590
I will be copy pasting some code instead of typing at all because now

372
00:20:05.590 --> 00:20:09.360
we have a lot of python code that will do auto completion and show

373
00:20:09.360 --> 00:20:13.950
information pop ups and everything because I scaled up my editor for this video

374
00:20:13.960 --> 00:20:17.560
is going to block a lot of things and be more distracting than anything.

375
00:20:18.540 --> 00:20:20.270
So this is how you open a file,

376
00:20:20.840 --> 00:20:24.410
you can say with open Jason file name as in file

377
00:20:24.680 --> 00:20:26.640
and this will open the file for reading

378
00:20:26.940 --> 00:20:29.930
and once the with statement is done it will close it again,

379
00:20:29.930 --> 00:20:33.780
making sure that we don't leave any open file handles around online.

380
00:20:33.900 --> 00:20:35.170
This is not such an issue,

381
00:20:35.240 --> 00:20:39.330
but on Windows is a problematic because if the file is opened by one program,

382
00:20:39.340 --> 00:20:42.000
another program won't be able to read or write to that file,

383
00:20:42.000 --> 00:20:43.850
you won't even be able to delete the file.

384
00:20:44.040 --> 00:20:47.180
So always make sure that you close a file after you have opened it.

385
00:20:47.310 --> 00:20:50.160
And the with statement is a perfect way to do that.

386
00:20:50.740 --> 00:20:52.270
After we've opened the file

387
00:20:52.640 --> 00:20:56.710
we can pass it to Jason download which takes a file as the first parameter.

388
00:20:56.740 --> 00:21:00.460
And this will load the file and parse it into a python data structure.

389
00:21:01.240 --> 00:21:03.980
This means that link info here is a dictionary.

390
00:21:03.990 --> 00:21:08.000
If you want to see exactly what in there you can use a print statement, run the code

391
00:21:08.200 --> 00:21:09.760
and then you can see for yourself.

392
00:21:10.240 --> 00:21:13.280
So going back to our Jason file we know that it represents

393
00:21:13.280 --> 00:21:17.930
a dictionary with one key called collections which contains another dictionary.

394
00:21:17.940 --> 00:21:19.860
So let's see how we access this in python.

395
00:21:21.370 --> 00:21:23.050
You would take the link in for dick.

396
00:21:23.440 --> 00:21:28.350
We get the key collections and we can loop over this by taking key value pairs

397
00:21:28.530 --> 00:21:31.230
and for this we use the dot items function

398
00:21:31.600 --> 00:21:36.060
dot items on the dictionary gives us something that we can loop over with a for loop

399
00:21:36.440 --> 00:21:39.520
and it will get you paris of the key and the value

400
00:21:39.870 --> 00:21:43.550
in our case it will give us rocks with that dictionary.

401
00:21:44.140 --> 00:21:46.660
And then pants with the 2nd dictionary.

402
00:21:47.240 --> 00:21:50.260
So call name is first rocks and call info is it's

403
00:21:50.260 --> 00:21:53.770
dictionary and then on the second iteration call name is plants

404
00:21:53.970 --> 00:21:56.270
and then call info is the plants dictionary.

405
00:21:56.740 --> 00:22:01.270
So the first thing we have to do is ensure that this collection actually exists.

406
00:22:02.440 --> 00:22:05.560
We have a function for that. So let's move that code

407
00:22:06.600 --> 00:22:07.660
and put it here.

408
00:22:08.340 --> 00:22:11.590
And instead of having environment as art coated collection,

409
00:22:11.660 --> 00:22:14.460
we of course take the collection name from the Jason file.

410
00:22:15.660 --> 00:22:18.800
Now might also be a good time to just save your file.

411
00:22:18.820 --> 00:22:23.350
Reloaded in blender and see that it actually creates those collections for you.

412
00:22:23.460 --> 00:22:25.900
Just as a first step to see that that is working.

413
00:22:26.180 --> 00:22:30.350
And also run it twice to see that it still works when the collections already exist.

414
00:22:31.340 --> 00:22:33.590
So the next thing we have to do is take a look

415
00:22:33.590 --> 00:22:36.460
at that link key and the list that is in there.

416
00:22:37.040 --> 00:22:39.670
So calling for link will give us this list

417
00:22:41.840 --> 00:22:44.670
and of course because it's a list we can just loop over it

418
00:22:44.680 --> 00:22:47.750
and it will give us that dictionary with a file and a prefix.

419
00:22:47.870 --> 00:22:50.360
Let's make it a little bit easier for us and

420
00:22:51.340 --> 00:22:56.670
take the file and the prefix keys and store their values in these variables.

421
00:22:57.340 --> 00:23:00.270
So for the first iteration, foul path will be

422
00:23:00.640 --> 00:23:01.360
this path

423
00:23:02.940 --> 00:23:04.770
and this prefix will be

424
00:23:05.440 --> 00:23:06.260
this prefix

425
00:23:08.140 --> 00:23:11.050
what is left now is to link everything to the scene.

426
00:23:12.840 --> 00:23:15.770
But instead of using her heart coated file path

427
00:23:17.380 --> 00:23:21.350
we take the file path from adjacent file and the same for the prefix.

428
00:23:22.940 --> 00:23:25.080
Now we have to make sure that the white location

429
00:23:25.080 --> 00:23:28.120
is increased and not everything ends up at Y0.

430
00:23:28.130 --> 00:23:32.170
So we can do the same thing for this as we've done before with the explication.

431
00:23:34.940 --> 00:23:40.800
We can start location Y at zero and then passed it here. After this is done.

432
00:23:40.800 --> 00:23:45.720
All we need to do is increments the wild location with the step size and that's it.

433
00:23:45.740 --> 00:23:47.770
Let's go back to blender and see it in action.

434
00:23:49.520 --> 00:23:52.450
Let's delete everything there was including the collection

435
00:23:57.240 --> 00:24:00.960
and there it is. We have rocks, we have plants

436
00:24:03.740 --> 00:24:08.270
and everything is there in the file for you to start dressing up your set.

437
00:24:08.840 --> 00:24:10.270
Now this one final touch

438
00:24:10.700 --> 00:24:13.720
let's say the asset files change and something new was added.

439
00:24:13.950 --> 00:24:16.750
This is not automatically loaded into this file.

440
00:24:16.820 --> 00:24:20.170
If we rerun the operator again, it will create all the empties again.

441
00:24:20.180 --> 00:24:22.840
Including the ones that already exists in the file.

442
00:24:22.890 --> 00:24:25.380
That's at a little filter to the creation of the empties

443
00:24:25.430 --> 00:24:27.920
so that her coat doesn't try to recreate an empty.

444
00:24:27.920 --> 00:24:29.060
If it's already there

445
00:24:29.440 --> 00:24:32.940
for this we have to go to the link to seen function linking things

446
00:24:32.940 --> 00:24:36.970
that are already linked is fine blender will skip these automatically for us.

447
00:24:36.980 --> 00:24:40.660
So it really only the creation of the empties that we need to worry about.

448
00:24:41.240 --> 00:24:43.570
So here we are at the line that creates a new empty.

449
00:24:43.580 --> 00:24:47.190
So before this we want to have our check this I think is a nice approach.

450
00:24:47.200 --> 00:24:50.580
The collection name is going to be used as the empty name as well.

451
00:24:50.590 --> 00:24:54.450
So if the collection name already exists in linked to the objects,

452
00:24:54.450 --> 00:24:57.260
we know that the empty is already there and we can just keep it

453
00:24:58.240 --> 00:25:03.250
so I'll move this as a way. So we should not see anything new. Pop up here.

454
00:25:03.260 --> 00:25:05.890
And of course you can also see it here in the outline

455
00:25:05.890 --> 00:25:08.140
er that these numbers shouldn't increase

456
00:25:08.140 --> 00:25:09.960
because everything has already been loaded

457
00:25:14.240 --> 00:25:16.270
there. You have it it all works fine.

458
00:25:17.140 --> 00:25:19.780
So this is it for this episode of scripting for artists.

459
00:25:19.900 --> 00:25:23.250
Of course this is just the start but you can imagine that

460
00:25:23.260 --> 00:25:27.610
things get pretty powerful when you can store information into adjacent file,

461
00:25:27.620 --> 00:25:31.410
load that into blender and use that to insensate certain collections.

462
00:25:31.420 --> 00:25:34.860
Of course collections can also consist of links collections.

463
00:25:35.040 --> 00:25:38.610
So the set that you're building with this tool can also be

464
00:25:38.610 --> 00:25:41.960
a collection that you instantly eight into a shot file for example.

465
00:25:42.140 --> 00:25:45.900
So I'll leave that up to your imagination and your workflow for now.

466
00:25:45.900 --> 00:25:48.320
This is it If you have any questions or comments,

467
00:25:48.320 --> 00:25:50.560
leave them below and I will see you soon


