Hello and welcome to scripting for artists.
My name is Simon 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 her 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 if you wanted to scratch their.
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 ad on Israeli 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?
Well look at ingredients of an add on,
we'll turn our monkey red operator into a monkey bread 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 dot pie
and there really is nothing more to it.
Editing Dakota is also simple,
you can pretty much use any text editor out there just don't use a word processor
like open office or or Microsoft word because
those are really not for just plain text.
You may be tempted to use blenders 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 blenders. Editorial was get confused.
I never know whether I'm editing a tax file that is part of
blender or that is externally safe to disk to save the final.
You have to press all s and not control?
S otherwise she saved the blend file itself, 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 at them 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 if you give them all the tries a week you like best
as for the name of the cold file. I already mentioned that it has two ended up. I
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 adults,
you can go to help manual and their go to advanced scripting and extending blender
and there's a lot of info there, including an adult tutorial.
Now let's look at the ingredients of an add on.
There are three things that you really have to have
in your add on at the top of the final.
There should be some metadata in a bill info addict and this metadata should include
the name of the add on which version of blender is compatible with it,
the category, the adult should be in and some other info.
The 2nd and 3rd thing you need to have,
you've already seen and that's the registered and unregistered functions.
These functions tell blender what your adult contains so
that it can be loaded and unloaded properly.
The bill info you see at the top is called
a dictionary and this is basically a look up 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 render
will copy it into the final location and we can edit it from there later.
So I'm going to send you to more scripting for Artists folder and amusing only
lower case letters and underscores for the name ending the name in the pie.
Yeah.
Now we have to install the add on. Go to user preferences,
go to add ons,
install
and find our smallest add on.
Now he had on is installed and blender copied it for us to the right location.
You can see we're here.
So it is my home directory dot com fix left blender slash the version of blender
scripts add ons the
smallest add on top. I
we could have manually put it there. That's all fine.
As long as the finest there is good. But this way blender tells you where it has to go.
You can see that 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,
overwriting 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 the python file is.
You can import it.
So you can say import smallest add on. And these loads are adam.
You can see that because we have a registered function for example that Prince Hello
world and we have an unregistered function that says goodbye World as we expected.
So this gives you access from lender to your add on and their 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.
Actually open the file.
So here we are in visual studio code and let's open the file
and of course the file open dialog is very platform specific.
In my case I can just paste the file name with Contrave,
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 in predict 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 bill info.
There are some more of the fields that I just copied from that documentation
location is the location of your adam where people can find it in blender itself.
Since we won't have any user interface yet.
We can just type operators search here and
then people will have to find for themselves,
description just describes the functionality if you add on more monkeys.
The other fields will leave empty for now.
I've saved it with control s or back at the user
preferences in blender and we have changed the final desk.
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 type there
are 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 say 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 adults directory on the
side so you can see I've blended cloud add on
installed and our smallest adult that we just made.
So let's make a new one,
create a new file called monkey create dot pie from my smallest add on.
I copy the bill info into it. I changed the name to
Monkey grid because it's not a monkey. Great add on. It's going to add new measures.
So let's change the category two mesh location is still operate a surge.
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 reveal info at the top.
And now all we need to do is copy all the code
that we already had in that land file into this file.
So back at blender
I copy all the code
and the 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
impressive. Three
monkey grid and then we are our own adult 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 adam's list
but that only refreshes the adults list.
Does not actually reload your adam from disk once it's been loaded.
But first let's make a change so that we can see
that reloading actually did something back in the code editor.
Let's say we change the default value of count. Y
Change it from two 25 and save the file back in blender.
Of course if we redo the monkey grid,
you still see too as the default value because we haven't reloaded the script yet.
Press F three. Reload scripts that will actually reload your script.
And now when we do 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 and add on is
just a tax file that has a specific name in a certain location on your computer.
It has metadata at the top in the bill 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