xuv's notebook

Side notes, howtos and random bits of information related to Julien Deswaef's projects

User Tools

Site Tools


workshops:blender:loading_external_libraries

Loading external libraries

An example where we are going to use the requests library and the xml.etree library to pull some content from the NYTimes website and display it in Blender.

The finished script will look something like this:

nytimes.py
import bpy
import requests
from xml.etree import ElementTree as ElTree
from math import pi as PI   
 
# A function to draw some text    
def addText( t, loc, rot=(PI/2, 0, 0) ):
    bpy.ops.object.text_add( enter_editmode=True, location = loc, rotation = rot )
    bpy.ops.font.select_all()
    bpy.ops.font.delete(type='SELECTION')
    bpy.ops.font.text_insert(text=t)
    bpy.ops.object.editmode_toggle()
 
# Fetch the latest articles from NYTimes.com
req = requests.get('http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml')
 
if( req.status_code == 200):
    # if the server responded correctly to our request
    #print(req.headers)
    rss = ElTree.fromstring(req.text)
    #print(rss)
 
    for i, title in enumerate(rss.iter('title')):
        #print(str(i) + ' ' + title.text)
        if ( i > 1 ):
            addText(title.text, (0, 0, i))
workshops/blender/loading_external_libraries.txt · Last modified: 2015/06/20 23:40 by Julien Deswaef