Chad Moore : 02.2010 : tweetMaya (techArt)

I needed to look into python and web application API for a lil’ project I am working on.  To prototype this, I decided to see if I could tweet from Maya.  

Why?  Well Maya is extensible via python and there are a bunch of python wrappers for the twitter api.

What follows is just really my notes to self when working this out this evening.  I am sure there is much to improve upon and learn, but if your curious take this stuff and run with it!

A couple of notes before we begin:

easy_install

  • What is it?
    • easy_install is a python module that let’s you install other python modules relatively easily.
  • Where is it?
  • How to install it?
    • Follow the above instructions (download and run the .exe)
  • How to use it?
    • When you find a module you’d like to install (like simplejson noted below, just navigate to the easy_install directory and call it and the name of the module your looking for)
      • Run a windows prompt and go to c:\python26\Scripts
      • Type easy_install simplejson
      • Rejoice in it’s ease.

simplejson

  • What is it?
    • It’s one of the format twitter exposes for data exchange in it’s api
    • http://www.json.org/ says “JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write.”
  • Where is it?
  • How to install it?
    • See instructions for easy install above
  • How to use it?
    • For this use case, the twitter-python does all the work, simplejson is required for it to work

twitter-python

  • What is it?
    • It’s a python wrapper for the twitter api
      • It let’s python talk to twitter
  • Where is it?
  • How to install it?
    • See instructions for easy install above
    • Run a windows prompt and go to c:\python26\Scripts
    • Type easy_install twitter-python
    • Rejoice in it’s ease.
  • How to use it?
    • See the script below

Easy-Install gotcha?

This was probably user error of some kind since this was my first time with easy_install… but I wound up having to copy the modules I needed into the C:\Python26\Lib\site-packages directory.  I expect that to happen automatically with the easy install commands, but I probably missed something.  Either way, just manually copy and you’ll be set.

Make a python path environment variable.

In your maya.env you’ll need to make paths for Maya to go find python in.  Simply add a pythonpath as such … PYTHONPATH = C:\Python26\;C:\Python26\Lib\site-packages\

OK, on with the code

Now that we have everything we need here’s the code…

import twitter #import the module

twitterUser = 'USERNAME' #hard code in the username
twitterPass = 'PASSWORD' #hard code in the password

api = twitter.Api(twitterUser, twitterPass)#call the api with user credentials

def countCharacters(tweet):#make sure there isnt more than 140 characters in the tweet
    validTweet = 0
    numCharacters = len(tweet)
    if numCharacters < 140:
        validTweet += 1
    else:
        validTweet = 0

    return validTweet, numCharacters

#post a tweet
tweet = 'this tweet brought to you by the python script editor in Maya'

goNoGo = countCharacters(tweet)#do the work to see if we're over 140 characters
if goNoGo[0] == 1:# < 140 characters? == Tweet!
    status = api.PostUpdate(tweet)
else: #> 140 characters? == Fail!
    print "tweet exceeds 140 character limit by %d"%(goNoGo[1] - 140)

Theres a lot more that needs to be added; making this tool a standalone def, building a UI for user/pass/tweet text string fields and some optimization. But hey, it was a fun challenge for the evening.

Share and Enjoy:
  • del.icio.us
  • Twitter
  • Tumblr
  • PDF
  • email