background top icon
background center wave icon
background filled rhombus icon
background two lines icon
background stroke rhombus icon

Download "Beginner Blender Python Exercise: Track an empty with a camera"

input logo icon
Video tags
|

Video tags

blender
python
scripting
tutorial
Subtitles
|

Subtitles

subtitles menu arrow
  • ruRussian
Download
00:00:00
Victor Stepanov: This is a beginner, Blender Python exercise, where we'll go
00:00:03
over how to make a camera track and empty.
00:00:06
And at the end of the video, I'll go over a couple more exercises
00:00:10
to solidify your knowledge.
00:00:12
Hey, I'm Victor Stepanov.
00:00:14
I'll be guiding you through this beginner Blender Python exercise.
00:00:18
Today's video was inspired by one of the comments left in my previous videos.
00:00:23
Thank you.
00:00:24
Crazy Fanatic, Man, for suggesting this topic.
00:00:28
Let's get straight into the tutorial, uh, or we're going to
00:00:31
go into the scripting workspace.
00:00:34
And we will need to add a timeline right below the 3d viewport, and because
00:00:40
we're going to do a bit of animation.
00:00:44
All right.
00:00:45
So leave it like, so I'm going to hit new right here to, uh, create a new.
00:00:52
Next to save some time.
00:00:54
I'm gonna copy over some comments for us.
00:00:57
Uh, so we can follow them along.
00:01:00
The idea here is pretty straightforward.
00:01:03
We're going to add a row of cubes.
00:01:06
We're gonna add a camera.
00:01:07
We're going to add a empty, we're going to animate the empty's location and we're
00:01:12
gonna make a camera track, uh, the empty.
00:01:16
The first thing that we usually do.
00:01:17
And any Blender Python script is we import, uh, bpy uh, at the top,
00:01:25
this is a, a module, a Python module that.
00:01:29
Python access to Blenders functionality, meaning that it allows Python to press
00:01:35
Blender's, buttons, or execute some of the commands that Blender can do.
00:01:40
Next we're going to set up some parameters that help us control the script.
00:01:46
For example, uh, we're gonna set, uh, the cube, uh, counts,
00:01:52
and I want to add 10, uh, cubes.
00:01:56
So I'm going to put, uh, I'm going to create a variable and set the value to 10.
00:02:01
I'm also gonna set the frame count.
00:02:06
And set that to, uh, 300.
00:02:09
Uh, so right now this is just a variable.
00:02:12
It's not set.
00:02:13
It's not actually doing anything, uh, or Bunder right.
00:02:17
to set the frame count.
00:02:19
I'm going to go here and just move this a bit.
00:02:23
And right here, below in the interactive, sorry, in the info
00:02:26
panel, we can see a command that set.
00:02:29
Uh, the frame count, right?
00:02:32
I'm going to copy this just right here.
00:02:35
And I'm going to replace, uh, this variable that we
00:02:38
said and set it right here.
00:02:41
All right. I'm going to just clear everything from the scene by hitting A, X and then delete.
00:02:46
And then I'm going to just run this script.
00:02:50
So after I want to watch how the number of frames change, all right.
00:02:56
Uh, you can see that after I.
00:02:58
The only thing that changed and our project right here is just
00:03:02
the number of frames in the scene.
00:03:05
Next, we want to add a row of cubes to do that.
00:03:08
Let's first add a cube into the scene.
00:03:11
Uh, we're going to do that by hovering over the 3d viewport and hitting
00:03:15
shift a to bring up the add menu.
00:03:18
And I'm just going to select a cube, uh, with this, uh, I told Blender to
00:03:24
add a cube and we can see the command.
00:03:28
Oh, we can do in Python, right in the info panel.
00:03:31
I'm just going to copy this right here and paste it right here.
00:03:37
I'm going to remove everything, all these parameters except
00:03:41
the location, uh, and the size.
00:03:45
Maybe we can update that later.
00:03:49
All right. So, um, Uh, select everything by hitting and delete everything by heading A X
00:03:55
and then delete and run the script.
00:03:57
And we get our queue back, but we want a row of cubes on the Y axis.
00:04:03
So we need to have a, we need to use a four loop to do that.
00:04:08
Right. We can. Of course, um, And do something like this by just copying a number of these,
00:04:14
uh, statements and then, uh, maybe updating the Y coordinate like, so,
00:04:20
and, uh, this will get us somewhere, but it's not going to get us far.
00:04:26
Let's actually create a loop.
00:04:28
So it's, uh, for, uh, I in range.
00:04:33
And then.
00:04:35
I'm going to indent.
00:04:37
So I'm hitting tab two and den telling a Python in that
00:04:41
everything under this indentation.
00:04:43
So we can have multiple lines.
00:04:45
This'll be repeated.
00:04:46
Uh, however many times I put, uh, here in the range.
00:04:50
So in this case, I'm going to use a queue count and I'm gonna
00:04:54
just use this variable here.
00:04:57
What's clear the scene, uh, and then run the script.
00:05:02
So at this point, uh, you think that everything it's just doing the
00:05:07
same thing, but actually if we look into the outliner, it's adding a
00:05:10
number of cubes right into the same.
00:05:13
Place right.
00:05:15
Uh, this is, uh, not great because they're all overlapping.
00:05:19
Let's try to fix that.
00:05:20
And, uh, let's select everything and clear.
00:05:24
I'm going to use this eye variable right here.
00:05:28
And gets updated on each iteration of the loop, uh, at, uh, you can go into
00:05:34
the F interact Python console, right here and see how, uh, this I can get updated.
00:05:43
So you can see it's going from zero to nine.
00:05:46
Uh, and, uh, I'm going to F I think that let's, uh, just use this right here.
00:05:51
Uh, as the, as the Y coordinate, let's run the script and, uh, you can see that.
00:05:59
There is a, some like cubes are getting added, but, uh,
00:06:03
they are overlapping, right?
00:06:05
So this is not what we want to see.
00:06:09
So we need to add some spacing, so, oh, let's go back.
00:06:13
Uh, and let's maybe multiply this by two.
00:06:18
So.
00:06:20
Well, let's rerun that.
00:06:21
Okay.
00:06:22
This is giving us a bit more, uh, uh, space, but it's still, uh, I'd like some
00:06:29
more breathing room, uh, for these coops.
00:06:32
I'm going to set this value right here.
00:06:36
Two, three.
00:06:37
So let's check out what that's going to do, uh, right.
00:06:42
You can see that, uh, this is.
00:06:45
Spacing the cubes nicely right here.
00:06:48
And I'm going to leave it at that.
00:06:52
I'm actually going to, uh, create, um, location offsets variable.
00:07:01
Right? So maybe in the future, we would want to update this.
00:07:05
I'm going to move this three right here, uh, and put it right here.
00:07:17
Okay, next, we need to add our empty and I'm going to do this.
00:07:21
I'm going to get the code for the empty, the same way we did with a cube.
00:07:26
I'm going to hover over the 3d viewport and had shift a and then, uh, hit a plane.
00:07:33
Get a plain Axis empty.
00:07:36
Uh, all right.
00:07:37
I got added right here.
00:07:41
And, uh, the code, uh, was, uh, added to the info panel right here.
00:07:48
I'm going to just paste this, right?
00:07:51
Like, so actually we can just delete all the parameters that are getting passed in.
00:07:57
Uh, and they're giving me just set to default.
00:08:01
Next we need to create a variable.
00:08:03
That's going to allow us to access the parameters and set
00:08:07
the properties of this object.
00:08:10
And to do that, we'll need to grab the currently active object in the
00:08:16
interactive Python and console.
00:08:18
What's uh, Check out the context and then the active object.
00:08:26
Right? So this is a, the currently active object.
00:08:30
And I'm going to use that to create a new variable and call it empty.
00:08:34
I'm just going to copy this right below.
00:08:38
I'm going to call this object empty.
00:08:41
And now, uh, with this, we'll be able to, uh, use this variable right here
00:08:46
to do the location animation of the.
00:08:51
So next we need to animate location property.
00:08:55
So all the objects, uh, in blender have a location property.
00:09:00
Uh, we can use that like, so, so let me just do the same thing here
00:09:06
in the interact five and console.
00:09:08
So I'm going to create a variable empty.
00:09:11
Uh, see if I just, uh, just, uh, enter that it gives me that object, uh, and it
00:09:18
has the location, uh, property, and to get these previews that I'm getting right
00:09:24
here, I just type a bit and then hit tab.
00:09:27
And that, uh, kind of shows me the POS possibilities that, uh, I can like enter
00:09:33
in this interactive Python console.
00:09:35
So the current location.
00:09:37
Uh, is that, and let's, uh, just maybe change the Z location.
00:09:44
I'm going to set it to zero and you can see that the location
00:09:47
of the empty got updated.
00:09:50
Uh, like, so, so, um, we're gonna want to animate this and, uh, to
00:09:57
anime, we need to add a key frame.
00:10:00
So, uh, we need to add a key frame.
00:10:05
And then insert.
00:10:06
When you're entering a function name, you can hit tab and get a full, uh,
00:10:11
documentation string about, uh, what you need to pass in to this function.
00:10:15
And this is what I'm doing right here.
00:10:17
So I really need the, uh, set the data path and I'll be, uh,
00:10:22
using this frame right here.
00:10:25
So I'm just gonna copy this, um, pasted right here.
00:10:30
And for the data path, I'm going to be using a location or here,
00:10:35
like this location is going to be the name of the data path.
00:10:39
I'm going to put the quotes around it, um, making it a text or like a string,
00:10:45
uh, and I'm going to use, uh, the frame.
00:10:49
Uh, to insert, uh, to insert, uh, the key frame at a particular frame that I want.
00:10:57
So in this case, I want to insert in the first frame, uh, and let's actually delete
00:11:02
everything and let's rerun a script.
00:11:08
All right. Uh, so I'm gonna move this cube and here's our empty.
00:11:14
You can see, we have a key frame set at frame one.
00:11:19
Uh, now let's do the same time.
00:11:21
I'm just going to copy this and use the frame count and set that like, so
00:11:30
now let's rerun, let's lead everything in rerun script.
00:11:34
Uh, and as you can see, we're actually we've selected a, the, um, All right.
00:11:40
It's the last thing that we're adding.
00:11:42
So you could see already that there's two key frames, one at the,
00:11:48
uh, frame one and one at frame 300.
00:11:52
That's great. Now I want to move the empty along the Y access.
00:11:56
I'm going to do something similar to this.
00:12:00
I'm just going to copy the location modification code.
00:12:03
Just paste it underneath here.
00:12:05
I'm going to change the why.
00:12:07
So when we inserted the first frame, uh, the location of the
00:12:11
empty is at the center of the scene.
00:12:14
And, uh, when we, before we insert the second key frame, we
00:12:18
want to change that location.
00:12:21
So I'm going to just use, uh, the location offset and I'm going
00:12:28
to multiply that by the number.
00:12:31
Of cubes.
00:12:33
So let's just do that.
00:12:35
I'm going to delete everything and a rerun the script.
00:12:41
So let's make this a bit bigger.
00:12:44
You can see that, uh, the empty right here.
00:12:47
So after generating everything is at the last frame, right?
00:12:50
It's like the last thing we, uh, move the empty and then we set the last key frame.
00:12:56
So as soon as I press space, it's gonna.
00:12:58
Teleport right here.
00:13:00
And you can see how it's going through all of these cubes animating along the Y axis.
00:13:10
All right.
00:13:12
Alright, the next thing that we need to do is add the camera into
00:13:16
the scene and you guessed it.
00:13:18
We're going to do it the same way that we did for the cube and the empty.
00:13:22
I'm going to hover over the 3d viewport at shift a and then,
00:13:26
uh, find the camera and just.
00:13:29
Like, so it gets added to the center of the scene.
00:13:32
So if I pick up this cube, I can, I'm moving the cube by hitting G
00:13:36
and then, uh, moving out of the way.
00:13:39
So here's our camera.
00:13:42
And, uh, now we can, uh, get the code and it's going to be the code for this,
00:13:48
uh, for adding a camera into the scene is going to be in the info panel down below.
00:13:54
I've already moved a couple of things.
00:13:55
So I need to scroll a bit up.
00:13:58
So here we go.
00:13:59
It's a object camera.
00:14:00
Add in a copy, this and paste it right here.
00:14:07
I'm going to just remove, again, all the parameters I'm going to be set to default.
00:14:12
Uh, I'm going to copy this, getting the active object.
00:14:15
So as soon as you add anything into blender, uh, the, that becomes
00:14:20
the currently active object.
00:14:21
In this case, it's going to be our camera.
00:14:25
I'm also gonna use, uh, the same, um, method we use to set the location.
00:14:33
So I'm gonna set the camera's location on the X, Y, and Z.
00:14:39
So I'm going to change.
00:14:41
Like so, So I'm going to set a X, two 15, a Z to a two, and I'm going to
00:14:52
try to put the camera in the middle.
00:14:54
So it's going to be, uh, this offset, right.
00:14:59
And then just divided by two.
00:15:02
All right. Let's lead everything from the scene or you run the script and
00:15:07
let's see where the camera is.
00:15:10
So it is right here, right?
00:15:13
It's you can see how it could be tracking, uh, this empty right now.
00:15:18
It's looking down.
00:15:20
We haven't really said anything.
00:15:21
We haven't rotated it.
00:15:23
So this is as expected.
00:15:25
Let's come back.
00:15:27
Alright. The only thing that's left for us to do is add the constraint.
00:15:30
They'll track the empty for us.
00:15:33
I'm going to select the camera.
00:15:34
Well, it's already selected and go into the constraints right here, add a new
00:15:41
constraint and I'm going to suck to track.
00:15:45
Uh, and then I'm just gonna type in the empty.
00:15:50
To that we're going to use to track.
00:15:52
And you can see already that this, uh, seeing got updated and the
00:15:57
camera's looking already at the empty.
00:16:00
So let's, uh,
00:16:03
let's scroll to the very bottom and let's copy these two lines right here.
00:16:10
Let's copy that.
00:16:12
And let's replace this part right here with the camera.
00:16:19
And, uh, this is going to add the constraint, right?
00:16:22
And then we're going to set the constraint, like what target we're going
00:16:26
to be tracking here is going to be, uh, it's going to just use this empty,
00:16:30
but in reality, we can use this, uh, variable right here that we created and
00:16:34
it's going to result in the same thing.
00:16:36
So I'm just going to paste that, uh, and.
00:16:40
Let's clear the scene and let's rerun the script and you can see it's already,
00:16:48
the camera's already tracking that empty.
00:16:52
Uh, you can look at this, right?
00:16:54
The constraint was added and the empty, oh, as set as the target, as we.
00:17:02
All right.
00:17:03
Let's now hover over the 3d viewport and press control space.
00:17:08
Uh, and we can actually, uh, press the zero on the numb pad and I'm
00:17:13
going to press space to start the animation and you can see them.
00:17:18
Uh, the camera is tracking our empty going through all these cubes.
00:17:31
And if you're learning something new today or just enjoying this
00:17:34
video, make sure to hit the light, but it will help me tremendously.
00:17:38
All right.
00:17:39
And as promised the exercises to solidify your knowledge for the first exercise, I
00:17:45
want you to update the code of this script and make the camera move with the empty.
00:17:51
So it's going to look something like this.
00:17:55
So go ahead and try this on your own positive video.
00:17:59
And after you and pause, I'll explain how I would have done.
00:18:03
All right to achieve that result.
00:18:06
I would have done the following.
00:18:08
Uh, I would just delete this or comment this out.
00:18:11
So we're going to be moving the camera along with empty.
00:18:15
Now I'm just going to copy this code that we already have for the.
00:18:20
And it pays to right underneath here, and I'm going to change
00:18:24
the variable that we use for them team and set it to the camera.
00:18:30
Right.
00:18:31
And let's see how that looks.
00:18:33
I'm going to delete everything.
00:18:36
I'm going to rerun the script.
00:18:39
And let's take a closer look.
00:18:42
And as you can see now, the camera is moving along with the empty.
00:18:51
All right.
00:18:52
And for your second exercise, I want you to try to achieve this scene right here.
00:19:00
Pause the video now and try it on your own.
00:19:04
Alright to achieve that scene.
00:19:06
Uh, it's pretty straightforward instead of, uh, moving, uh, the
00:19:10
empty, uh, along instead of animating the empty, uh, we're just going
00:19:14
to set the location of the empty.
00:19:16
So I'm just going to comment this right here and we're just going to copy it.
00:19:23
The location of the camera from our original scene.
00:19:29
So let's delete everything and rerun the script.
00:19:35
so, as you can see, the camera is animating and our tracking them to that's
00:19:43
in the middle of these rows of cube.
00:19:47
I plan to make another video that will transform this code right
00:19:51
here and add some functions.
00:19:53
We're starting to work with more code here, and I really want you to start
00:19:58
getting used to creating functions.
00:20:00
So that's what we're going to do in the next video.
00:20:04
Make sure to subscribe for more videos like this.
00:20:07
Thank you so much for watching.

Description:

In this video, we will go over a Python script that sets up a camera to track an empty. Final code: https://gist.github.com/CGArtPython/c4f91fc13d025c1b0a61476dcf81891e 00:00 - Intro 00:27 - Setup Workspace 00:52 - Go over the steps 01:24 - Start writing the script 02:17 - Set the frame count 03:06 - Create a row of cubes 07:16 - Add empty 10:00 - Animate the empty's location 13:16 - Add camera 15:32 - Add a constraint to track the empty 17:33 - Exercise #1 18:52 - Exercise #2 19:47 - Outro Intro Music Fiery Trails - Silent Partner Background Music Where To - Birocratic Outro Music Geographer - Easy Shake

Preparing download options

popular icon
Popular
hd icon
HD video
audio icon
Only sound
total icon
All
* — If the video is playing in a new tab, go to it, then right-click on the video and select "Save video as..."
** — Link intended for online playback in specialized players

Questions about downloading video

mobile menu iconHow can I download "Beginner Blender Python Exercise: Track an empty with a camera" video?mobile menu icon

  • http://unidownloader.com/ website is the best way to download a video or a separate audio track if you want to do without installing programs and extensions.

  • The UDL Helper extension is a convenient button that is seamlessly integrated into YouTube, Instagram and OK.ru sites for fast content download.

  • UDL Client program (for Windows) is the most powerful solution that supports more than 900 websites, social networks and video hosting sites, as well as any video quality that is available in the source.

  • UDL Lite is a really convenient way to access a website from your mobile device. With its help, you can easily download videos directly to your smartphone.

mobile menu iconWhich format of "Beginner Blender Python Exercise: Track an empty with a camera" video should I choose?mobile menu icon

  • The best quality formats are FullHD (1080p), 2K (1440p), 4K (2160p) and 8K (4320p). The higher the resolution of your screen, the higher the video quality should be. However, there are other factors to consider: download speed, amount of free space, and device performance during playback.

mobile menu iconWhy does my computer freeze when loading a "Beginner Blender Python Exercise: Track an empty with a camera" video?mobile menu icon

  • The browser/computer should not freeze completely! If this happens, please report it with a link to the video. Sometimes videos cannot be downloaded directly in a suitable format, so we have added the ability to convert the file to the desired format. In some cases, this process may actively use computer resources.

mobile menu iconHow can I download "Beginner Blender Python Exercise: Track an empty with a camera" video to my phone?mobile menu icon

  • You can download a video to your smartphone using the website or the PWA application UDL Lite. It is also possible to send a download link via QR code using the UDL Helper extension.

mobile menu iconHow can I download an audio track (music) to MP3 "Beginner Blender Python Exercise: Track an empty with a camera"?mobile menu icon

  • The most convenient way is to use the UDL Client program, which supports converting video to MP3 format. In some cases, MP3 can also be downloaded through the UDL Helper extension.

mobile menu iconHow can I save a frame from a video "Beginner Blender Python Exercise: Track an empty with a camera"?mobile menu icon

  • This feature is available in the UDL Helper extension. Make sure that "Show the video snapshot button" is checked in the settings. A camera icon should appear in the lower right corner of the player to the left of the "Settings" icon. When you click on it, the current frame from the video will be saved to your computer in JPEG format.

mobile menu iconWhat's the price of all this stuff?mobile menu icon

  • It costs nothing. Our services are absolutely free for all users. There are no PRO subscriptions, no restrictions on the number or maximum length of downloaded videos.