﻿WEBVTT

00:00:00.880 --> 00:00:02.400
Hey there, welcome to Scripting for Artists.

00:00:02.400 --> 00:00:04.600
This is a video tutorial series on Scripting Blender.

00:00:04.600 --> 00:00:06.600
It is aimed at people

00:00:06.600 --> 00:00:08.480
who don't necessarily want to be a programmer,

00:00:08.480 --> 00:00:10.680
but for whom Scripting may be beneficial.

00:00:11.800 --> 00:00:13.480
And it can be useful for a lot of things,

00:00:13.480 --> 00:00:14.600
as we'll see soon.

00:00:15.000 --> 00:00:16.800
Before we start, let me introduce myself.

00:00:16.800 --> 00:00:18.480
My name is Sybren Stuvel.

00:00:18.480 --> 00:00:20.080
I'm a core Blender developer and

00:00:20.080 --> 00:00:22.000
I worked at the Blender Institute.

00:00:22.000 --> 00:00:23.200
I've experience working next to artists.

00:00:23.480 --> 00:00:25.200
I've worked on the Blender Cloud itself,

00:00:25.200 --> 00:00:26.400
and I've worked as a tractor in Flamenco.

00:00:26.400 --> 00:00:28.480
I also have a PhD in computer animation,

00:00:28.480 --> 00:00:30.480
during which I worked on the Blender game engine.

00:00:30.480 --> 00:00:32.280
After that I started working here

00:00:32.280 --> 00:00:33.480
at the Blender Institute.

00:00:33.480 --> 00:00:36.000
This series is about Scripting Blender.

00:00:36.000 --> 00:00:37.600
Basically

00:00:37.600 --> 00:00:39.880
letting Blender pushing its own buttons.

00:00:39.880 --> 00:00:42.080
If you have ever done any dump monkey work that

00:00:42.080 --> 00:00:43.680
takes a lot of clicks and a lot of repetition,

00:00:44.800 --> 00:00:46.280
this may be the series for you.

00:00:46.800 --> 00:00:48.600
Examples for Scripting would be useful.

00:00:48.600 --> 00:00:52.000
Our mass naming of bones or objects,

00:00:52.000 --> 00:00:54.080
or rendering 10,000 OBJ files in some directory.

00:00:55.000 --> 00:00:56.400
One thing to keep in mind is that

00:00:56.400 --> 00:00:58.680
computers are stupid. They will just do

00:00:58.680 --> 00:01:01.000
what they're told, line by line, word by word,

00:01:01.000 --> 00:01:03.080
and not really think about what you mean.

00:01:04.000 --> 00:01:04.480
So

00:01:04.800 --> 00:01:06.200
it's good to keep in mind that

00:01:06.200 --> 00:01:07.080
you have to be a literal.

00:01:07.400 --> 00:01:09.080
So that's it for the introduction of this series.

00:01:09.880 --> 00:01:11.680
Let's go and do something practical

00:01:11.680 --> 00:01:12.800
in Blender itself.

00:01:13.280 --> 00:01:15.080
Blender can be programmed in Python.

00:01:15.080 --> 00:01:16.800
That's quite a nice language

00:01:16.800 --> 00:01:18.480
that can be read and understood quite easily.

00:01:18.800 --> 00:01:19.880
Furthermore, it allows you

00:01:19.880 --> 00:01:22.200
to interactively toy around with it.

00:01:22.200 --> 00:01:24.000
So let's dive into Blenders Python console,

00:01:24.880 --> 00:01:26.800
"after I change my shirt", that is.

00:01:28.000 --> 00:01:30.480
So here is the default startup file.

00:01:30.480 --> 00:01:33.200
We can replace the timeline with the Python console,

00:01:33.200 --> 00:01:34.680
because we don't need the timeline right now.

00:01:34.680 --> 00:01:37.000
Let's do some computations,

00:01:37.880 --> 00:01:42.200
3 x 4 = 12, 2 + 3 x 4 = 14,

00:01:43.480 --> 00:01:45.680
because multiplication and division

00:01:45.680 --> 00:01:48.000
go before addition and subtraction.

00:01:48.480 --> 00:01:50.000
So if you want to do the addition first,

00:01:50.000 --> 00:01:52.880
you have to use parentheses,

00:01:52.880 --> 00:01:53.600
(2 + 3) x 4 = 20.

00:01:54.280 --> 00:01:56.280
Let's go and see if we can do something

00:01:56.280 --> 00:01:58.280
to that default Cube.

00:01:58.280 --> 00:02:00.400
If we press N in the Viewport for numeric data,

00:02:01.080 --> 00:02:03.280
you can see locations X Y Z.

00:02:03.880 --> 00:02:05.200
Let's move it somewhere else.

00:02:05.800 --> 00:02:07.280
So if you hover your mouse over the locations,

00:02:07.280 --> 00:02:08.800
you can see it in Python information,

00:02:08.800 --> 00:02:11.400
bpy.data.objects.cube.location.0,

00:02:13.200 --> 00:02:15.080
which basically means from

00:02:15.080 --> 00:02:17.800
Blender's Python Interface, get the data, in there are objects,

00:02:17.800 --> 00:02:19.680
from those objects, I want to have cube,

00:02:19.680 --> 00:02:20.400
cube has a location,

00:02:20.880 --> 00:02:23.000
and location has a few components.

00:02:23.000 --> 00:02:25.600
X is component 0, Y is component 1

00:02:25.600 --> 00:02:28.480
and Z is component 2.

00:02:29.280 --> 00:02:31.600
We can just copy this into our Blender Python console,

00:02:31.600 --> 00:02:34.080
bpy.data.objects.cube.location.0

00:02:36.280 --> 00:02:39.800
and this gives us the X location,

00:02:41.400 --> 00:02:43.000
and location 1

00:02:43.280 --> 00:02:46.280
gives us the Y location, and

00:02:46.280 --> 00:02:47.400
of course 2 will give us Z.

00:02:48.080 --> 00:02:51.000
Alternatively you can use letters.

00:02:51.000 --> 00:02:53.400
So there is actually a ".X", do exactly the same thing.

00:02:53.400 --> 00:02:55.480
I like those little bit better.

00:02:55.480 --> 00:02:57.400
It makes your script a bit easier to read

00:02:57.400 --> 00:02:59.600
if you just name them X Y and Z.

00:03:00.400 --> 00:03:02.400
This isn't limited to reading,

00:03:02.400 --> 00:03:03.880
we can also write to it.

00:03:04.200 --> 00:03:05.680
So we can set ".Z" to 0,

00:03:06.000 --> 00:03:07.280
and then is (0)

00:03:07.880 --> 00:03:08.480
or 1

00:03:09.080 --> 00:03:09.800
or Alt + 0

00:03:10.200 --> 00:03:12.600
which means increment.

00:03:12.880 --> 00:03:14.800
Increment by 0.1,

00:03:15.200 --> 00:03:16.600
and we can keep using this.

00:03:17.600 --> 00:03:19.800
I'm pressing the up arrow key in the console

00:03:19.800 --> 00:03:21.280
that gives me the previous command,

00:03:21.280 --> 00:03:23.000
and then press "enter", so you don't have

00:03:23.000 --> 00:03:24.800
to type everything by hand again.

00:03:26.000 --> 00:03:27.800
We can also assign to the location directly

00:03:27.800 --> 00:03:29.880
instead of to each component individually.

00:03:31.080 --> 00:03:33.600
By removing the last component and then

00:03:33.600 --> 00:03:35.000
assigning it three numbers in parentheses.

00:03:35.000 --> 00:03:37.880
For example like this,

00:03:38.880 --> 00:03:41.480
and then we set X Y Z at the same time.

00:03:41.800 --> 00:03:43.080
This also worked for Scale.

00:03:51.280 --> 00:03:53.000
And it also works for the Rotation.

00:03:56.600 --> 00:03:59.000
Except that on the Python side of things,

00:03:59.000 --> 00:04:01.000
Rotations always work with Radiance,

00:04:01.480 --> 00:04:03.680
and not with Degrees that you might see in the UI.

00:04:03.680 --> 00:04:05.600
So this might get

00:04:05.600 --> 00:04:07.000
a little bit getting used to.

00:04:07.400 --> 00:04:09.000
One more thing that may make things

00:04:09.000 --> 00:04:11.080
a little bit easier to type is that

00:04:11.080 --> 00:04:13.480
the Python console supports auto-completion.

00:04:13.480 --> 00:04:15.480
So you can do "bpy.date" and then

00:04:15.480 --> 00:04:18.000
press Ctrl Space ".object.cube",

00:04:18.680 --> 00:04:20.600
you have to type yourself,

00:04:20.600 --> 00:04:23.000
bu then "rotation" and then "euler".

00:04:24.080 --> 00:04:25.280
So this is done with Ctrl Space,

00:04:25.680 --> 00:04:27.880
can be really useful.

00:04:27.880 --> 00:04:29.800
Can also help you figuring out what is there to type

00:04:29.800 --> 00:04:30.800
in the first place.

00:04:32.000 --> 00:04:34.000
So there's a whole list of things here.

00:04:35.400 --> 00:04:36.880
This you can find in the documentation by the way.

00:04:36.880 --> 00:04:38.480
If you go to Help, and then Python API reference,

00:04:38.480 --> 00:04:40.680
then you get all these information.

00:04:40.680 --> 00:04:42.080
But we'll cover that in a moment.

00:04:43.280 --> 00:04:44.880
Let's look at some other things.

00:04:44.880 --> 00:04:46.880
We can find about Python in Blender User Interface.

00:04:46.880 --> 00:04:49.080
We have seen these properties of existing objects.

00:04:50.680 --> 00:04:52.400
Let's see if we can delete

00:04:52.400 --> 00:04:54.400
and maybe create some new objects.

00:04:54.400 --> 00:04:56.680
This is done with operators,

00:04:56.680 --> 00:04:59.880
operators are Blender's way of binding certain functionality

00:04:59.880 --> 00:05:02.680
to menu items or buttons in the interface.

00:05:02.680 --> 00:05:05.280
So what we can do is just hover over Button,

00:05:05.280 --> 00:05:07.080
and then you see the operator command.

00:05:07.080 --> 00:05:08.880
You don't even have to remember it.

00:05:08.880 --> 00:05:11.000
You can just press Ctrl C to copy,

00:05:11.400 --> 00:05:13.480
and then press Ctrl V to paste it into the console,

00:05:13.480 --> 00:05:15.600
and it will just work.

00:05:15.600 --> 00:05:17.400
So  now we've deleted an object,

00:05:17.400 --> 00:05:19.280
let's look at creating one.

00:05:19.280 --> 00:05:21.600
You press Shft A, then over an operator

00:05:21.880 --> 00:05:24.080
hover your mouse and you see

00:05:24.080 --> 00:05:26.400
"bpy.ops.mesh.primitive UV sphere add".

00:05:27.280 --> 00:05:30.280
So again, press Ctrl C and then in the console,

00:05:30.280 --> 00:05:32.680
press Ctrl V and "enter"

00:05:32.680 --> 00:05:34.600
and there, you have your sphere.

00:05:34.600 --> 00:05:36.800
It's a bit boring one, is the default one.

00:05:36.800 --> 00:05:39.000
Let's see if we can do something about that.

00:05:39.000 --> 00:05:40.680
Don't be afraid,

00:05:40.680 --> 00:05:41.800
if you don't understand all these options yet,

00:05:42.080 --> 00:05:43.880
I will give you some information later

00:05:43.880 --> 00:05:45.680
where you can find out exactly what they do.

00:05:45.680 --> 00:05:48.280
So let's create it

00:05:48.600 --> 00:05:51.800
with size equals 0.10

00:05:52.880 --> 00:05:53.680
at location 1 2 3.

00:05:55.280 --> 00:05:57.600
And there you have a really tiny sphere.

00:05:58.000 --> 00:05:59.280
Let's create a bit bigger one at a different location.

00:06:06.600 --> 00:06:07.800
It's that simple really.

00:06:08.800 --> 00:06:10.400
We've already found quite a bit of information

00:06:10.400 --> 00:06:12.280
in Blenders User Interface,

00:06:12.280 --> 00:06:14.200
but there is also Blenders API documentation,

00:06:14.200 --> 00:06:16.600
which you can find right

00:06:16.600 --> 00:06:17.400
here in Blender, Python API reference.

00:06:17.800 --> 00:06:20.080
It opens the browser for you with information,

00:06:20.080 --> 00:06:22.280
and you can

00:06:22.280 --> 00:06:24.600
use that copy paste trick again.

00:06:24.600 --> 00:06:25.600
So Let's see,

00:06:26.200 --> 00:06:28.800
Shft A, Mesh, UV Sphere, Ctrl C,

00:06:29.680 --> 00:06:32.400
then I go to the browser and hit Paste in the quick Search,

00:06:35.280 --> 00:06:36.280
click on the first link,

00:06:36.880 --> 00:06:38.800
and there you have the explanation of what is what.

00:06:40.200 --> 00:06:41.880
Another good source of information

00:06:41.880 --> 00:06:43.280
is the Python standard library.

00:06:43.880 --> 00:06:45.880
So what have we seen so far?

00:06:45.880 --> 00:06:48.200
We know that computer is stupid

00:06:48.200 --> 00:06:50.080
and just do the commands one by one which we gave them.

00:06:50.600 --> 00:06:53.000
We've seen how to use the Python console,

00:06:53.000 --> 00:06:55.080
and how to create objects, remove objects,

00:06:55.080 --> 00:06:57.280
and modify existing ones.

00:06:58.000 --> 00:06:59.400
We've seen what we can do with

00:06:59.400 --> 00:07:01.680
copying information from Blenders User Interface.

00:07:02.800 --> 00:07:04.680
And it points you to a few sources of information

00:07:04.680 --> 00:07:06.200
where you can find more stuff.

00:07:07.280 --> 00:07:09.600
So in the next video, we will look at

00:07:09.600 --> 00:07:12.680
Names and Objects because typing

00:07:12.680 --> 00:07:15.080
bpy.data.objects.cube.something.something.something

00:07:16.280 --> 00:07:18.280
becomes really boring after a while.

00:07:18.800 --> 00:07:19.800
See you in the next video.