Posted on Leave a comment

Cinema 4D Python Tips: Working with text files

Сегодня узнаем как импортировать текстовые данные в Cinema 4D c помощью Python-скрипта.
Помимо этого поговорим о том, как производится “парсинг” текстовых данных.

Код скрипта:

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()

Leave a Reply