PDA

View Full Version : Python Cinema 4D Data Exchange Machine


Continuumx
07-04-2012, 03:36 AM
Hello All,

I look for collaboration in sharing usable Python scripts for Cinema 4D that can parse various geometry and elements likes splines for point data that can be useful in other programs outside of Cinema 4d.

Please feel free to share as I will post my discoveries as well in this thread.

Happing Coding!

Continuumx
07-04-2012, 10:43 PM
https://img.skitch.com/20120705-tfk4b5f25k14wtmwc5g94cqrwh.jpg

The data in the structure menu of an editable object is a good indication that point data can be accessed via Python and setup to create a text based exporter.

This is a collaborative effort and should remain open source for all to help in any way possible. The results of all of this may be very useful in research and professional work.

Continuumx
07-04-2012, 11:20 PM
Hopefully the information for this simple spline test can be gathered via Python Script and custom formatted into text output. A final test will be to read this from the text file back into python for display in Cinema 4D.

https://img.skitch.com/20120705-tmi6qgpcskcgjcprcwwy6ebm5x.png

NiklasR
07-05-2012, 07:35 AM
Hello Continuumx,

I had a few minutes left to write this (http://pastebin.com/tCekDcuN) for you.

The export-format is simple.

- number of segments (in ascii) + newline
- number of points for the first segment (in ascii) + newline
- n-lines of comma-seperated float-values (in ascii) + newline
- - - each consists of 9 float-values
- - - (point.x, point.y, point.z, rt.x, rt.y, rt.z, lt.x, lt.y, lt.z)
- number of points for the second segment (in ascii) + newline
- ...

Example output for the circle-spline in XZ plane:

1
4
200.0000,0.0000,0.0000,0.0000,0.0000,83.0000,0.0000,0.0000,-83.0000
0.0000,0.0000,200.0000,-83.0000,0.0000,0.0000,83.0000,0.0000,-0.0000
-200.0000,0.0000,0.0000,-0.0000,0.0000,-83.0000,0.0000,0.0000,83.0000
-0.0000,0.0000,-200.0000,83.0000,0.0000,-0.0000,-83.0000,0.0000,0.0000

Cheers,
-Nik

Continuumx
07-05-2012, 02:41 PM
Hello Nick,

Thank you! I am going to study the script you wrote. This is very fascinating. I will see if I can get permission to share some more information about how this can be very useful going forward.

Regards,

ernia
07-05-2012, 02:45 PM
Sooo cool!
Danke Nux.

Continuumx
07-06-2012, 02:27 AM
Question 1: Is it possible to export point data from Cinema4D using python into a formatted excel file?

If not, this could be done by exporting to a text delimited file and then this file can be opened in excel and saved as an excel file.

Question 2: Is it possible to reference C# DLL that contains functions for exporting data to an excel file in Python as a module?

If so, then this could be used to write the excel file that is needed. If not, then most of the functions of the DLL will need to be recreated as a Python module for Cinema 4D.

Regards,

Scott Ayers
07-06-2012, 05:06 PM
There is an excel module you can download here: http://www.python-excel.org/

You simply copy&paste the xlwt folder from that download into your C4D packages folder.
On my Windows machine it's located here: C:\Users\user\AppData\Roaming\MAXON\CINEMA 4D version\library\python\packages\win64.
*If you use the 32 bit version of C4D. You need to put the folder in the 32bit packages folder.

Then you're all set up to use it: EXAMPLE: import c4d,os
import xlwt
from datetime import datetime

def main():
style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',
num_format_str='#,##0.00')
style1 = xlwt.easyxf(num_format_str='D-MMM-YY')

wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')

ws.write(0, 0, 1234.56, style0)
ws.write(1, 0, datetime.now(), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))

fn = c4d.storage.GeGetC4DPath(c4d.C4D_PATH_DESKTOP) #Gets the desktop path
path = os.path.join(fn,'mybook.xls') #The path to the .xls file

wb.save(path) #Saves the file

if __name__=='__main__':
main()

-ScottA

Continuumx
07-07-2012, 04:39 PM
Hello Scott,

This is very helpful and will be perfect for the next bit of information.

Continuumx
07-07-2012, 05:24 PM
Hello Scott,

This is very helpful and will be perfect for the next bit of information.

NiklasR
07-15-2012, 09:01 AM
As the script above only works for bezièr curves (thanks to ernia, I really forgot about that fact) here (http://pastebin.com/KsyWvdSF) is a version that works for every spline-type.
I've also changed a little-bit in the file-format to make it more human-readable.

This is the outcome of exporting a Text-spline with linear interepolation.

spline linear, 5 segments
8 points, closed
-133.15,0.00,0.00
-133.15,126.27,0.00
-180.32,126.27,0.00
-180.32,143.16,0.00
-66.85,143.16,0.00
-66.85,126.27,0.00
-114.21,126.27,0.00
-114.21,0.00,0.00
15 points, closed
-0.83,33.40,0.00
17.33,31.15,0.00
1.42,6.45,0.00
-28.27,-2.34,0.00
-64.36,11.67,0.00
-77.69,50.98,0.00
-64.21,91.60,0.00
-29.25,106.05,0.00
4.74,91.89,0.00
17.92,52.05,0.00
17.82,47.36,0.00
-59.52,47.36,0.00
-49.85,21.19,0.00
-28.17,12.11,0.00
-11.67,17.19,0.00
5 points, closed
-58.54,61.82,0.00
-0.63,61.82,0.00
-7.28,81.45,0.00
-29.05,91.60,0.00
-49.41,83.50,0.00
15 points, closed
27.69,0.00,0.00
65.58,53.91,0.00
30.52,103.71,0.00
52.49,103.71,0.00
68.41,79.39,0.00
75.63,67.77,0.00
83.54,79.20,0.00
101.03,103.71,0.00
122.02,103.71,0.00
86.18,54.88,0.00
124.76,0.00,0.00
103.17,0.00,0.00
81.88,32.23,0.00
76.22,40.92,0.00
48.97,0.00,0.00
20 points, closed
177.78,15.72,0.00
180.32,0.20,0.00
167.04,-1.37,0.00
152.20,1.66,0.00
144.78,9.62,0.00
142.63,30.37,0.00
142.63,90.04,0.00
129.74,90.04,0.00
129.74,103.71,0.00
142.63,103.71,0.00
142.63,129.39,0.00
160.11,139.94,0.00
160.11,103.71,0.00
177.78,103.71,0.00
177.78,90.04,0.00
160.11,90.04,0.00
160.11,29.39,0.00
161.04,19.73,0.00
164.06,16.31,0.00
170.07,15.04,0.00

Continuumx
07-21-2012, 07:24 PM
Thanks Niklas!

Continuumx
07-21-2012, 10:29 PM
I am stuck here in an attempt to get the ws.write into a functions of the excel for python module. This script does not do anything yet with C4D. I am working my way up to that but not quite there. I have tried a lot of things to pull the ws.write calls into a function with no success that actually writes the fixed data.

Here is the script so far:

import os
import c4d
import xlwt

#variables
id = 0
i = 1
spline_data1 = '100,100,100'
spline_data2 = '200,200,200'
action = 'Action'
object = 'Line'


def main():
#Header row information for excel file to be created
wb = xlwt.Workbook() #opens excel workbook for information
ws = wb.add_sheet('C4D Spline Point Output') #provides sheet information
#:::::Start Header Information
ws.write(0,0,'RowId')
ws.write(0,1,'ElementId')
ws.write(0,2,'Action')
ws.write(0,3,'Object')
ws.write(0,4,'Value01')
ws.write(0,5,'Value02')
ws.write(0,6,'Value03')
ws.write(0,7,'Value04')
#:::::End Header Information

#:::::Formatting
style0 = xlwt.easyxf('alignment: horizontal left')

#Data Write
#:::::ID Loop
ws.write(i,0,'%.5f' % (id+1),style0)
#:::::Action for REVIT
ws.write(i,2,action)
#:::::Object for REVIT
ws.write(i,3,object)
#:::::Data Point 0
ws.write(i,4,spline_data1)
#:::::Data Point 1 - Connecting Value
ws.write(i,5,spline_data2)
#:::::Repeat Spline ID Loop for Point 1 with Connecting Value to Point 2


path = c4d.storage.GeGetC4DPath(c4d.C4D_PATH_DESKTOP)
path = c4d.storage.SaveDialog(title="Export Data to Excel File", def_path=path)

if os.path.isdir(path):
c4d.MessageDialog("Selected path points to a directory.")
return

with open(path, 'w') as ws:
wb.save(path) #saves the file


if __name__ == "__main__":
main()

CGTalk Moderation
07-21-2012, 10:29 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.