 Hello everyone, as you've heard my name is Mark Zoploch and I'm having a presentation on topic of the Python API and Blender is a really sophisticated piece of software. It basically blends literally all of the tools in one and you can do compositing 3D editing animations video editing, basically anything you can imagine with multimedia you can do with Blender But there is a catch to this You take Blender, you start learning it and there's like tons and hundreds of Keyboard shortcuts and it's really hard and tough to learn them all and The nice thing about Blender's API is that Basically anything you can do in Blender you can do with the Python API and Sometimes even more things because you can basically touch the internal data inside of the Blender So one of really nice things About Blender is that if you point your mouse cursor basically at any element inside of the GUI It will tell you how do you access it in Python? So for example, if I wanted to add an empty Or wait, let's add Lamp and Sun it will tell me that in Python I can do it using this operation and and What we will look at today is some examples of what you can do why it's interesting and I think that many people don't know about this that you can actually do it in a Blender But every time I show to someone they go all like oh wow, I didn't know that it's gonna make my life so much easier So if you're interested in Blender These things may help you Automatize things you you need to do like with every single project It it can help you build new modeling tools it can help you with animating stuff and Sometimes it's really easier to just write a script for To spin something rather than edit the paths and use curve And stuff like that really inside of the blender manually So let's get to it blender has quite a Lot of modes you can use it in There are some presets to make your job easier when you do some basic stuff That's good. That's what the default is good for and they have for animations compositing stuff like that, so We are going to switch to scripting mode and now on this left side I Have a text editor like where I can run where I can write my scripts. I can save them into a file For it for them to be persistent and down here. We have a normal Python Python console Which is interactive you can try stuff, but Once you finish writing it. It's lost You cannot open it somewhere else cannot save it anywhere, but it's good for the sake of testing Like what's this thing going to do? What's that thing going to do? So one very basic thing you can do is using The bpy module which basically stands for a blender Python It's already imported in this console I'm going to make the text larger so you can read it. Can everyone in the back read it All right, thank you so in this module there is There are a few categories Which make it easier to orient inside this large complex API and Most of the time you're interested in operations the ops Context and data The rest is for really specific things But overall you spend most of the time in these three sections so We can take a look at ops and there is category mesh. Oh, sorry and Mesh is basically what everything most of the things in blender are made of whenever you build an Object, it's made of mesh so For example this cube right here if I'm going to edit it manually I can select this vertex grab it deform the cube and do things to it But Then there are really complicated meshes which have like thousands of vertices and These meshes are usually used for animating of people or Animals basically anything You'll see in the animated movies. They are all made of meshes Which is quite mind-blowing sometimes when you'll see how realistic something is and it's all made of little points and in mesh With operations for meshes you can add new meshes Deform them transform them. So We are going to add Primitive cube now we have a new cube in our scene and All right, this is boring, right This function takes arguments for example where to place the cube so you can do Is it important it's not so let's import random and For e in the range For example, 255 55 We are going to and over here. We can see what arguments this function can handle and Currently we are probably interested in the position one which is located right over here Cation Alright, so it takes a tuple of three values and These three values are basically coordinates in the space the x y and z and z values for this The rule of right thumb is used where the right thumb is the x axis the index finger is the y axis and the z finger is the Z axis so it grows towards you in most cases in blender not for whatever reason So if we want to add a new cube, let's say we want a Random random in and let's say We want 255 divided by three, so it's not as large, but it's more dense on each other and It might this might see Like really dense, but given that there are three spatial dimensions is going to be really far far apart So it actually helps to do it more dense so you can actually see the cubes not so far away from each other and And now it should add More cubes for whatever reason it does not this is the problem with Life-coding really whenever you want to do something. It just doesn't work So I'm missing one over here and now There is missing one position argument B. Oh, all right from zero to the value By three. Oh, let's do the four again and add the cubes in Well, it's in text. Oh I have zero in a Sorry for that All right, this should work right and now we should have many cubes all around Thank you and And actually if you made this If you made these for example asteroids, you now have The asteroid belt in the space and you can model ships and you have basically a nice space scene without any work So this is for example, one of the use cases I would use this for or if you want it I Just do I don't know make some random shafts for some horror movie you you could generate the pattern quite randomly, so it actually feels like The shafts are narrow and Random So I can imagine early lots of use cases in animations for the for this I'd love to use it someday, but a blender is really a time-consuming thing and It's really quite hard to get to it sit to it for 10 hours and create something Because it's the rule of one detail. It just sit to do one Small detail and you lose all day, right? So Now that we have lots of cubes. We can go back to Our two small cubes over here. I'm gonna delete this one Another really useful thing in this API is that it holds context the Vpy has this context section which basically manages the things That are Somewhere around there. So for example the current scene Selected objects Things you currently work with and you might use and find useful So for example when I take context I use my scene This thing over here is called scene In blender you usually per project create one scene where you put Objects and you work with them and you made them right We are ten minutes to the end and In this in the scene it's Reflected also in the API. So everything is in the scene all of the objects lights and You can manipulate them through this. So for example, there is a whole list of objects It's actually dictionary, but you can go also Through them using indexes And you can go through them using names. So for example here is cube to five six and Like that You for example if you need to move all seen to the left to for example Create earthquake and move everything to the left and to the right you can just select all the objects So for example, if I wanted to select this cube, I would just do dot select equals true and Some of those two hundred and fifty six cubes got selected. Please believe me Another interesting thing Is for example, bpy dot data Where you can manipulate basically the internals of blender? so you can create mesh out of Individual vertices which is quite useful when you want to do some Shape that is easily described mathematically, but would be really terrible to create manually So for example, if you want to create Sine wave it it could be out of vertices. It could be quite challenging to do by hand but Using the API and some mathematical module You can just add the vertices connect them add them to a single object and then You've got a sine wave so To demonstrate how creating of such an object Goes We can create for example a list of vertices I'm going to zoom in Delete this cube Let's create our vertices Vertices are just lists of individual points in space. So that means you create lists of lists of values I've tried to use tuples, but it seemed to crash blender So I'm staying away from that now because I don't want to crash my blender currently But I think it's cleaner to use tuples when you can but since there is a risk to it I'm going to use list and Let's say the first point of the triangle is going to be at the origin Which means where these axes meet it's this cursor Next point where we're going to create on the right Next point let's create on the top and now we have a normal Python object which represents list of lists of numbers Now We might want a mesh So let's create our mesh Bpy data Mesh is new and Let's name it triangle mesh Now this mesh is Empty it doesn't have any data. It doesn't have an any vertices, but since now we have our List containing these data we can create we can use this newly created mesh And update it from Python data and it even has nice documentation string to it which tells you what you can do and It takes The edges faces Sorry, sorry, sorry vertices edges and faces You might want to define edge in a case where you want two points Connected with a single line So for example the sine wave could be an example of that But for triangle we only want one face and it will automatically get the edges from and to the points and The the face is defined by the indexes of the Vertices in the mesh so zeroes first And second and now There is a problem That and has no land. Oh, yeah It's a list of faces so it has to be a list of lists of numbers again And now the mesh is updated But we still cannot see anything and that is because we don't have any object in the scene So first let's create an object Again in Bpy data We can we can take objects and create a new object call it the actual triangle Let's call it obj Require permit object data is not specified. Oh, and it needs also the object data, which is the mesh We have just created so let's just give it a mesh Now there is an object the actual triangle somewhere in the memory But we can see it like that. We want we want it in the scene so we take our scene We use of our context because it's it's this particular scene we are working with right now and We use the scene objects and Link it to it and now we give it our object and the triangle is there so This was a nice demonstration of the Python API. I think any questions anything I should show Before we finish a more complicated sample how to use the API Did I understand it correctly so far? I haven't used the API in any complicated way Because Blender is just my hobby and sometimes I do a render for example my wallpaper here is what I've made by hand but I've had a video sometimes somewhere where I created planets that were orbiting around the Sun and it looked really Nice so I think this for example something You can use it for But I don't have it here. I'm sorry for that, right? So I think that's it If you want to chat about the API see some other examples or Or just want to have a chat feel free to stop by. Thank you