FreeCAD Logo FreeCAD 1.0
  • English Afrikaans Arabic Belarusian Catalan Czech German Greek Spanish Spanish Basque Finnish Filipino French Galician Croatian Hungarian Indonesian Italian Japanese Kabyle Korean Lithuanian Dutch Norwegian Bokmal Polish Portuguese Portuguese Romanian Russian Slovak Slovenian Serbian Swedish Turkish Ukrainian Valencian Vietnamese Chinese Chinese
  • Features
  • Download
  • Blog
  • Documentation
    Documentation index Getting started Users documentation The FreeCAD manual Workbenches documentation Python coding documentation C++ coding documentation Tutorials Frequently asked questions Privacy policy About FreeCAD
  • Contribute
    How to help Sponsor Report a bug Make a pull request Jobs and funding Contribution guidelines Developers handbook Translations
  • Community
    Code of conduct Forum The FPA GitHub GitLab Codeberg Mastodon Matrix IRC IRC via Webchat Gitter Discord Reddit Twitter Facebook LinkedIn Calendar
  • ♥ Donate

Donate

$
SEPA Information
Please set up your SEPA bank transfer to:
Beneficiary: The FreeCAD project association
IBAN: BE04 0019 2896 4531
BIC/SWIFT: GEBABEBBXXX
Bank agency: BNP Paribas Fortis
Address: Rue de la Station 64, 1360 Perwez, Belgium

While Stripe doesn't support monthly donations, you can still become a sponsor! Simply make a one-time donation equivalent to 12 months of support, and you'll gain access to the corresponding sponsoring tier. It's an easy and flexible way to contribute.

If you are not sure or not able to commit to a regular donation, but still want to help the project, you can do a one-time donation, of any amount.

Choose freely the amount you wish to donate one time only.

You can support FreeCAD by sponsoring it as an individual or organization through various platforms. Sponsorship provides a steady income for developers, allowing the FPA to plan ahead and enabling greater investment in FreeCAD. To encourage sponsorship, we offer different tiers, and unless you choose to remain anonymous, your name or company logo will be featured on our website accordingly.

from 1 USD / 1 EUR per month. You will not have your name displayed here, but you will have helped the project a lot anyway. Together, normal sponsors maintain the project on its feet as much as the bigger sponsors.

from 25 USD / 25 EUR per month. Your name or company name is displayed on this page.

from 100 USD / 100 EUR per month. Your name or company name is displayed on this page, with a link to your website, and a one-line description text.

from 200 USD / 200 EUR per month. Your name or company name and logo displayed on this page, with a link to your website and a custom description text. Companies that have helped FreeCAD early on also appear under Gold sponsors.

Instead of donating each month, you might find it more comfortable to make a one-time donation that, when divided by twelve, would give you right to enter a sponsoring tier. Don't hesitate to do so!

Choose freely the amount you wish to donate each month.

Please inform your forum name or twitter handle as a notein your transfer, or reach to us, so we can give you proper credits!

Macro FreeCAD to Kerkythea

{{Macro |Name=FC to Kerkythea |Icon=Macro_FreeCAD_to_Kerkythea.png |Description=Macro to export your model to the raytracing program [http://www.kerkythea.net/cms/ Kerkythea]. |Author=marmni |Version=1.0 |Date=2015-05-30 |FCVersion=0.17 and above |Download=[https://www.freecadweb.org/wiki/https://raw.githubusercontent.com/FreeCAD/FreeCAD-documentation/master/wiki/images/8/8c/Macro_FreeCAD_to_Kerkythea.png ToolBar Icon] }}

Description

Macro to export your model to the raytracing program Kerkythea.

Usage

The macro you can find in the following github repository: FreeCAD to Kerkythea-Exporter

It is pretty self-explanatory. At the moment there are problems with exporting lights and the camera position.

Link

The page discussion Kerkythea Rendering System

Script

ToolBar Icon

Macro ExportToKerkythea.py

{{MacroCode|code=

-- coding: utf8 --

**

Kerkythea exporter

Copyright (c) 2014,2015

marmni marmni@onet.eu

This program is free software; you can redistribute it and/or modify

it under the terms of the GNU Lesser General Public License (LGPL)

as published by the Free Software Foundation; either version 2 of

the License, or (at your option) any later version.

for detail see the LICENCE text file.

This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public

License along with this program; if not, write to the Free Software

Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307

USA

**

**

BASED ON

IOANNIS PANTAZOPOULOS

Sample code exporting basic Kerkythea XML file.

Version v1.0

**

title="Kerkythea exporter" author = "marmni marmni@onet.eu" url = ["http://www.freecadweb.org"]

import FreeCAD, FreeCADGui import re import random import builtin import Mesh from PySide import QtCore, QtGui import sys import os

##############################################

############################################## class point3D: def init(self, point): self.x = "%.4f" % (point[0] 0.001) self.y = "%.4f" % (point[1] 0.001) self.z = "%.4f" % (point[2] * 0.001)

def __str__(self):
    return '<P xyz="{0} {1} {2}"/>'.format(self.x, self.y, self.z)

#def __eq__(self, other):
    #if self.x == other.x and self.y == other.y and self.z == other.z:
        #return True
    #else:
        #return False 

##############################################

############################################## class indexListPoint3D: def init(self, point): self.i = point[0] self.j = point[1] self.k = point[2]

def __str__(self):
    return '<F ijk="{0} {1} {2}"/>'.format(self.i, self.j, self.k)

##############################################

############################################## class Material: def init(self): self.diffuse = None # Texture() self.shininess = 1000.0 self.ior = 2.0

def write(self, file):
    file.write('''<Object Identifier="Whitted Material" Label="Whitted Material" Name="" Type="Material">\n''')

    self.diffuse.write(file, "Diffuse")
    self.diffuse.write(file, "Translucent")
    self.diffuse.write(file, "Specular")
    self.diffuse.write(file, "Transmitted")

    file.write('''<Parameter Name="Shininess" Type="Real" Value="{shininess}"/>
\n'''.format(shininess=self.shininess, ior=self.ior))

##############################################

############################################## class Texture: def init(self, color): self.color = color

def getColorSTR(self):
    return '{0} {1} {2}'.format(self.color[0], self.color[1], self.color[2])

def toGrayscale(self):
    RGB = 0.299 * self.color[0] + 0.587 * self.color[1] + 0.114 * self.color[2]
    self.color = [RGB, RGB, RGB]

def write(self, file, identifier):
    file.write('''<Object Identifier="./{identifier}/Constant Texture" Label="Constant Texture" Name="" Type="Texture">
\n'''.format(identifier=identifier, color=self.getColorSTR()))

##############################################

############################################## class Model: def init(self): self.vertexList = [] self.normalList = [] self.indexList = []

    self.name = self.wygenerujID(5, 5)
    self.material = Material()

def addFace(self, face):
    mesh = self.meshFace(face)
    for pp in mesh.Facets:
        num = len(self.vertexList)
        for kk in pp.Points:
            self.vertexList.append(point3D(kk))
        self.indexList.append(indexListPoint3D([num, num + 1, num + 2]))

def meshFace(self, shape):
    faces = []
    triangles = shape.tessellate(1) # the number represents the precision of the tessellation
    for tri in triangles[1]:
        face = []
        for i in range(3):
            vindex = tri[i]
            face.append(triangles[0][vindex])
        faces.append(face)
    m = Mesh.Mesh(faces)
    #Mesh.show(m)
    return m

def wygenerujID(self, ll, lc):
    ''' generate random model name '''
    numerID = ""

    for i in range(ll):
        numerID += random.choice('abcdefghij')
    numerID += "_"
    for i in range(lc):
        numerID += str(random.randrange(0, 99, 1))

    return numerID

def write(self, file):
    file.write('''
\n'''.format(name=self.name, pointListSize=len(self.vertexList)))
    for i in self.vertexList:
        file.write('{0}\n'.format(i))

    file.write('''</Parameter>
\n'''.format(pointListSize=len(self.vertexList))) file.write('

\n' * len(self.vertexList)) file.write(''' \n'''.format(indexListSize=len(self.indexList)))

    for i in self.indexList:
        file.write('{0}\n'.format(i))

    file.write('''</Parameter>\n</Object>\n''')

    self.material.write(file)

    file.write('</Object>\n')

##############################################

############################################## class Camera: def init(self): self.name = "Camera_1" self.f_number = "Pinhole" self.resolution = "1024x768" self.focusDistance = "1" self.lensSamples = "3" self.blades = "3" self.diaphragm = "Circular" self.projection = "Planar"

def addParameter(self, name, pType, value):
    return '<Parameter Name="{0}" Type="{1}" Value="{2}"/>\n'.format(name, pType, value)

def write(self, file):
    cam = FreeCADGui.ActiveDocument.ActiveView.getCameraNode()
    camValues = cam.position.getValue()

    x = "%.4f" % (30 * .0254)
    y = "%.4f" % (30 * .0254)
    z = "%.4f" % (30 * .0254)

    file.write('<Object Identifier="./Cameras/{0}" Label="Pinhole Camera" Name="{0}" Type="Camera">\n'.format(self.name))
    file.write(self.addParameter("Resolution", "String", self.resolution))
    file.write(self.addParameter("Frame", "Transform", "-0.609474 0.471636 -0.63726 {0} 0.792806 0.362576 -0.489895 {1} 2.70762e-06 -0.803802 -0.594897 {2}".format(x, y, z)))
    file.write(self.addParameter("Focus Distance", "Real", self.focusDistance))
    file.write(self.addParameter("f-number", "String", self.f_number))
    file.write(self.addParameter("Lens Samples", "Integer",self.lensSamples ))
    file.write(self.addParameter("Blades", "Integer", self.blades))
    file.write(self.addParameter("Diaphragm", "String", self.diaphragm))
    file.write(self.addParameter("Projection", "String", self.projection))
    file.write('</Object>\n')

##############################################

############################################## class exportTokerkythea: def init(self): self.models = [] self.cameras = [] modelsMultiColors = False

def write(self, file, name):
    file = __builtin__.open(file, "w")
    #
    self.writeHeader(file, name)

    if self.modelsMultiColors:
        for i, j in self.models.items():
            file.write('<Object Identifier="./Models/{0}" Label="Default Model" Name="{0}" Type="Model">\n'.format(i))
            for k in j:
                k.write(file)
            file.write('</Object>\n')
    else:
        for i in self.models.values():
            i.write(file)
    # CAMERA
    activeCamera = self.cameras[0][0].name
    for i in self.cameras:
        i[0].write(file)
        if i[1]:
            activeCamera = i[0].name
    #
    file.write('<Parameter Name="./Cameras/Active" Type="String" Value="{0}"/>\n'.format(activeCamera))

    self.writeFooter(file, name)

def writeHeader(self, file, name):
    file.write('''<Root Label="Kernel" Name="" Type="Kernel">
\n '''.format(name))
def writeFooter(self, file, name):
    file.write('''</Object>
'''.format(name))

##############################################

##############################################

class exportKerkytheaDialog(QtGui.QDialog): def init(self, parent=None): QtGui.QDialog.init(self, parent)

    self.setWindowTitle(u'Export to Kerkythea v1.1')
    self.setFixedSize(500, 550)

    #
    self.buttonAccept = QtGui.QPushButton('Accept')
    #
    tab = QtGui.QTabWidget()
    tab.addTab(self.addGeneralPage(), u'General')
    tab.addTab(self.addLightsPage(), u'Light')
    tab.addTab(self.addCmerasPage(), u'Camera')
    #
    lay = QtGui.QVBoxLayout(self)
    lay.addWidget(tab)
    lay.addWidget(self.buttonAccept)

def changePathF(self):
    path = QtGui.QFileDialog().getSaveFileName(self, u"Save as", os.path.expanduser("~"), "*.xml")

    fileName = path[0]
    if not fileName == "":
        if not fileName.endswith('xml'):
            fileName = fileName + '.xml'
        self.filePath.setText(fileName)

def addGeneralPage(self):
    self.filePath = QtGui.QLineEdit(os.path.join(os.path.expanduser("~"), 'Unnamed.xml'))
    self.filePath.setReadOnly(True)

    changePath = QtGui.QPushButton('...')
    changePath.setFixedWidth(30)
    self.connect(changePath, QtCore.SIGNAL("clicked ()"), self.changePathF)

    generalBox = QtGui.QGroupBox(u'General')
    generalBoxLay = QtGui.QGridLayout(generalBox)
    generalBoxLay.addWidget(QtGui.QLabel(u'Path           '), 0, 0, 1, 1)
    generalBoxLay.addWidget(self.filePath, 0, 1, 1, 2)
    generalBoxLay.addWidget(changePath, 0, 3, 1, 1)

    generalBoxLay.setColumnStretch(1, 10)
    #
    self.exportObjects_All = QtGui.QRadioButton(u'All visible objects')
    self.exportObjects_All.setChecked(True)
    self.exportObjects_Selected = QtGui.QRadioButton(u'All selected objects')
    self.exportObjects_SelectedFaces = QtGui.QRadioButton(u'All selected faces')
    self.exportObjects_SelectedFaces.setDisabled(True)

    exportObjectsBox = QtGui.QGroupBox(u'Export objects')
    exportObjectsBoxLay = QtGui.QVBoxLayout(exportObjectsBox)
    exportObjectsBoxLay.addWidget(self.exportObjects_All)
    exportObjectsBoxLay.addWidget(self.exportObjects_Selected)
    exportObjectsBoxLay.addWidget(self.exportObjects_SelectedFaces)
    #
    self.exportObjectsAs_YES = QtGui.QRadioButton(u'Yes')
    self.exportObjectsAs_YES.setChecked(True)
    self.exportObjectsAs_NO = QtGui.QRadioButton(u'No')

    exportObjectsAsBox = QtGui.QGroupBox(u'Group models by color')
    exportObjectsAsBoxLay = QtGui.QVBoxLayout(exportObjectsAsBox)
    exportObjectsAsBoxLay.addWidget(self.exportObjectsAs_YES)
    exportObjectsAsBoxLay.addWidget(self.exportObjectsAs_NO)
    #
    self.exportObjectColor_MulCol = QtGui.QRadioButton(u'Multi colors')
    self.exportObjectColor_Gray = QtGui.QRadioButton(u'Grayscale')
    self.exportObjectColor_SinCol = QtGui.QRadioButton(u'Single color (random)')
    self.exportObjectColor_SinCol.setChecked(True)

    self.exportObjectColorBox = QtGui.QGroupBox(u'Colors')
    self.exportObjectColorBox.setDisabled(True)
    exportObjectColorBoxLay = QtGui.QVBoxLayout(self.exportObjectColorBox)
    exportObjectColorBoxLay.addWidget(self.exportObjectColor_MulCol)
    exportObjectColorBoxLay.addWidget(self.exportObjectColor_Gray)
    exportObjectColorBoxLay.addWidget(self.exportObjectColor_SinCol)
    #####
    widget = QtGui.QWidget()

    lay = QtGui.QGridLayout(widget)
    lay.addWidget(generalBox, 0, 0, 1, 4)
    lay.addWidget(exportObjectsBox, 1, 0, 1, 4)
    lay.addWidget(exportObjectsAsBox, 2, 0, 1, 2)
    lay.addWidget(self.exportObjectColorBox, 2, 2, 1, 2)

    lay.setRowStretch(10, 10)
    #lay.setColumnStretch(10, 10)
    return widget

def addLightsPage(self):
    widget = QtGui.QWidget()

    return widget

def addCmerasPage(self):
    self.resolution = QtGui.QComboBox()
    self.resolution.addItems(['200x200', '320x200', '320x240', '500x500', '512x384', '640x480', '768x576', '800x600', '1024x768', '1280x1024', '1600x1200', '2048x1536', '2816x2112'])
    self.resolution.setCurrentIndex(self.resolution.findText('1024x768'))

    self.cameraName = QtGui.QLineEdit(u'Camera 1')

    filmBox = QtGui.QGroupBox(u'General')
    filmBoxLay = QtGui.QGridLayout(filmBox)
    filmBoxLay.addWidget(QtGui.QLabel(u'Camera name'), 0, 0, 1, 1)
    filmBoxLay.addWidget(self.cameraName, 0, 1, 1, 1)
    filmBoxLay.addWidget(QtGui.QLabel(u'Resolution'), 1, 0, 1, 1)
    filmBoxLay.addWidget(self.resolution, 1, 1, 1, 1)

    filmBoxLay.setHorizontalSpacing(50)
    #
    self.fNumber = QtGui.QComboBox()
    self.fNumber.addItems(['1', '1.4', '2', '2.8', '4', '5.6', '8', '16', '22', 'Pinhole'])
    self.fNumber.setCurrentIndex(self.fNumber.findText('Pinhole'))

    self.focusDistance = QtGui.QDoubleSpinBox()
    self.focusDistance.setValue(1.0)
    self.focusDistance.setRange(0.0, 1000.0)

    self.lensSamples = QtGui.QSpinBox()
    self.lensSamples.setValue(3)
    self.lensSamples.setRange(0, 1000)

    lensBox = QtGui.QGroupBox(u'Lens')
    lensBoxLay = QtGui.QGridLayout(lensBox)
    lensBoxLay.addWidget(QtGui.QLabel(u'f-number'), 0, 0, 1, 1, QtCore.Qt.AlignHCenter)
    lensBoxLay.addWidget(self.fNumber, 1, 0, 1, 1)
    lensBoxLay.addWidget(QtGui.QLabel(u'Focus Distance'), 0, 1, 1, 1, QtCore.Qt.AlignHCenter)
    lensBoxLay.addWidget(self.focusDistance, 1, 1, 1, 1)
    lensBoxLay.addWidget(QtGui.QLabel(u'Lens Samples'), 0, 2, 1, 1, QtCore.Qt.AlignHCenter)
    lensBoxLay.addWidget(self.lensSamples, 1, 2, 1, 1)

    lensBoxLay.setHorizontalSpacing(50)
    #
    self.projection = QtGui.QComboBox()
    self.projection.addItems(['Planar', 'Cylindrical', 'Spherical', 'Parallel'])
    self.projection.setCurrentIndex(self.projection.findText('Planar'))

    self.diaphragm = QtGui.QComboBox()
    self.diaphragm.addItems(['Circular', 'Polygonal'])
    self.diaphragm.setCurrentIndex(self.diaphragm.findText('Circular'))

    self.blades = QtGui.QSpinBox()
    self.blades.setValue(3)
    self.blades.setRange(3, 1000)

    geometryBox = QtGui.QGroupBox(u'Geometry')
    geometryBoxLay = QtGui.QGridLayout(geometryBox)
    geometryBoxLay.addWidget(QtGui.QLabel(u'Projection'), 0, 0, 1, 1, QtCore.Qt.AlignHCenter)
    geometryBoxLay.addWidget(self.projection, 1, 0, 1, 1)
    geometryBoxLay.addWidget(QtGui.QLabel(u'Diaphragm'), 0, 1, 1, 1, QtCore.Qt.AlignHCenter)
    geometryBoxLay.addWidget(self.diaphragm, 1, 1, 1, 1)
    geometryBoxLay.addWidget(QtGui.QLabel(u'Blades'), 0, 2, 1, 1, QtCore.Qt.AlignHCenter)
    geometryBoxLay.addWidget(self.blades, 1, 2, 1, 1)

    geometryBoxLay.setHorizontalSpacing(50)
    #
    #####
    widget = QtGui.QWidget()
    lay = QtGui.QVBoxLayout(widget)
    lay.addWidget(filmBox)
    lay.addWidget(lensBox)
    lay.addWidget(geometryBox)
    lay.addStretch(10)

    return widget

class exportKerkythea(exportKerkytheaDialog): def init(self, parent=None): exportKerkytheaDialog.init(self, parent)

    self.connect(self.buttonAccept, QtCore.SIGNAL("clicked ()"), self.acceptw)
    self.connect(self.exportObjectsAs_YES, QtCore.SIGNAL("clicked ()"), self.setColors)
    self.connect(self.exportObjectsAs_NO, QtCore.SIGNAL("clicked ()"), self.setColors)

def setColors(self):
    if self.exportObjectsAs_YES.isChecked():
        self.exportObjectColor_SinCol.setChecked(True)
        self.exportObjectColorBox.setDisabled(True)
    else:
        self.exportObjectColorBox.setDisabled(False)

def acceptw(self):
    if self.exportObjects_All.isChecked():
        projectObjects = [i for i in FreeCAD.ActiveDocument.Objects if i.ViewObject.Visibility]
    elif self.exportObjects_Selected.isChecked():
        projectObjects = []
        for i in FreeCADGui.Selection.getSelection():
            if i.ViewObject.Visibility and i not in projectObjects:
                projectObjects.append(i)
    #

    projectModels = {}
    for i in projectObjects:  # objects in document
        try:
            objectColors = i.ViewObject.DiffuseColor
            shape = i.Shape.Faces
        except:
            continue

        for j in range(len(i.Shape.Faces)):  # object faces
            # get face color
            if len(objectColors) == len(i.Shape.Faces):
                modelType = objectColors[j]
            else:
                modelType = objectColors[0]
            #
            if self.exportObjectsAs_YES.isChecked():
                modelID = str(modelType)
            else:
                modelID = i.Label
            #
            if self.exportObjectColor_SinCol.isChecked():
                modelsMultiColors = False

                if not modelID in projectModels:
                    model = Model()
                    if self.exportObjectsAs_NO.isChecked():
                        model.name = modelID
                    model.material.diffuse = Texture(modelType)
                    projectModels[modelID] = model
                else:
                    model = projectModels[modelID]
            else:
                modelsMultiColors = True

                if not modelID in projectModels:
                    projectModels[modelID] = []

                model = Model()
                model.name = 'Face {0}'.format(j)
                model.material.diffuse = Texture(modelType)
                if self.exportObjectColor_Gray.isChecked():
                    model.material.diffuse.toGrayscale()
                projectModels[modelID].append(model)

model = Model()

model.material.diffuse = Texture(modelType)

projectModels[i.Label].append(model)

            #
            model.addFace(i.Shape.Faces[j])

    exporter = exportTokerkythea()
    exporter.modelsMultiColors = modelsMultiColors
    exporter.models = projectModels
    # CAMERA
    camera = Camera()
    camera.name = self.cameraName.text()
    camera.f_number = self.fNumber.currentText()
    camera.resolution = self.resolution.currentText()
    camera.focusDistance = self.focusDistance.value()
    camera.lensSamples = self.lensSamples.value()
    camera.blades = self.blades.value()
    camera.diaphragm = self.diaphragm.currentText()
    camera.projection = self.projection.currentText()

    exporter.cameras.append([camera, True])
    #
    exporter.write(self.filePath.text(), FreeCAD.ActiveDocument.Label)

    self.close()

exportKerkythea().exec_()

}}


??? documentation index > Macro FreeCAD to Kerkythea

This page is retrieved from https://github.com/FreeCAD/FreeCAD-documentation/blob/main/wiki/Macro_FreeCAD_to_Kerkythea.md

Get in touch!
Forum GitHub Mastodon Matrix IRC Gitter.im Discord Reddit Twitter Facebook LinkedIn

© The FreeCAD Team. Homepage image credits (top to bottom): ppemawm, r-frank, epileftric, regis, rider_mortagnais, bejant.

This project is supported by: , KiCad Services Corp. and other sponsors

GitHubImprove this page on GitHub