-
follow:Follow updates
Python is a powerful, easy to learn programming language that is deeply integrated with Cinema 4D. But if you're a designer, chances are that you've been hesitant to dive into the world of code. The following videos don't assume any programming background, so you should be able to follow them even if you've never written a line of code. I use a series of simple projects to introduce you to the basics of Python and show you how it opens up a whole new world of possibilities in Cinema 4D.
These videos are based on the talk I gave at our September Chicago C4D meeting. I've included the sample code used in each video. This is the first in an ongoing series of Python tutorials. So if you have any questions, or a request for a specific tutorial, let me know in the comments below. And to hear about new tutorials as they're released, follow me at @BuiltLight. Have fun!
print("Hello world!")01 Hello World with Main
def main(): print('Hello World!') if __name__=='__main__': main()
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()
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 MoTextimport 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()
Comments
- How do you save scripts you make somewhere?
- How do you make Python in Cinema 4D aware of external Python libraries, like the twitter one?
- Where can you get a list of all the objects and functions in Cinema 4D Python object?
- Is everthing in Cinema 4D available as Python objects?
- Can you extend Cinema 4D through Python by creating plugins or something similar?
RSS feed for comments to this post