Introduction to Python

Part 1 – Python Fundamentals

00 Hello World

print("Hello world!")

01 Hello World with Main

def main():
    print('Hello World!')

if __name__=='__main__':
    main()

Part 2 – Talking to Cinema 4D

02 Hello World C4D

import c4d
#Welcome to the world of Python

def main():
    c4d.gui.MessageDialog('Hello World!')

if __name__=='__main__':
    main()

03 Hello World MoText

import c4d
#Welcome to the world of Python

def main():
    MoText = c4d.BaseObject(1019268)
    MoText[c4d.PRIM_TEXT_TEXT] = "Hello world!"
    doc.InsertObject(MoText)
    c4d.EventAdd()

if __name__=='__main__':
    main()

Part 3 – Talking to Twitter

Note: To use these last two scripts, you’ll need to install the Python Twitter module. I’ll put up a blog post walking you through the process shortly. Also, the code below is slightly different from the example shown in the video. Instead of using the MyTwitterApi module to create a Twitter client, it uses the twitter module directly. In order to run these scripts, you’ll need to sign up at the Twitter Developers site and select “Create an app”. Once you’ve done this, use the authentication credentials generated by Twitter to replace the placeholder arguments for the twitter.Api() function call.

04 Tweet to MoText

import c4d
import twitter

def main():
    #Create a Python twitter client.
    client = twitter.Api(“consumer_key”, ”consumer_secret”, ”access_token”, ”access_token_secret”)

    #Get one tweet from the friends timeline.
    timeline = client.GetFriendsTimeline()
    tweet = timeline[0]
    
    #Create a string from the user name and text of the tweet.
    uText = "@" + tweet.user.screen_name + ": " + tweet.text
    text = uText.encode('utf-8','ignore')
    print text
    
    #Create a MoText object.
    MoText = c4d.BaseObject(1019268)
    
    #Set its text to show the tweet.
    MoText[c4d.PRIM_TEXT_TEXT] = text
    
    #Add it to the document.
    doc.InsertObject(MoText)
    c4d.EventAdd()

if __name__ == "__main__":
    main()

05 MoTweet

import c4d
import twitter

def main():
    #Create a Python twitter client.
    client = twitter.Api(“consumer_key”, ”consumer_secret”, ”access_token”, ”access_token_secret”)
    #Get tweets from the friends timeline.
    timeline = client.GetFriendsTimeline()
    #Create a "MoTweet" for each tweet.
    yPosition = 0
    for tweet in timeline:
        CreateMoTweet(tweet,yPosition)
        yPosition += 100
    #Update the object manager and the view.
    c4d.EventAdd()

def CreateMoTweet(tweet,yPos):
    #Create a MoText object.
    MoText = c4d.BaseObject(1019268)
    #Set its name as it will appear in the Object Manager.
    MoText[c4d.ID_BASELIST_NAME] = str(tweet.user.screen_name)
    #Generate the text we'll use for the MoText object.
    uText = "@" + tweet.user.screen_name + ": " + tweet.text
    text = uText.encode('utf-8', 'ignore')
    print text    
    MoText[c4d.PRIM_TEXT_TEXT] = text
    #Use number of followers to determine Z position.
    zPos = tweet.user.followers_count * 0.05
    #Set the object's position.
    MoText[c4d.ID_BASEOBJECT_REL_POSITION] = c4d.Vector(0,yPos,zPos)
    #Use the number of tweets to set the size of the text.
    MoText[c4d.PRIM_TEXT_HEIGHT] = 20 + tweet.user.statuses_count * 0.01
    MakePretty(MoText)
    #Add it to the current document.
    doc.InsertObject(MoText)

def MakePretty(MoText):
    #Set depth, rounding, and add phong tag.
    MoText[c4d.MGTEXTOBJECT_SPLINEMOVE] = MoText[c4d.PRIM_TEXT_HEIGHT] * 0.2
    MoText[c4d.CAP_START] = 3
    MoText[c4d.CAP_STARTSTEPS] = 3
    MoText[c4d.CAP_STARTRADIUS] =  MoText[c4d.PRIM_TEXT_HEIGHT] * 0.025
    MoText.MakeTag(c4d.Tphong)

if __name__ == '__main__':
    main()

Leave a Reply