# 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()
with open(pts_file,"r") as pts:
num_points = sum(1 for line in pts)
print("XYZ point count: " + str(num_points))
new_obj = c4d.PolygonObject(num_points,0)
vtx_color_tag = c4d.VertexColorTag(num_points)
vtx_color_tag.SetPerPointMode(True)
vtx_color_tag[c4d.ID_VERTEXCOLOR_DRAWPOINTS] = True
data = vtx_color_tag.GetDataAddressW()
points = new_obj.GetAllPoints()
normals = new_obj.GetAllPoints()
colors = new_obj.GetAllPoints()
pts.seek(0)
mult_z = 1.0
if INVERT_Z is True:
mult_z = -1.0
idx = 0
for line in pts:
values = line.strip().replace("\\n","").split(",")
print values
# read position values
points[idx] = c4d.Vector(float(values[0])/1E14,float(values[1])/1E14,float(values[2])/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)
new_obj.InsertTag(vtx_color_tag)
doc.InsertObject(new_obj)
c4d.EventAdd()
if __name__=='__main__':
main()
C4D Python自动添加顶点
于 2021-05-31 17:18:54 首次发布