Hello TD community,
I recently stumbled upon Airtable to manage some data for our projects at Les Fantômes.
I’ve been looking around for a solution to read/write with TD and didn’t find much.
So here is my working solution with the Airtable Python wrapper
Step 1: Setup Airtable
I assume here that you already have an Airtable account. This is working with a Pro plan, not 100% sure the Free plan works too but let me know if you tried.
Here is a quick Test table I setup with 3 records. Notice that we have some animated gifs attachments in there.
Step 2: Download Python requirements
Make sure you have the matching Python version of Touchdesigner installed on your machine.
In your Command shell, you can write :
pip install airtable-python-wrapper
If you execute the command twice it will tell you where the packages have been stored:
Go get the new folders in the site_packages folder and copy them to your TD Lib folder :
C:\Program Files\Derivative\TouchDesigner\bin\Lib
Start Touchdesigner.
Step 3: READ DATA in TOUCHDESIGNER
You’ll need 2 pieces of info to access your Airtable data from Python.
1. First find your Base ID
Go to the API documentation of your base: https://airtable.com/api
Click on your base
2. Then get your API KEY in the Account information page
Click on the purple zone and Copy the key.
3. Python in TouchDesigner
Create a TextDAT with the following code
import requests
from airtable import airtable
at = airtable.Airtable(STR_BASE_ID , STR_TABLE_NAME , STR_YOUR_API_KEY)
print(at.get_all())
Replace STR_BASE_ID and STR_YOUR_API_KEY by the info above.
STR_TABLE_NAME is “Test” in our case.
Type Alt-T to show your Textport.
Right-click your TextDAT and Run the script
Here you go, you’re reading Airtable Data within TouchDesigner.
Now you can do many things if you’re familiar with Python, like organizing the data in a TableDAT for instance.
Here is a simple approach that works with my Test table.
import requests
from airtable import airtable
table = op('table1')
table.clear(keepFirstRow=True)
at = airtable.Airtable('appz0Q5ikqkHPN9I9','Test', STR_API_KEY)
for record in at.get_all():
table.appendRow(
[record["id"],
record["fields"]["Name"],
record["fields"]["Notes"],
record["fields"]["Attachments"][0]["url"],
record["fields"]["Status"]
])
From there you can do whatever you want with the data within Touchdesigner. Like showing all the animated clips with a Name overlay for instance:
You can also make this dynamic with the ReplicatorCOMP to automatically adapt to the table size when you update your database.
Step 4: WRITE DATA IN AIRTABLE
Here we’ll simply use the Airtable.insert() function of the Python API.
import requests
from airtable import airtable
at = airtable.Airtable('appz0Q5ikqkHPN9I9','Test', STR_API_KEY)
record = {"Name":"Jane", "Notes":"456"}
at.insert(record)
Run the code (Right-click and Run Script or Ctrl+R on your TextDAT)
Hello Jane!
1. Data Validation
Notice that the Status field has only 3 restricted possible values “Todo”, “In Progress”, and “Done”. If you specify something else in Python like
record = {"Name":"Jane", "Notes":"456", "Status":"New"}
You’ll receive an HTTP error from Airtable saying :
requests.exceptions.HTTPError: ('422 Client Error: Unprocessable Entity for url: https://api.airtable.com/v0/appz0Q5ikqkHPN9I9/Test', '422 Client Error: Unprocessable Entity for url: https://api.airtable.com/v0/appz0Q5ikqkHPN9I9/Test [Error: {\'type\': \'INVALID_MULTIPLE_CHOICE_OPTIONS\', \'message\': \'Insufficient permissions to create new select option ""New""\'}]')
Results of run operation resulted in exception.
Step 5: UPLOAD ATTACHMENT TO AIRTABLE
Another field that we didn’t fill up above is the Attachment field.
To upload an image to Airtable (as of today) you’ll need to upload your file on the Web to a third party service and provide the URL to the Attachment field when inserting the new Record.
I used Cloudinary for the example. Create an account and run :
pip install cloudinary
Get the folders from site_packages to your Touchdesigner/bin/Lib folder.
Restart Touchdesigner.
import cloudinary
import cloudinary.uploader
cloudinary.config(
cloud_name = '****************',
api_key = '****************',
api_secret = '****************'
)
up = cloudinary.uploader.upload("MyFolder\\Clip.gif")
print(up["url"])
Replace the ***** by the info that is located on your Cloudinary console
Here you’ll get the Web URL of your uploaded image.
Let’s now use that to Upload a new Record with an image attachment to Airtable
import cloudinary
import cloudinary.uploader
cloudinary.config(
cloud_name = '*************',
api_key = '*************',
api_secret = '*************'
)
up = cloudinary.uploader.upload("Anna.gif")
import requests
from airtable import airtable
at = airtable.Airtable('*************','Test', '*************')
record = {"Name":"Anna"}
record["Attachments"] = [{"url": up["url"] }]
at.insert(record)
Hello Anna!
Hope that helps some of you, let me know if you have questions or things to add to this article!
@philippe.dubost