Skip to content

Instantly share code, notes, and snippets.

@gr4ph0s
Last active April 4, 2023 09:08
Show Gist options
  • Save gr4ph0s/6e54928233950bbd4e3e72036a203c62 to your computer and use it in GitHub Desktop.
Save gr4ph0s/6e54928233950bbd4e3e72036a203c62 to your computer and use it in GitHub Desktop.
[c4d]CSV import exemple
#CSV Importer exemple by graphos
#12/07/2017
#More informations about csv structure => https://www.c4dcafe.com/ipb/forums/topic/99062-can-objects-be-created-with-python/
import c4d
import csv
def add_mesh(num, name, r_h, p_x, p_y, outer_radius):
#Create obj in memory
disc_obj = c4d.BaseObject(c4d.Odisc)
poly_obj = c4d.BaseObject(c4d.Opolygon)
#Set obj names
disc_obj.SetName("{}-Alpha {}".format(str(num).zfill(2), name))
poly_obj.SetName("{}-{}".format(str(num).zfill(2), name))
#Set disc_parameter
disc_obj[c4d.PRIM_DISC_ORAD] = outer_radius
disc_obj[c4d.ID_BASEOBJECT_REL_ROTATION,c4d.VECTOR_X] = r_h
disc_obj[c4d.ID_BASEOBJECT_REL_POSITION,c4d.VECTOR_X] = p_x
disc_obj[c4d.ID_BASEOBJECT_REL_POSITION,c4d.VECTOR_Y] = p_y
#remove visiblity for poly_obj
poly_obj[c4d.ID_BASEOBJECT_VISIBILITY_RENDER] = 1
#insert disc into the doc
doc.InsertObject(disc_obj)
doc.AddUndo(c4d.UNDOTYPE_NEW, disc_obj)
#insert poly_obj under disc
poly_obj.InsertUnder(disc_obj)
doc.AddUndo(c4d.UNDOTYPE_NEW, poly_obj)
def main():
path = "F:\\test.csv"
doc.StartUndo()
with open(path, 'rb') as csv_file:
readed = csv.DictReader(csv_file, delimiter=',')
for i, row in enumerate(readed):
try:
name = str(row["ObjectName"])
r_h = float(row["R.H"][:-1]) #-1 for removing ° depand of the encoding of the file can be -2
p_x = float(row["P.X"])
p_y = float(row["P.Y"])
outer_radius = float(row["OuterRadius"])
except:
print "Error while reading - {}".format(row)
continue
r_h = c4d.utils.Rad(r_h) #convert in radian since c4d handle rotation in radian
add_mesh(i, name, r_h, p_x, p_y, outer_radius)
c4d.EventAdd()
doc.EndUndo()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment