Hey there, welcome to Scripting for Artists.
This is a video tutorial series on Scripting Blender.
It is aimed at people
who don't necessarily want to be a programmer,
but for whom Scripting may be beneficial.
And it can be useful for a lot of things,
as we'll see soon.
Before we start, let me introduce myself.
My name is Sybren Stuvel.
I'm a core Blender developer and
I worked at the Blender Institute.
I've experience working next to artists.
I've worked on the Blender Cloud itself,
and I've worked as a tractor in Flamenco.
I also have a PhD in computer animation,
during which I worked on the Blender game engine.
After that I started working here
at the Blender Institute.
This series is about Scripting Blender.
Basically
letting Blender pushing its own buttons.
If you have ever done any dump monkey work that
takes a lot of clicks and a lot of repetition,
this may be the series for you.
Examples for Scripting would be useful.
Our mass naming of bones or objects,
or rendering 10,000 OBJ files in some directory.
One thing to keep in mind is that
computers are stupid. They will just do
what they're told, line by line, word by word,
and not really think about what you mean.
So
it's good to keep in mind that
you have to be a literal.
So that's it for the introduction of this series.
Let's go and do something practical
in Blender itself.
Blender can be programmed in Python.
That's quite a nice language
that can be read and understood quite easily.
Furthermore, it allows you
to interactively toy around with it.
So let's dive into Blenders Python console,
"after I change my shirt", that is.
So here is the default startup file.
We can replace the timeline with the Python console,
because we don't need the timeline right now.
Let's do some computations,
3 x 4 = 12, 2 + 3 x 4 = 14,
because multiplication and division
go before addition and subtraction.
So if you want to do the addition first,
you have to use parentheses,
(2 + 3) x 4 = 20.
Let's go and see if we can do something
to that default Cube.
If we press N in the Viewport for numeric data,
you can see locations X Y Z.
Let's move it somewhere else.
So if you hover your mouse over the locations,
you can see it in Python information,
bpy.data.objects.cube.location.0,
which basically means from
Blender's Python Interface, get the data, in there are objects,
from those objects, I want to have cube,
cube has a location,
and location has a few components.
X is component 0, Y is component 1
and Z is component 2.
We can just copy this into our Blender Python console,
bpy.data.objects.cube.location.0
and this gives us the X location,
and location 1
gives us the Y location, and
of course 2 will give us Z.
Alternatively you can use letters.
So there is actually a ".X", do exactly the same thing.
I like those little bit better.
It makes your script a bit easier to read
if you just name them X Y and Z.
This isn't limited to reading,
we can also write to it.
So we can set ".Z" to 0,
and then is (0)
or 1
or Alt + 0
which means increment.
Increment by 0.1,
and we can keep using this.
I'm pressing the up arrow key in the console
that gives me the previous command,
and then press "enter", so you don't have
to type everything by hand again.
We can also assign to the location directly
instead of to each component individually.
By removing the last component and then
assigning it three numbers in parentheses.
For example like this,
and then we set X Y Z at the same time.
This also worked for Scale.
And it also works for the Rotation.
Except that on the Python side of things,
Rotations always work with Radiance,
and not with Degrees that you might see in the UI.
So this might get
a little bit getting used to.
One more thing that may make things
a little bit easier to type is that
the Python console supports auto-completion.
So you can do "bpy.date" and then
press Ctrl Space ".object.cube",
you have to type yourself,
bu then "rotation" and then "euler".
So this is done with Ctrl Space,
can be really useful.
Can also help you figuring out what is there to type
in the first place.
So there's a whole list of things here.
This you can find in the documentation by the way.
If you go to Help, and then Python API reference,
then you get all these information.
But we'll cover that in a moment.
Let's look at some other things.
We can find about Python in Blender User Interface.
We have seen these properties of existing objects.
Let's see if we can delete
and maybe create some new objects.
This is done with operators,
operators are Blender's way of binding certain functionality
to menu items or buttons in the interface.
So what we can do is just hover over Button,
and then you see the operator command.
You don't even have to remember it.
You can just press Ctrl C to copy,
and then press Ctrl V to paste it into the console,
and it will just work.
So now we've deleted an object,
let's look at creating one.
You press Shft A, then over an operator
hover your mouse and you see
"bpy.ops.mesh.primitive UV sphere add".
So again, press Ctrl C and then in the console,
press Ctrl V and "enter"
and there, you have your sphere.
It's a bit boring one, is the default one.
Let's see if we can do something about that.
Don't be afraid,
if you don't understand all these options yet,
I will give you some information later
where you can find out exactly what they do.
So let's create it
with size equals 0.10
at location 1 2 3.
And there you have a really tiny sphere.
Let's create a bit bigger one at a different location.
It's that simple really.
We've already found quite a bit of information
in Blenders User Interface,
but there is also Blenders API documentation,
which you can find right
here in Blender, Python API reference.
It opens the browser for you with information,
and you can
use that copy paste trick again.
So Let's see,
Shft A, Mesh, UV Sphere, Ctrl C,
then I go to the browser and hit Paste in the quick Search,
click on the first link,
and there you have the explanation of what is what.
Another good source of information
is the Python standard library.
So what have we seen so far?
We know that computer is stupid
and just do the commands one by one which we gave them.
We've seen how to use the Python console,
and how to create objects, remove objects,
and modify existing ones.
We've seen what we can do with
copying information from Blenders User Interface.
And it points you to a few sources of information
where you can find more stuff.
So in the next video, we will look at
Names and Objects because typing
bpy.data.objects.cube.something.something.something
becomes really boring after a while.
See you in the next video.