读取文本文档中的数据,每行为两个点的坐标:x1,y1,z1,x2,y2,z2,在场景中批量生成样条线
# XYZ to PolygonObject
# 908video / Michael Auerswald
# 908video.de / 908lab.de
import c4d
from c4d import gui
INVERT_Z = False
SCALE = 1.0
def main():
# Get input file
pts_file = c4d.storage.LoadDialog(title="Open XYZ file for processing", flags=c4d.FILESELECT_LOAD, def_path="d:\\")
if pts_file is None:
return # cancelled
doc = c4d.documents.GetActiveDocument()
lines=[]
lid=0
with open(pts_file,"r") as pts:
num_points = sum(1 for line in pts)
print("XYZ point count: " + str(num_points))
pts.seek(0)
mult_z = 1.0
if INVERT_Z is True:
mult_z = -1.0
idx = 0
for line in pts:
lines.append(c4d.SplineObject(2,1))
new_obj = lines[lid]
lid=lid+1
points = new_obj.GetAllPoints()
values = line.strip().replace("\\n","").split(",")
print values
# read position values
points[0] = c4d.Vector(float(values[0])/1E14,float(values[1])/1E14,float(values[2])/1E14*mult_z) * SCALE
points[1] = c4d.Vector(float(values[3])/1E14,float(values[4])/1E14,float(values[5])/1E14*mult_z) * SCALE
"""
# since XYZ has no defined order of these columns, these columns may or may not exist and can contain normals, color or whatever
# read second column of values
if len(values) > 3:
colors[idx] = c4d.Vector(float(values[3])/255.0,float(values[4])/255.0,float(values[5])/255.0)
c4d.VertexColorTag.SetColor(data, None, None, idx, colors[idx])
#normals[idx] = c4d.Vector(float(values[3]),float(values[4]),float(values[5]))
# read third column of values
if len(values) > 6:
normals[idx] = c4d.Vector(float(values[6]),float(values[7]),float(values[8]))
"""
# increase index
idx += 1
new_obj.SetAllPoints(points)
doc.InsertObject(new_obj)
c4d.EventAdd()
if __name__=='__main__':
main()