Skip to content

Instantly share code, notes, and snippets.

@MikeUdin
Last active January 24, 2021 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeUdin/e4375fd1dd38a279d9fe82e6255ef847 to your computer and use it in GitHub Desktop.
Save MikeUdin/e4375fd1dd38a279d9fe82e6255ef847 to your computer and use it in GitHub Desktop.
How to create text file, read and write info
import c4d
from os import path as p
from c4d import gui
# Welcome to the world of Python
# Author: Mike Udin,
# Tutorial here https://mikeudin.net/2016/10/06/cinema-4d-python-tips-working-with-text-files/
# 2016
def main():
script_path,script_name = p.split(__file__) #split full script filepath on two vars
txt_fpath = p.join(script_path,'data_file.txt') #define textdata file path
if p.exists(txt_fpath) == False: #if textdata file not exists, create new
txt_file = open(txt_fpath,'w')
txt_file.write('#comment\n1 mike\n2 udin\n3 python\n4 cinema 4d')
txt_file.close()
c4d.storage.GeExecuteFile(txt_fpath)
return
txt_file = open(txt_fpath,'r')
data = txt_file.read()
txt_file.close()
for line in data.splitlines():
if '#' in line: continue
index,value = line.split(';')
obj = doc.SearchObject('title_'+index)
if obj:
obj[c4d.PRIM_TEXT_TEXT] = value
c4d.EventAdd()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment