Creating a twitter bot with Blender and a Raspberry Pi
Recently I wrote my first twitter bot. Since I know a little of Blender and Python I thought I would combine the two of them to make something. As I could not find any tutorial to do this specifically, here is what I learned in the process. I had a lot of fun on this project and highly recommend you try it yourself. There is something very satisfying in seeing this little extension of yourself post it’s own creation on social media.
All the files from this tutorial are available on Github.
In this article I will try to teach you how to install Blender on the Pi, and script it to render at regular interval before posting to Twitter.
To follow it is good if you have basic knowledge of the Blender UI. No experience with Raspberry Pi is required as I will walk you through it.
To follow this tutorial you will need :
- A Raspberry Pi 3 with the latest version of Raspbian, connected to the internet
- A computer running Blender 2.78 or newer
First a basic introduction to the way this little bot will work. Most of the 3d work is done on your computer in Blender. There you will adjust the main details of your Blender file. The automation part is where the RasperryPi will be useful.
In total the system is composed of four different parts :
- A Blender file — It contains your beautiful composition, material, lighting, etc.
- A script to run in Blender — This is were you will add procedural elements for your bot, that will be different in every picture
- A script to post your images on twitter — No point in rendering images if you don’t post them! This script will do that for you.
- The “crontab” — This is a little file on your Pi that will run your script at regular intervals
Let’s get started. First we will open blender on the computer and create a simple file. This is is the file that you Pi will render at regular interval. For the purpose of this example I have made a simple Suzanne scene that you can download here if you want to follow along.
Here are a few things to keep in mind when making your scene :
- Switch the render engine to
- Set the render device to
- The Pi is not as powerful as your computer, keep your resolution and samples low if you want it to render quickly. A 500x500px pictures and 50 samples is a good start.
- Your Pi only has 1GB of ram, so keep your texture files small
- Your file will render exactly as you saved it, so make sure you check all you settings, the angle of the camera, etc…
Now save your file on a thumb drive and jump to the fun part : the Raspberry Pi
First thing you need to do on the Raspberry is to install blender. We will do this with the terminal. If you are not familiar with linux, the terminal lets you easily install programs and run scripts. Open the terminal by clicking it’s icone in the task bar.
First you will need to update your list of software, type this in the terminal :
sudo apt-get install
Now you are ready to install blender, simply type :
sudo apt-get install blender
Don’t forget to copy your .blend file to the raspberry. Create a
robot folder in your main directory and drop it there.
Next thing we are going to do is write the script that will render our picture. The Pi is not powerful enough to run blender with it’s graphical interface, but thankfully we can still use it through the terminal. We will use the script to tell blender where to render the file, but most importantly to add some random factors so that every rendered picture is different.
The script will be written in Python. If you have never heard of Python, fear not! It is very simple to learn and is, in fact, often used as a first language to teach beginners to code. Most of the Blender software is also written in Python, so it make it very straightforward to send commands to Blender.
To write the script, we will use
Thonny Python IDE which is already installed on the Raspberry, in the main menu, under
Let’s start typing the code. First, we need to include Blender’s functions in the code :
Next, we will create a variable called
rndr that will store all the information of the scene we will render
rndr = bpy.data.scenes['Scene'].render
Great! Now let’s tell blender how to name our picture
rndr.filepath = 'mypicture.png'
Finally, we need to tell blender to render, and to save the picture.
Good job you are done! This is what your file should look like :
rndr = bpy.data.scenes['Scene'].render
rndr.filepath = 'mypicture.png'
Now save your file in your
robot folder as
mycode.py and jump back to the terminal to make sure that it is working correctly.
Here we will tell Blender to run, open your file, then run the python script that we just wrote :
blender -b robot/suzanne.blender --python robot/mycode.py
As you can see
-b will tell blender which .blend file to open.
--python tells Blender to open a python file and run it. You always need to type the full path to your files, so that the terminal knows where to find them.
Once you typed this, you should see something happening in the console, indicating that is rendering. After a bit you can check you
robot folder. Magic! Your picture is here, without even opening the graphical interface! Good job.
Now let’s add a little variation to our picture so that it is produces a different output every time you start your script. Go back to editing your Python file.
A very nice thing about Blender is that for each action you perform in the interface, it will also tell you what is the equivalent in Python that you can write in your code. For this you need to pull down the top window, and you will see a console appear, logging all your actions. Note that some actions, like selecting an object, are not logged and you will still need to look up what they are on Google.
What we are going to do here is very simple, we will select Suzanne, and rotate her by a random amount, different for each picture.
First, we need to import a function capable of generating a random number. After the first line of
mycode.py type this :
from random import randrange
We will also need a function to convert degrees to radian, as radian is what Blender is using to measure angles.
from math import radians
Good! Now let’s create a variable
rotation and give it a random number between 0 and 360.
rotation = randrange(0, 360, 1)
1 here is to specify the interval between possible random outputs. This will let us have only round numbers.
Now we will select Suzanne
bpy.data.objects['Suzanne'].select = True
And finally rotate her by the random amout we just generated
bpy.context.object.rotation_euler = radians(rotation)
 refers to the Z axis. Number 0 is X axis, number 1 is Y axis. We type
radians(rotation) to convert our number in degrees to a number in radians.
Now, every time you run the script Suzanne will be turned by a random amount on her Z axis before being rendered.
Now you are able to render pictures on demand from you Pi, congrats. Next step is to post them on Twitter. For this you will need to use a library called Twython, that allows you to post on twitter from the terminal. You will also need to setup your twitter account.
I will not provide a step by step description on setting the Twitter account as there is already plenty on the internet. I recommend this very good one on instructables.
You can also find a working code on Github. Just replace the authentification strings with your own!
Last step. Now that you are able to render and post on twitter, you should automate the process and make it happen on a regular basis. I recommend no more than a tweet every couple hours if you don’t wish to spam people and fill up their timeline.
Luckily with linux it is very easy to time events like this. For that we will use a file called the crontab, that checks every minute if it need to start a command on your behalf. In this case the crontab will need to run your Blender file first, and your Twython script second.
Lets start by opening the crontab from the terminal.
Bow you should see the crontab, with a lot of comments explaining how it works. Let’s add our first line
1 * * * * blender -b /home/pi/robot/suzanne.blende --python /home/pi/robot/mycode.py
To exit, hit
ctrl + x , then
y and finally hit
On the first minute of every hour the crontab will now run the command and render a new picture. You might have noticed that the filepath are different from the one we were using earlier. This is because the crontab runs from the root directory and needs to be guided all the way to your file.
Do the same for the twython script.
59 * * * * python /home/pi/robot/twitterbot.py
Make sure that your
twitterbot.py has a direct link to you picture from the root directory too.
That’s it! Congrats on completing your first twitter bot!
I would love to see what fun things you come up with, make sure to hit me up on twitter @bouteillax to share it with me. This is also the best way to get in touch if you need some further help.