def find_export_object(root):
# Recursively search for the object named 'Export' in the hierarchy
if root.GetName() == 'Export':
for child in root.GetChildren():
found_object = find_export_object(child)
if found_object is not None:
def export_to_csv(csv_filename, points):
# Open the CSV file in append mode
with open(csv_filename, 'a', newline='') as csvfile:
csvwriter = csv.writer(csvfile)
# Loop through each point and write its coordinates
csvwriter.writerow([x, y, z])
print(f'Data appended to {csv_filename}')
def get_next_filename(base_path, base_filename):
new_filename = f"{base_filename}_{version}.csv"
full_path = os.path.join(base_path, new_filename)
if not os.path.exists(full_path):
# Get the active document
doc = c4d.documents.GetActiveDocument()
c4d.gui.MessageDialog("No active document found.")
# Find the object named 'Export'
export_object = find_export_object(doc.GetFirstObject())
if export_object is None:
c4d.gui.MessageDialog("Object named 'Export' not found in the scene.")
# Specify the CSV file directory
base_path = r'C:\Users\Chris\OneDrive\Desktop\Test'
# Manually set the frames per second (FPS)
# Get the next available filename before the frame loop
csv_filename = get_next_filename(base_path, base_filename)
# Determine the start and end frames
start_frame = int(doc.GetMinTime().GetFrame(fps))
end_frame = int(doc.GetMaxTime().GetFrame(fps))
for frame in range(start_frame, end_frame + 1):
doc.SetTime(c4d.BaseTime(frame, fps))
result = c4d.utils.SendModelingCommand(
command=c4d.MCOMMAND_CURRENTSTATETOOBJECT,
deformed_object = result[0]
# Check if the deformed_object is a point-based object (e.g., spline, polygon object)
if deformed_object.GetType() == c4d.Opolygon or deformed_object.GetType() == c4d.Ospline:
points = deformed_object.GetAllPoints()
export_to_csv(csv_filename, points)
# Delete the temporary object after use
temp_doc = c4d.documents.BaseDocument()
temp_doc.InsertObject(deformed_object)
c4d.documents.KillDocument(temp_doc)
print("Error: Deformed object is not a point-based object.")
print("Error: No result from SendModelingCommand.")
print("Export completed.")
# Execute the main function
if __name__ == "__main__":