Addicted to 3dsmax, python and FB? (okay and a bit of PHP)

posted in: Blog | 0

Then we should be friends! I already have this for some time in my repository but it was somewhat broken. To test it “à la Artur” I left it untouched for a couple of months and this week I went back and see if I got it working without too much hassle.

It didn’t work. It sucked, got me tired and I accidentally managed to delete all the PHP code I had before. No harm done, sometimes it’s for the best! Anyway, as you might have figured it out by now, my main purpose is to write a python package that allows everyone to easily interact with 3dsmax and my “3dsmax connector” FB app or create their own and use that instead.

There are two separate components to this, the first one is the python module, which is basically what I’m going to discuss here today and then the PHP server-side authentication to get the user access token which I’ll follow up in a different post.

To interact with FB, posting messages, images, get user info, I just wrote a simple wrapper class around facepy, no need to re-invent the wheel! The tricky part, at least for me, was how to get the user logged in, accept the FB app permissions and the get the access token that facepy.GraphAPI(token) needs without having the AppID and AppSecret in the python source file 🙂

Now, if you know a better way to do this, PLEASE let me know! But for now the best way I could get this to work was to write a module where I make a request to a PHP page on my website and get the access token from there.

I used a simple HTTPServer with a custom handler to grab the access token back from the PHP. This way, we start a python http server, then we send over a request to our PHP server page, wait for it… wait for it…. until we get the access token in python back again and we’re done! All ready to make our FB interaction using maxfb.FbConnector().

After this it all becomes easier, check this simple snippet to post a message and an image with a caption:

import maxfb

if __name__ == '__main__':
    fbc = maxfb.FbConnector()

    print 'Your Facebook username is:' + fbc.getUsername()

    fbc.postMessage('3dsmax python connector with automatic authentication')
    fbc.postImage('A nice image', r'c:test.jpg')

I hope you find this useful! Don’t be afraid the first time you run the code, 3dsmax will freeze for a while for some reason I’m still trying to figure out (running it outside of 3dsmax, that issue does not exist).

DOWNLOAD

The PHP code is in the Externals folder in my YCDIVFX MaxPlus repository, feel free to dig into it since it’s very simple, you can also change it to match the settings of your FB app, just don’t forget to also change the auth_url in fbauth.py module. I need to handle all of this better but for now that’s it! Alot of room for improvement here.

I’ll post again to talk about the PHP code but it’s pretty easy and I tried to comment it.

Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.