 Hello and welcome to Scripting for Artists, my name is Sibren and in this episode we'll be looking at creating your own add-on. In the previous video we took our existing code to create a monkey grid, added some stuff around it to turn it into an operator and in this video we'll do basically the same, we take our existing operator, we add some code around it, save it to a different place and then we'll have our add-on. Up to now our code has always been inside the blend file itself and this is perfect for situations where it is specific to that blend file, it was one itch you wanted to scratch there. However, this is not always useful because you maybe want to use it in different blend files as well, you always want to have it available to you and maybe you even want to share your code with colleagues or with friends and in all these cases turning your code into an add-on is really the right choice. In this video we'll look at the following, we'll look at what is actually a code file and how do you edit it, we'll look at ingredients of an add-on, we'll turn our monkey grid operator into a monkey grid add-on and finally we'll be looking at something you will be doing a lot which is reloading your code after editing it. So let's get started, what is a code file actually? Well, you may have guessed it, it's actually just a plain text file with a name that ends in .py and there really is nothing more to it. Editing the code is also simple, you can pretty much use any text editor out there, I just don't use a word processor like OpenOffice or Microsoft Word because those are really not for just plain text. You may be tempted to use Blender's own internal text editor for it, after all it even has syntax highlighting for Python that means that it has some basic knowledge of Python, it knows the keywords like for and if and death and it can color them differently making it easier for you to understand the structure of the code. But with Blender's editor I always get confused, I never know whether I'm editing a text file that is part of Blender or that is externally saved to disk. To save the file you have to press alt S and not control S otherwise you save the blend file instead of the text file. I would rather recommend something different, something more suitable for the task. My personal favorite is Visual Studio Code, it's an open source project by Microsoft and it's a really nice editor. Other popular choices are Atom or Sublime Text or if you want to go really big pie charm. There's lots of choice out there so I recommend just download a few, give them all a try, see what you like best. As for the name of the code file I already mentioned that it has to end in a pie, there are a few more requirements though, you don't have to know all of them, if you just stick to letters without any accents, underscores and numbers you'll be fine. If you want to know more about creating add-ons you can go to help, manual and there go to advanced scripting and extending Blender and there's a lot of info there including an add-on tutorial. Now let's look at the ingredients of an add-on. There were three things that you really have to have in your add-on, at the top of the file there should be some metadata in a BL Info dict and this metadata should include the name of the add-on, which version of Blender is compatible with it, the category the add-on should be in and some other info. The second and third thing you need to have you've already seen and that's the register and unregister functions. These functions tell Blender what your add-on contains so that it can be loaded and unloaded properly. The BL info you see at the top is called a Dictionary and this is basically a lookup table. We've seen it before in chapter five of scripting for artists. Okay, let's get this miniature add-on to work. First we have to save it to a location on disk. It doesn't really matter where right now because we will install it and then Blender will copy it into the final location and we can edit it from there later. So I'm going to save it to my scripting for artist folder and I'm using only lowercase letters and underscores for the name ending the name in .py. Now we have to install the add-on. Go to user preferences, go to add-ons, install and find our smallest add-on. Now the add-on is installed and Blender copied it for us to the right location. You can see where here. So it is my home directory.config slash Blender slash the version of Blender scripts add-ons smallest add-on.py. We could have manually put it there. That's all fine as long as the file is there is good. But this way Blender tells you where it has to go. You can see that the user interface shows very little information about our add-on. So before we even enable our add-on, let's see if we can fix that. We could go back to the Blender text editor, change the file, save it again, install it again over writing the file we just installed and then click refresh. But I think that's too much work. So let's just take this file that you see here and open that in a code editor instead. So here's a little trick for you. If you want to figure out where a Python file is, you can import it. So you can say import smallest add-on and this loads our add-on. You can see that because we have a register function for example that prints hello world and we have an unregister function that says goodbye world as we expected. So this gives you access from Blender to your add-on and there's a secret name there that doesn't just show up underscore underscore file underscore underscore and that will give you the file that the add-on is stored in. So this you can copy paste into your editor and actually open the file. So here we are in Visual Studio Code and let's open the file. And of course the file open dialogue is very platform specific. In my case, I can just paste the file name with Ctrl V, press enter and then I open the file and here we see the code that Blender just installed for us, it is our code file. Now let's extend that deal infodict with some more information. First let's add an author field. You can add an email address if you want that way people can easily read you. The rest of the fields are documented in the Python API manual. So in Blender just go to help Python API and then search for BL info. Here are some more of the fields that I just copied from that documentation. Location is the location of your add-on where people can find it in Blender itself. Since we won't have any user interface yet, we can just type operator search here and then people will have to find it for themselves. Description just describes the functionality of your add-on. More monkeys, the other fields will leave empty for now. I've saved it with Ctrl S. We're back at the user preferences in Blender and we have changed the file on disk. So let's click the refresh button. You can see that it collapsed this panel again, which is an indication that it was reloaded and when we open it, we get the info that we typed there. Our registered and unregistered functions don't do much, they just print something to the screen so let's see what happens. I show the terminal here and when we enable the add-on, you can see Hello World. Of course, when we disable, you can see Goodbye World and this just keeps going and going. So now we know everything that's needed to turn our monkey grid operator into a monkey grid add-on. So we're back in Visual Studio Code and you can see that I've opened the add-ons directory on the side. So you can see I have a Blender Cloud add-on installed and our smallest add-on that we just made. So let's make a new one, create a new file called monkeygrid.py. From my smallest add-on, I copy the BL info into it. I change the name to monkey grid because it's not the monkey grid add-on. It's going to add new meshes so let's change the category to mesh. Location is to operator search. The description is now even more applicable than ever. Let's keep it all like it is now. Next to what we had, this is the only thing we need. We have a code file that's in the right location that has the right name that has to write BL info at the top and now all we need to do is copy all the code that we already had in that Blend file into this file. So back at Blender, I copy all the code and in Visual Studio Code, I paste it in. Just a quick check that there is nothing weird. This we can remove because we're no longer running it from the editor itself, but only loading it as an add-on, we no longer need that little bit of magic. Save the file and now we go back to Blender. Let's start with a new file just to be sure and then go to the user preferences again. Refresh the list, search for monkey and there we have our monkey grid. We can enable it, let's see if it works. Press F3, monkey grid and then we are. Our own add-on is working. It's nice that things are working but things never are perfect in the first run. So what you will do is often go back and forth between your code editor and Blender itself and then you have to tell Blender to reload your scripts. We've already seen this refresh button in the add-ons list but that only refreshes the add-ons list. It does not actually reload your add-on from disk once it's been loaded, but first let's make it change so that we can see that reloading actually did something. Back in the code editor, let's say we changed the default value of count y, change it from two to five and save the file. Back in Blender, of course, if we redo the monkey grid, you still see two as the default value because we haven't reloaded the script yet. Press F3, reload scripts, that will actually reload your script and now when we do a monkey grid, you can see that the default has been set to five. So this is something you will do a lot. So that is it for this episode of scripting for artists. To recap, an add-on is just a text file that has a specific name in a certain location on your computer. It has metadata at the top in a BL info dictionary and it has a registered and unregistered function at the bottom. And finally, reloading was done with a reload script operator. So this is it. If you have any questions or remarks, please leave a comment below and I will see you soon.