On 16/05/2014 at 04:08, xxxxxxxx wrote:
Ok, here a small example how to read ascii data.
#the world of Python in CINEMA 4D
import c4d
from c4d import storage, gui
#
# read ascii file and create object
#
def ReadAscii() :
pointFile = "D:\Users\pgrooff\Documents\pim\c4d R15\import -export ascii\simple cube.txt"
try:
fpoints = open(pointFile)
except IOError, e:
gui.MessageDialog(e)
return
polysFile = "D:\Users\pgrooff\Documents\pim\c4d R15\import -export ascii\simple cube polys.txt"
try:
fpolys = open(polysFile)
except IOError, e:
gui.MessageDialog(e)
return
#split the file into line list
lines = fpoints.readlines()
#how many lines? so points
nrPoints = len(lines)
#split the file into line list
linesPolys = fpolys.readlines()
#how many lines? so polys
nrPolys = len(linesPolys)
#-1 for first line
print "Nr points-polys: ", nrPoints-1, nrPolys-1
#create polygon object with point count of lines and count of polygons
recube = c4d.BaseObject(c4d.Opolygon)
recube.ResizeObject(nrPoints-1, nrPolys-1)
#Points
for i, line in enumerate(lines) :
#split line into components
coord = line.split(",")
#ignore corrupt csv line
if len(coord)<4: continue
try:
nr = int(coord[0])
x = float(coord[1])
y = float(coord[2])
z = float(coord[3])
except ValueError, e:
continue
recube.SetPoint(i-1,c4d.Vector(x,y,z))
print "Point inserted: ", i-1,x,y,z
#Polys
for i, line in enumerate(linesPolys) :
#split line into components
coord = line.split(",")
#ignore corrupt csv line
if len(coord)<5: continue
try:
nr = int(coord[0])
p1 = int(coord[1])
p2 = int(coord[2])
p3 = int(coord[3])
p4 = int(coord[4])
except ValueError, e:
continue
recube.SetPolygon(nr,c4d.CPolygon(p1,p2,p3,p4))
print "Poly inserted: ", nr,x,y,z
doc.InsertObject(recube)
recube.Message (c4d.MSG_UPDATE)
c4d.EventAdd()
if __name__=='__main__':
ReadAscii()
I used the following input files. One for points and one for the polys.
I got these files by exporting a cube data to these files.
Later I edited the files, to add some ",".
But you can choose the format you want.
Point file:
Point X Y Z
0, -100, -100 , -100
1, -100 , 100 , -100
2, 100, -100 , -100
3, 100 , 100 , -100
4, 100 , -100 , 100
5, 100 , 100, 100
6, -100, -100, 100
7, -100 , 100 , 100
Polygon file:
Polygon A B C D
0, 0, 1, 3, 2
1, 2, 3, 5, 4
2, 4, 5, 7, 6
3, 6, 7, 1, 0
4, 1, 7, 5, 3
5, 6, 0, 2, 4