def write_frame_data(filePath, frame_data):
with open(filePath, "a") as file:
# Join all RGB values into a single line
line = ",".join(frame_data)
# Get the active Cinema 4D document
doc = c4d.documents.GetActiveDocument()
# Find the Matrix object by name 'Export'
matrix_object = doc.SearchObject('Export')
# Check if the Matrix object is correctly referenced
print("Matrix object named 'Export' not found")
# Get the grid resolution vector
grid_resolution = matrix_object[c4d.MG_GRID_RESOLUTION]
if grid_resolution is None:
print("Grid resolution data not found")
# Extract the X and Y values (assuming Y is the second dimension in your grid)
x_grid_size = int(grid_resolution.x)
y_grid_size = int(grid_resolution.y) # Or z if your grid is in the XZ plane
# Determine the range of frames to iterate over
startFrame = doc.GetLoopMinTime().GetFrame(doc.GetFps())
endFrame = doc.GetLoopMaxTime().GetFrame(doc.GetFps())
numberOfFrames = endFrame - startFrame + 1
# Amend the file path with the number of frames and grid sizes
filePath = f"H:/My Drive/02_Projects/41_SPTF_FCBNY Spotify/01_Production/C4D/240205_WriteValuesToExcel/Written/matrix_colors_{numberOfFrames}frames_{x_grid_size}x{y_grid_size}grid.csv"
# Iterate over each frame in the range
for frame in range(startFrame, endFrame + 1):
# Set the document's time to the current frame
doc.SetTime(c4d.BaseTime(frame, doc.GetFps()))
# Update the document to reflect the change in time
c4d.DrawViews(c4d.DA_ONLY_ACTIVE_VIEW | c4d.DA_NO_THREAD | c4d.DA_STATICBREAK)
c4d.GeSyncMessage(c4d.EVMSG_TIMECHANGED)
doc.ExecutePasses(None, True, True, True, c4d.BUILDFLAGS_0)
md = c4d.modules.mograph.GeGetMoData(matrix_object)
print("MoGraph data not found for frame", frame)
# Get the array of colors of the clones for the current frame
colors = md.GetArray(c4d.MODATA_COLOR)
print("Color data not found for frame", frame)
# Convert color values to 0-255 range and format them for CSV
frame_data = [f"{int(color.x*255)}, {int(color.y*255)}, {int(color.z*255)}" for color in colors]
# Write the current frame's color data to the file
write_frame_data(filePath, frame_data)
# Print a message for confirmation
print(f"Color data for frames {startFrame} to {endFrame} written to file")
# Execute the main function