WEBVTT 0 00:00:00.140 --> 00:00:02.040 Hello and welcome to scripting for artist. 1 00:00:02.040 --> 00:00:05.270 My name is civilian and in this episode we'll visit the topic that 2 00:00:05.280 --> 00:00:09.710 many of you have asked for user interfaces in the previous videos. 3 00:00:09.710 --> 00:00:12.860 We created our own operator and then turned it into an 4 00:00:12.870 --> 00:00:16.270 ad on the basics of creating your own user interface. 5 00:00:16.270 --> 00:00:20.560 In blender is pretty much the same. You create a class, you add some properties to it, 6 00:00:20.740 --> 00:00:23.480 you had a function to it and then you register that thing. 7 00:00:23.490 --> 00:00:27.760 So in this video we will look at the following will be finding existing you Aiko. 8 00:00:27.760 --> 00:00:31.630 So if you're wondering how did they do it after this? You'll know where to find it. 9 00:00:31.640 --> 00:00:35.720 We'll look at the ingredients of panel before making our own monkey grid panel. 10 00:00:35.720 --> 00:00:36.310 Of course, 11 00:00:36.320 --> 00:00:38.780 what else we'll be adding operator buttons to the 12 00:00:38.780 --> 00:00:41.030 panel and then tweaking the layout a little bit. 13 00:00:41.040 --> 00:00:43.150 Then we'll add seeing an object protest to it. 14 00:00:43.160 --> 00:00:45.460 And we'll also look at conditional drawing 15 00:00:45.540 --> 00:00:47.740 because sometimes there is no active objects. 16 00:00:47.740 --> 00:00:50.580 So you won't be able to draw properties from the active object. 17 00:00:50.590 --> 00:00:54.780 And finally we'll look at adding your own operators to existing menus and blender 18 00:00:54.900 --> 00:00:58.860 Before we make your own let's take a look at existing panels and blender 19 00:00:59.240 --> 00:01:00.370 in the three D view port, 20 00:01:00.370 --> 00:01:04.230 press and to show the panels and then let's look at the three d cursor panel, 21 00:01:04.319 --> 00:01:05.420 right click and choose. 22 00:01:05.430 --> 00:01:06.110 Edit Sirs. 23 00:01:06.120 --> 00:01:08.110 If you don't see the edit source option, 24 00:01:08.110 --> 00:01:11.320 go to preferences interface and enable developer extras and 25 00:01:11.320 --> 00:01:13.580 python tool tips and then it should show up. 26 00:01:13.590 --> 00:01:15.240 So let's see what happens if we click it 27 00:01:15.360 --> 00:01:18.360 blend owner tells us here in the corner but 28 00:01:18.370 --> 00:01:21.440 it's a bit far away because of my scaling basically 29 00:01:21.440 --> 00:01:25.310 it tells us to go to a text editor and there we should find the file with the source. 30 00:01:25.310 --> 00:01:26.560 But there's an easier way 31 00:01:27.340 --> 00:01:31.560 if you split this window and make sure that there is a text editor right here, 32 00:01:32.040 --> 00:01:34.620 you can click edit sores and there. 33 00:01:34.620 --> 00:01:38.360 Yes, the file, it will be open for you and even scrolls to the right line. 34 00:01:39.640 --> 00:01:42.420 Now let's scroll through this and see what we can find 35 00:01:42.670 --> 00:01:46.380 as we've seen with operators as a class. It has the same kind of naming. 36 00:01:46.380 --> 00:01:51.360 It has the category in capitals and then pt for panel type instead of operator type 37 00:01:51.740 --> 00:01:54.690 and then in lower case the rest of the identifier. 38 00:01:54.740 --> 00:01:57.300 And here you have the parent class in this case it's 39 00:01:57.300 --> 00:02:00.360 panel and the operator you would expect big pipe type. 40 00:02:00.360 --> 00:02:04.780 Stop panel here instead of a panel and we'll see why this is in a bit. 41 00:02:05.080 --> 00:02:06.570 Let's look for other commonalities. 42 00:02:06.570 --> 00:02:10.550 First will be a label which is the panels naming the user interface 43 00:02:10.840 --> 00:02:14.050 and then we have a category or region type and a space type. 44 00:02:14.440 --> 00:02:18.970 The space type and region type defined where in the user face it will show up. 45 00:02:18.980 --> 00:02:23.330 In this case we're looking at three D view port and region type issue I which 46 00:02:23.330 --> 00:02:27.100 means it's right in the three D view port and not in a header or footer. 47 00:02:27.110 --> 00:02:30.640 And the category is view which is the name of the tab here 48 00:02:30.710 --> 00:02:33.520 and then we get to be a label which is the name of 49 00:02:33.530 --> 00:02:38.400 the panel where operators had an execute function that performs the task of 50 00:02:38.400 --> 00:02:41.820 the operator here we have a draw function that draws the panel. 51 00:02:41.830 --> 00:02:46.560 The drawing itself is done through self dot layout which is a ui layout instance 52 00:02:46.720 --> 00:02:51.560 and you can find this with help and then python api reference and search for you. 53 00:02:51.560 --> 00:02:53.710 I lay out one word there you'll find the class 54 00:02:53.710 --> 00:02:56.770 definition with everything that is possible in this video. 55 00:02:56.770 --> 00:02:59.930 I will show you the basics though and that will get you quite far already. 56 00:02:59.970 --> 00:03:01.990 Now let's go back to the class definition 57 00:03:02.120 --> 00:03:05.010 because we saw that panel here and this is 58 00:03:05.010 --> 00:03:07.210 a little bit different than we've seen before. 59 00:03:07.260 --> 00:03:07.890 As I said, 60 00:03:07.890 --> 00:03:12.670 you would have expected something like bp dot types dot panel and not just panel. 61 00:03:12.740 --> 00:03:16.260 And this is because in this file is imported in a different way 62 00:03:16.640 --> 00:03:17.960 that's cruel to the top 63 00:03:18.540 --> 00:03:21.950 and here you can see from be part of types import heather, 64 00:03:21.960 --> 00:03:26.380 menu and panel and because of this you can just type panel instead of be 65 00:03:26.380 --> 00:03:30.180 part of type stock panel and it would refer to the same thing personally. 66 00:03:30.180 --> 00:03:32.860 I would say be very careful with imports like this, 67 00:03:32.870 --> 00:03:35.760 it can save you some typing and that may sound like a good thing 68 00:03:36.040 --> 00:03:39.200 but you will read code way more often than you write it. 69 00:03:39.210 --> 00:03:43.010 I would always optimise for code readability and understand 70 00:03:43.010 --> 00:03:46.490 ability rather than saving a few seconds of keystrokes. 71 00:03:46.500 --> 00:03:48.340 The only time where I would use an import like 72 00:03:48.340 --> 00:03:51.150 this is where it is very clear what's going on. 73 00:03:51.160 --> 00:03:55.610 In this case it's blenders user interface definition and there is only one header, 74 00:03:55.610 --> 00:03:56.710 there's only one menu, 75 00:03:56.710 --> 00:04:00.060 there's only one panel and they always come from the pilot types. 76 00:04:00.340 --> 00:04:02.660 So in this case I would find it acceptable 77 00:04:02.670 --> 00:04:05.450 in general though I would avoid this and always use 78 00:04:05.450 --> 00:04:09.280 the full name of the thing so that once you see it is clear where it comes from. 79 00:04:09.290 --> 00:04:10.610 Now that is out of the way. 80 00:04:10.620 --> 00:04:14.500 Let's just jump in and make her own panel for the monkey grid. 81 00:04:14.510 --> 00:04:17.560 Let's take a look at our adults code where we left off last time. 82 00:04:18.240 --> 00:04:20.250 So here we have the monkey grip that pie 83 00:04:21.269 --> 00:04:23.960 with the monkey grid operator class 84 00:04:24.540 --> 00:04:26.660 and the registered and unregistered functions. 85 00:04:27.040 --> 00:04:28.150 Now to this 86 00:04:28.400 --> 00:04:30.760 we can add our monkey great panel. 87 00:04:31.340 --> 00:04:33.350 So just like before we create our class 88 00:04:33.840 --> 00:04:38.130 we name it properly. I kept the category few three D. Because it's a view three D. 89 00:04:38.130 --> 00:04:41.360 Panel. Anyway we have to P. T. I called it monkey grid 90 00:04:41.740 --> 00:04:47.430 and we extend be piled up types that panel. I just copy these values from the three D. 91 00:04:47.430 --> 00:04:48.520 Cursor panel 92 00:04:48.660 --> 00:04:51.740 and into category monkeys so that we get our own little tab there 93 00:04:51.940 --> 00:04:53.950 and I labeled it grid 94 00:04:54.280 --> 00:04:57.160 and then the only thing that you really need to have is a draw function 95 00:04:57.440 --> 00:04:59.340 for it to show up as a panel. 96 00:04:59.340 --> 00:05:02.360 It needs to have draw function even if that function is nothing. 97 00:05:02.740 --> 00:05:06.210 And because the function in python always have to have something 98 00:05:06.570 --> 00:05:10.260 I put pass here which is python code for don't do anything. 99 00:05:10.360 --> 00:05:13.050 Every code block after a Colin is indented 100 00:05:13.050 --> 00:05:15.840 and every invented blog needs a little bit of 101 00:05:15.840 --> 00:05:18.220 text there in order to make sure that python 102 00:05:18.220 --> 00:05:20.410 understands that it's invented in the first place. 103 00:05:20.740 --> 00:05:22.860 So that's why they have the past keyword. 104 00:05:23.240 --> 00:05:23.430 Now. 105 00:05:23.430 --> 00:05:27.010 The only thing that's left is to tell blender that this class exists in the first 106 00:05:27.010 --> 00:05:29.410 place which means registering it so we can 107 00:05:29.410 --> 00:05:32.750 just copy the registered and unregistered class function calls 108 00:05:33.140 --> 00:05:38.520 and put in our new class and that should be it. Let's go to blender. See what happens 109 00:05:38.800 --> 00:05:40.660 now, all we have to do is reload the script 110 00:05:41.340 --> 00:05:45.450 so a good system. Reload scripts and you'll see monkeys pop up here. 111 00:05:45.940 --> 00:05:50.310 This route also gives you access to right click and then add to quick favorites. 112 00:05:50.320 --> 00:05:53.640 So if you do that then you can just press Q. And reload scripts 113 00:05:53.780 --> 00:05:57.660 will give you a fast way to reload them. When we go to the monkeys tab. 114 00:05:57.670 --> 00:06:01.000 We can see that our great panel is here. It doesn't contain anything yet. 115 00:06:01.000 --> 00:06:03.940 Of course we don't draw anything there. But at least it shows up. 116 00:06:03.950 --> 00:06:06.060 So let's put a button there for the operator. 117 00:06:06.340 --> 00:06:07.660 Let's go back to the code 118 00:06:08.040 --> 00:06:09.450 and here in draw. 119 00:06:10.040 --> 00:06:10.240 Yeah. 120 00:06:10.730 --> 00:06:13.550 Self taught layout lets us draw things. And to draw an operator, 121 00:06:14.140 --> 00:06:18.240 you type operator and then you give the idea name of the operator. 122 00:06:18.240 --> 00:06:19.360 You want to have their 123 00:06:19.940 --> 00:06:22.510 let's go to paste that in, go to blender. 124 00:06:22.680 --> 00:06:23.750 Rewrote the script 125 00:06:24.340 --> 00:06:25.740 and there you can see we have a monkey 126 00:06:25.740 --> 00:06:29.050 grip button in the grid panel and it actually works 127 00:06:29.440 --> 00:06:31.830 to customise this button. There's a few possibilities. 128 00:06:31.830 --> 00:06:33.650 And let's go take a look at them. 129 00:06:33.660 --> 00:06:37.660 First of all we can change the text that is shown on the button. 130 00:06:37.670 --> 00:06:41.460 So let's call it the default grid because pressing 131 00:06:41.460 --> 00:06:44.020 this button will just give us the default properties. 132 00:06:44.030 --> 00:06:46.450 Save the file, go back to blender. 133 00:06:46.840 --> 00:06:49.560 Reload the scripts and now it's called default grid. 134 00:06:49.570 --> 00:06:53.570 The nice thing is that if we search for the monkey grid then it's still called 135 00:06:53.570 --> 00:06:57.210 monkey grid in the F three panel because that is the name of the operator. 136 00:06:57.220 --> 00:06:59.990 It is just that on this particular spot in the 137 00:06:59.990 --> 00:07:02.580 user interface we have a different text on the button. 138 00:07:02.590 --> 00:07:06.250 Finally we can add an icon to it 139 00:07:06.840 --> 00:07:09.020 and then we have to know the name of the icons. 140 00:07:09.020 --> 00:07:11.600 So you can either look at existing code to cheat 141 00:07:11.600 --> 00:07:13.840 off of that or you can activate the icon, 142 00:07:13.840 --> 00:07:17.450 viewer add on it's bundled with blender but not activated by default. 143 00:07:17.460 --> 00:07:20.480 And then in the python cause all you get this button called icon viewer. 144 00:07:20.490 --> 00:07:23.090 Then you can click on the icon that you want to say a monkey. 145 00:07:23.100 --> 00:07:24.780 This is copied it onto the clipboard, 146 00:07:25.110 --> 00:07:27.260 paste it there, save 147 00:07:27.440 --> 00:07:31.160 go back to blender. Reload the script and there you have the icon 148 00:07:32.140 --> 00:07:32.960 in the tool tip. 149 00:07:32.960 --> 00:07:36.420 You can also see the description and you can see that 150 00:07:36.420 --> 00:07:39.730 it will actually call our operator with all the default parameters. 151 00:07:39.740 --> 00:07:41.830 Let's tweak this and make some buttons that you 152 00:07:41.830 --> 00:07:44.880 specific values for these parameters rather than the default. 153 00:07:44.890 --> 00:07:47.050 So again, we go back to the source, 154 00:07:47.840 --> 00:07:49.460 let's copy this call. 155 00:07:49.470 --> 00:07:56.110 And instead of just having this we can say props equals self layout of the operator, 156 00:07:56.110 --> 00:07:57.010 yada, yada yada. 157 00:07:57.020 --> 00:08:00.230 Let's change this to big grid. 158 00:08:00.240 --> 00:08:03.290 Self to lay out the operator is a function that returns something 159 00:08:03.300 --> 00:08:06.820 and the thing that it returns represented properties of our operator. 160 00:08:06.830 --> 00:08:10.160 So here we can say props dot and let's 161 00:08:10.640 --> 00:08:11.930 take a look. We had 162 00:08:12.280 --> 00:08:14.560 count X. Count y and size. 163 00:08:15.040 --> 00:08:15.250 Okay, 164 00:08:15.640 --> 00:08:17.650 so here we can save props dot 165 00:08:18.440 --> 00:08:20.160 count x equals 10 166 00:08:20.740 --> 00:08:23.130 and count y equals 10 167 00:08:23.710 --> 00:08:27.160 and size equals 0.8. 168 00:08:27.640 --> 00:08:31.550 So let's save. Go back to blender. Reload the script. 169 00:08:31.560 --> 00:08:34.690 We have a new button, big grid and when you have over it, 170 00:08:34.690 --> 00:08:39.159 you can see that it has different values for those parameters to lead the monkeys. 171 00:08:39.740 --> 00:08:43.530 If you click on big grid automatically, it will take those values of course. 172 00:08:43.710 --> 00:08:44.580 To top this off, 173 00:08:44.580 --> 00:08:48.860 we can also make a small grid and then we have a few more buttons to play around with. 174 00:08:50.040 --> 00:08:52.050 So for the small grid 175 00:08:52.840 --> 00:08:59.510 let's set count exceeds one count y is one and keep the size to the default. 176 00:08:59.510 --> 00:09:01.150 So we just don't mention it here. 177 00:09:02.540 --> 00:09:04.660 Save picture blender. 178 00:09:05.140 --> 00:09:05.950 Reload 179 00:09:06.540 --> 00:09:08.360 no word of a small grid as well, 180 00:09:08.840 --> 00:09:10.360 which is of course just one. 181 00:09:10.830 --> 00:09:14.570 But in the redo panel we can extend this however we want. 182 00:09:14.680 --> 00:09:18.030 Now you can also see the value of being able to set the text 183 00:09:18.030 --> 00:09:21.660 on that button because it's all the same operator but with different behaviors. 184 00:09:22.340 --> 00:09:23.460 Now there's one more thing. 185 00:09:23.460 --> 00:09:26.000 I don't like how these buttons are space so far apart 186 00:09:26.000 --> 00:09:28.670 from each other because they're basically doing the same thing. 187 00:09:28.670 --> 00:09:32.030 They're creating a monkey grid and they should really be grouped together. 188 00:09:32.040 --> 00:09:35.580 Fortunately we can do this by laying in the mountain a column and I 189 00:09:35.580 --> 00:09:38.340 can hear you ask but see when they are already in a column. 190 00:09:38.340 --> 00:09:39.260 So what are you on about? 191 00:09:39.340 --> 00:09:39.780 Well, 192 00:09:39.780 --> 00:09:42.530 a function to create a column has some parameters 193 00:09:42.530 --> 00:09:44.470 that we can use to influence the layout. 194 00:09:44.470 --> 00:09:46.060 So let's take a look at our code again. 195 00:09:46.140 --> 00:09:46.670 Right now, 196 00:09:46.670 --> 00:09:51.520 each operator is drawn by calling softer layout operator and we can change it. 197 00:09:51.530 --> 00:09:54.960 We can create a column first. 198 00:09:56.540 --> 00:10:01.360 And then instead of calling operator on self layout, we call operator on call 199 00:10:03.640 --> 00:10:04.560 safety code. 200 00:10:05.440 --> 00:10:06.780 Go back to blender. 201 00:10:06.790 --> 00:10:09.770 Reload the script and you can see they're already squeezed together a 202 00:10:09.770 --> 00:10:12.610 little bit more and we can go a little bit further, 203 00:10:12.620 --> 00:10:15.550 add align equals true here 204 00:10:16.440 --> 00:10:20.300 and then they are exactly as I wanted them to be nicely squeezed together. 205 00:10:20.380 --> 00:10:21.710 So beside the operators, 206 00:10:21.710 --> 00:10:24.160 you can also drop properties on the panel and this 207 00:10:24.160 --> 00:10:26.760 can be pretty much any property that you want. 208 00:10:26.940 --> 00:10:30.050 So you could use it to group together these properties that 209 00:10:30.050 --> 00:10:33.610 are strewn throughout lenders user interface but you use a lot. 210 00:10:33.620 --> 00:10:36.410 So for your specific workflow you can add them to your 211 00:10:36.410 --> 00:10:38.550 own panel and make your life a little bit easier. 212 00:10:38.740 --> 00:10:43.000 So let's take two properties. One property of cycles. 213 00:10:43.010 --> 00:10:46.320 So let's say the number of vieux port samples in cycles and 214 00:10:46.320 --> 00:10:49.630 the other I want to have relative to the currently selected object. 215 00:10:49.630 --> 00:10:52.370 So let's well the view port visibility there. 216 00:10:52.380 --> 00:10:56.120 So one of the properties is this guy here, the number of U port samples. 217 00:10:56.130 --> 00:10:58.040 And the other property that we want to add is this 218 00:10:58.040 --> 00:11:00.930 one here disabling vieux port before we go to the code, 219 00:11:00.940 --> 00:11:02.870 let's see what blender already tells us. 220 00:11:02.880 --> 00:11:06.990 So you hover over the view port property there, it says at the bottom of the tool tip, 221 00:11:07.000 --> 00:11:12.280 be pi data seen seen that cycles of previous samples and this is almost what we want. 222 00:11:12.290 --> 00:11:15.810 But if we were to use this we would hard code the name seen in our script. 223 00:11:15.810 --> 00:11:18.840 So if we ever rename the scene then our scriptures broken already. 224 00:11:18.970 --> 00:11:22.260 And also when you use multiple scenes, it also wouldn't work anymore. 225 00:11:22.340 --> 00:11:25.870 So rather than using this literally as it stands there, 226 00:11:25.880 --> 00:11:28.060 even though for this case it might work 227 00:11:28.440 --> 00:11:33.060 what we use the context again and just get the current scene with context not seen. 228 00:11:33.640 --> 00:11:35.550 So the property that we want to show is 229 00:11:35.550 --> 00:11:39.550 actually context dot scene dot cycles dot preview samples. 230 00:11:39.940 --> 00:11:43.360 Now, let's go to the code, let's add another column 231 00:11:50.030 --> 00:11:53.860 and then we call call dot prop to render the property for us. 232 00:11:54.240 --> 00:11:56.730 It takes a minimum of two parameters. 233 00:11:56.740 --> 00:11:58.540 The first one is the thing that contains the 234 00:11:58.540 --> 00:12:01.180 property and then it's the name of the property. 235 00:12:01.190 --> 00:12:04.470 So the thing that contains the property is context not seen, 236 00:12:04.480 --> 00:12:07.260 but cycles everything up to the last point. 237 00:12:07.740 --> 00:12:10.760 And then the name is preview samples. 238 00:12:11.140 --> 00:12:14.350 Let's save, Go back to blender. Rewrote the script 239 00:12:14.740 --> 00:12:17.290 And there you have it preview samples 10. 240 00:12:17.300 --> 00:12:19.400 So we can change this and you can see that here. 241 00:12:19.400 --> 00:12:22.600 It changes as well, which indicates that we have the right property. 242 00:12:22.670 --> 00:12:26.410 So here you can also add these texts and icon parameters. 243 00:12:26.420 --> 00:12:28.580 They work the same as with the operator call. 244 00:12:28.590 --> 00:12:32.760 So in order to keep this video a bit to the point, I'll leave that for you to toy with. 245 00:12:33.340 --> 00:12:35.780 Now let's take a look at the second property we wanted 246 00:12:35.780 --> 00:12:38.990 to add which is a property of the active object. 247 00:12:39.000 --> 00:12:43.150 I want to be able to control the view port, visibility. So that is this guy over here 248 00:12:43.540 --> 00:12:47.000 and there it says be pi data object. Suzanne dot hide view port. 249 00:12:47.010 --> 00:12:51.460 And again, is with the scene. We don't want to do this specifically for Suzanne only. 250 00:12:51.470 --> 00:12:53.010 We want to do this for the currently 251 00:12:53.010 --> 00:12:56.400 active object which means context dot active object. 252 00:12:56.410 --> 00:13:01.150 So the property we're going for is context active object dot hide view port. 253 00:13:01.940 --> 00:13:05.360 Let's just copy what we had. 254 00:13:05.740 --> 00:13:10.150 Change the thing that contains the property to context active object 255 00:13:10.540 --> 00:13:11.050 and 256 00:13:12.540 --> 00:13:16.310 change the property name to hide view port. And this should render that property. 257 00:13:16.330 --> 00:13:19.160 Let's take a look like a blender. We reload 258 00:13:19.260 --> 00:13:21.660 and there is the property that we wanted to have. 259 00:13:22.540 --> 00:13:23.950 Now let's see if it works 260 00:13:24.940 --> 00:13:29.760 and there you go, it's working. But it's a bit weird because it disappears 261 00:13:30.140 --> 00:13:32.030 and here it doesn't. So what's going on? 262 00:13:32.040 --> 00:13:35.190 It turns out when you hide an object is no longer active. 263 00:13:35.200 --> 00:13:40.370 So that means that context, not active object becomes none. Instead of Suzanne. 264 00:13:40.690 --> 00:13:45.490 None doesn't have a property hide report. So our code is actually causing problems. 265 00:13:45.490 --> 00:13:45.900 Now 266 00:13:46.010 --> 00:13:49.340 let's take a look at the terminal where started blender And here you see rather 267 00:13:49.340 --> 00:13:54.160 cryptic error message here at the bottom is the terminal where is to blend it from 268 00:13:54.840 --> 00:13:58.530 And it's complaining about the monkey great pie file line 269 00:13:58.540 --> 00:14:02.160 82 which is of course the call that we just added 270 00:14:02.740 --> 00:14:08.760 And it says ever with argument one. Data. So apparently this argument is called data 271 00:14:09.640 --> 00:14:12.570 function. That data does not support a non assignment. 272 00:14:12.570 --> 00:14:16.360 It's a bit vague description, but it does match what I just said. 273 00:14:16.370 --> 00:14:21.130 The active object is none and you cannot draw a property of nothing. 274 00:14:21.140 --> 00:14:25.660 So we have to add a little bit of a guard around this code to make it always work. 275 00:14:27.430 --> 00:14:28.460 We know that 276 00:14:28.840 --> 00:14:33.460 we know that contacts the active object can become none. So let's check for it. 277 00:14:33.460 --> 00:14:34.750 And in that case 278 00:14:35.340 --> 00:14:43.220 let's just add a label. Otherwise we'd rather property as we did before. 279 00:14:43.230 --> 00:14:46.650 Let's see how this works. Now. Reload the script 280 00:14:48.840 --> 00:14:51.960 and there you have it. It says no active object instead of 281 00:14:52.080 --> 00:14:54.970 going out and all the code will keep working. 282 00:14:54.980 --> 00:14:57.060 You could argue that this is a bit of a silly 283 00:14:57.060 --> 00:14:59.720 button because it only works once and you will be right. 284 00:14:59.730 --> 00:15:02.440 But I wanted to have this example to show you 285 00:15:02.440 --> 00:15:06.060 how things can go wrong and how to resolve that. 286 00:15:06.140 --> 00:15:08.600 It's also a demonstration of one thing that I find quite 287 00:15:08.600 --> 00:15:12.420 important and that is showing why something is not there. 288 00:15:12.430 --> 00:15:16.550 So rather than not drawing the property at all and just silently skipping over it. 289 00:15:17.040 --> 00:15:22.550 Now we draw the text, no active object which indicates to whoever is using your code, 290 00:15:22.590 --> 00:15:24.850 why the option is gone. 291 00:15:24.930 --> 00:15:28.690 I think that's quite important. So that's why I wanted to have this example here. 292 00:15:28.700 --> 00:15:30.890 Anyway, even though it's rather useless. 293 00:15:30.900 --> 00:15:33.030 You now know how to draw your own properties. 294 00:15:33.040 --> 00:15:36.150 So it's up to you to make it useful for your particular workflow 295 00:15:36.440 --> 00:15:37.930 before we end this video, 296 00:15:37.930 --> 00:15:41.560 I want to show you one more thing which is adding operators to menus, 297 00:15:41.740 --> 00:15:44.560 given what we've seen so far. This is rather easy. 298 00:15:44.560 --> 00:15:46.660 So this is why I kept it as the last step. 299 00:15:46.670 --> 00:15:50.420 Let's add our monkey grid operator to the mesh, add menu. 300 00:15:50.590 --> 00:15:52.060 So here we have add 301 00:15:52.540 --> 00:15:56.280 mesh and it would be nice if underneath monkey there was a monkey. Great. 302 00:15:56.290 --> 00:15:59.870 This means we have to add it to a menu and there's a trick for that. 303 00:15:59.880 --> 00:16:04.340 I will show you but first we have to know which menu it is so you can hover here or 304 00:16:04.350 --> 00:16:07.740 add and it shows view three D Empty AD And 305 00:16:07.740 --> 00:16:11.050 you probably already guessed empty stands for menu type. 306 00:16:11.340 --> 00:16:13.950 Now the thing is sub menus don't show this, 307 00:16:14.640 --> 00:16:16.510 You don't know what the measure is, 308 00:16:16.520 --> 00:16:20.930 but we can look around in the python console and see if we can find it. 309 00:16:20.940 --> 00:16:26.760 Anyway, it is a type. So it is part of be piled up types 310 00:16:27.340 --> 00:16:33.660 dot few three D. Empty. And let's see if we can find something 311 00:16:35.940 --> 00:16:36.150 right. 312 00:16:37.140 --> 00:16:42.080 There you go. Mash add. So this is very likely to be our menu. Calling it here. 313 00:16:42.080 --> 00:16:44.490 We'll just give an error. So let's not do that. 314 00:16:44.500 --> 00:16:47.990 But at least we have the name now few three D Empty. 315 00:16:48.000 --> 00:16:53.160 Mash add in be piled up types, let's copy this and then go to the code 316 00:16:53.440 --> 00:16:54.540 as you would have guessed it. 317 00:16:54.550 --> 00:16:58.060 We have to do something in the register and then the unregistered functions 318 00:16:58.340 --> 00:17:01.730 and this time is quite different than what we've seen so far. 319 00:17:01.740 --> 00:17:04.910 First we have to make our own function that will draw whatever we 320 00:17:04.910 --> 00:17:08.060 want to add to the menu so that looks something like this, 321 00:17:08.440 --> 00:17:16.540 the name that you can think of yourself and again self comma context 322 00:17:16.550 --> 00:17:19.970 for now let's put pass in there to make points and happy. 323 00:17:19.980 --> 00:17:24.420 And this function basically will be called after the draw function of that panel. 324 00:17:24.430 --> 00:17:28.359 So where our own panel has this draw function with self and context. 325 00:17:28.940 --> 00:17:32.500 This function will be called with the same self and the same context. 326 00:17:32.500 --> 00:17:35.250 So self will refer to that menu in our case. 327 00:17:35.260 --> 00:17:38.300 Now let's register and unregistered er this function 328 00:17:38.310 --> 00:17:40.100 this is done by taking that beep. 329 00:17:40.100 --> 00:17:42.670 I types few three D. Yeah really? Era 330 00:17:43.140 --> 00:17:45.110 and saying dot append 331 00:17:47.020 --> 00:17:50.050 at menu draw. The opposite is removed. 332 00:17:50.540 --> 00:17:52.010 So when the unregistered 333 00:17:53.640 --> 00:17:58.050 we remove mesh add menu draw now what to put in here. 334 00:17:58.440 --> 00:18:05.270 Well that's the same as with the panel we can just call self not layout dot operator. 335 00:18:08.640 --> 00:18:10.390 Okay the idea of the operator 336 00:18:10.520 --> 00:18:11.730 and this should work 337 00:18:11.880 --> 00:18:13.560 let's save and go back to blender. 338 00:18:13.940 --> 00:18:14.960 Reload the script, 339 00:18:15.740 --> 00:18:19.950 add mesh and there we have monkey grid, it doesn't have an icon yet 340 00:18:20.060 --> 00:18:23.440 but now you know how to add one. So I'll leave that for you 341 00:18:23.560 --> 00:18:23.800 now. 342 00:18:23.800 --> 00:18:25.440 You've seen how to add your own panel, 343 00:18:25.450 --> 00:18:27.900 how to add your own operators to existing menus, 344 00:18:28.020 --> 00:18:31.670 but most importantly, you've seen how to look at other code. 345 00:18:31.940 --> 00:18:34.540 So when you see something that's interesting, when you're wondering, 346 00:18:34.550 --> 00:18:36.170 how did they do that? 347 00:18:36.240 --> 00:18:39.780 You can just right click edit sores and most of the user interface 348 00:18:39.790 --> 00:18:43.560 elements in blender are written in python and then this just works. 349 00:18:43.570 --> 00:18:46.630 So this is it for this episode of scripting for artists. 350 00:18:46.720 --> 00:18:48.580 If you have any questions or remarks, 351 00:18:48.580 --> 00:18:51.170 please leave them in the comment below and I will see you soon.