Last active
March 21, 2017 20:20
-
-
Save yorikvanhavre/2f0e160438b14ac9c31a2fa5117126a2 to your computer and use it in GitHub Desktop.
A sketchfab exporter for FreeCAD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#*************************************************************************** | |
#* * | |
#* Copyright (c) 2017 - Yorik van Havre <[email protected]> * | |
#* * | |
#* 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 * | |
#* * | |
#*************************************************************************** | |
from __future__ import print_function | |
__title__ = "Sketchfab uploader" | |
__author__ = "Yorik van Havre" | |
__url__ = "http://www.freecadweb.org" | |
"""This macro is the same version of the sketchfab exporter found in the Web Workbench of FreeCAD | |
starting from FreeCAD v0.17. This marco is meant to be used with older versions of FreeCAD that still | |
don't have the tool""" | |
import FreeCAD, FreeCADGui, WebGui, os, zipfile, requests, tempfile, json, time, re | |
from PySide import QtCore, QtGui | |
try: | |
_fromUtf8 = QtCore.QString.fromUtf8 | |
except AttributeError: | |
def _fromUtf8(s): | |
return s | |
try: | |
def translate(context, text, disambig=None): | |
return QtGui.QApplication.translate(context, text, disambig, QtGui.QApplication.UnicodeUTF8) | |
except AttributeError: | |
def translate(context, text, disambig=None): | |
return QtGui.QApplication.translate(context, text, disambig) | |
SKETCHFAB_UPLOAD_URL = "https://api.sketchfab.com/v3/models" | |
SKETCHFAB_TOKEN_URL = "https://sketchfab.com/settings/password" | |
SKETCHFAB_MODEL_URL = "https://sketchfab.com/show/" | |
class Ui_Form(object): | |
def setupUi(self, Form): | |
Form.setObjectName(_fromUtf8("Form")) | |
Form.resize(218, 548) | |
Form.verticalLayout = QtGui.QVBoxLayout(Form) | |
Form.verticalLayout.setObjectName(_fromUtf8("verticalLayout")) | |
Form.label = QtGui.QLabel(Form) | |
Form.label.setMaximumSize(QtCore.QSize(200, 43)) | |
Form.label.setText(_fromUtf8("")) | |
Form.label.setPixmap(QtGui.QPixmap(_fromUtf8(":/icons/Sketchfab.svg"))) | |
Form.label.setScaledContents(True) | |
Form.label.setObjectName(_fromUtf8("label")) | |
Form.verticalLayout.addWidget(Form.label) | |
Form.label_7 = QtGui.QLabel(Form) | |
Form.label_7.setObjectName(_fromUtf8("label_7")) | |
Form.verticalLayout.addWidget(Form.label_7) | |
Form.Radio_Selection = QtGui.QRadioButton(Form) | |
Form.Radio_Selection.setChecked(True) | |
Form.Radio_Selection.setObjectName(_fromUtf8("Radio_Selection")) | |
Form.verticalLayout.addWidget(Form.Radio_Selection) | |
Form.Radio_Document = QtGui.QRadioButton(Form) | |
Form.Radio_Document.setObjectName(_fromUtf8("Radio_Document")) | |
Form.verticalLayout.addWidget(Form.Radio_Document) | |
spacerItem = QtGui.QSpacerItem(20, 20, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed) | |
Form.verticalLayout.addItem(spacerItem) | |
Form.horizontalLayout_2 = QtGui.QHBoxLayout() | |
Form.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2")) | |
Form.label_3 = QtGui.QLabel(Form) | |
Form.label_3.setObjectName(_fromUtf8("label_3")) | |
Form.horizontalLayout_2.addWidget(Form.label_3) | |
Form.Check_Private = QtGui.QCheckBox(Form) | |
Form.Check_Private.setObjectName(_fromUtf8("Check_Private")) | |
Form.horizontalLayout_2.addWidget(Form.Check_Private, QtCore.Qt.AlignRight) | |
Form.verticalLayout.addLayout(Form.horizontalLayout_2) | |
Form.Text_Name = QtGui.QLineEdit(Form) | |
Form.Text_Name.setObjectName(_fromUtf8("Text_Name")) | |
Form.verticalLayout.addWidget(Form.Text_Name) | |
Form.label_4 = QtGui.QLabel(Form) | |
Form.label_4.setObjectName(_fromUtf8("label_4")) | |
Form.verticalLayout.addWidget(Form.label_4) | |
Form.Text_Description = QtGui.QLineEdit(Form) | |
Form.Text_Description.setObjectName(_fromUtf8("Text_Description")) | |
Form.verticalLayout.addWidget(Form.Text_Description) | |
Form.label_5 = QtGui.QLabel(Form) | |
Form.label_5.setObjectName(_fromUtf8("label_5")) | |
Form.verticalLayout.addWidget(Form.label_5) | |
Form.horizontalLayout_4 = QtGui.QHBoxLayout() | |
Form.horizontalLayout_4.setObjectName(_fromUtf8("horizontalLayout_4")) | |
Form.label_8 = QtGui.QLabel(Form) | |
Form.label_8.setObjectName(_fromUtf8("label_8")) | |
Form.horizontalLayout_4.addWidget(Form.label_8) | |
Form.Text_Tags = QtGui.QLineEdit(Form) | |
Form.Text_Tags.setText(_fromUtf8("")) | |
Form.Text_Tags.setObjectName(_fromUtf8("Text_Tags")) | |
Form.horizontalLayout_4.addWidget(Form.Text_Tags) | |
Form.verticalLayout.addLayout(Form.horizontalLayout_4) | |
Form.label_6 = QtGui.QLabel(Form) | |
Form.label_6.setObjectName(_fromUtf8("label_6")) | |
Form.verticalLayout.addWidget(Form.label_6) | |
Form.Combo_Filetype = QtGui.QComboBox(Form) | |
Form.Combo_Filetype.setObjectName(_fromUtf8("Combo_Filetype")) | |
Form.Combo_Filetype.addItem(_fromUtf8("")) | |
Form.Combo_Filetype.addItem(_fromUtf8("")) | |
Form.Combo_Filetype.addItem(_fromUtf8("")) | |
Form.Combo_Filetype.addItem(_fromUtf8("")) | |
Form.Combo_Filetype.addItem(_fromUtf8("")) | |
Form.Combo_Filetype.addItem(_fromUtf8("")) | |
Form.verticalLayout.addWidget(Form.Combo_Filetype) | |
Form.label_2 = QtGui.QLabel(Form) | |
Form.label_2.setObjectName(_fromUtf8("label_2")) | |
Form.verticalLayout.addWidget(Form.label_2) | |
Form.horizontalLayout = QtGui.QHBoxLayout() | |
Form.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) | |
Form.Text_Token = QtGui.QLineEdit(Form) | |
Form.Text_Token.setEchoMode(QtGui.QLineEdit.Password) | |
Form.Text_Token.setObjectName(_fromUtf8("Text_Token")) | |
Form.horizontalLayout.addWidget(Form.Text_Token) | |
Form.Button_Token = QtGui.QPushButton(Form) | |
Form.Button_Token.setObjectName(_fromUtf8("Button_Token")) | |
Form.horizontalLayout.addWidget(Form.Button_Token) | |
Form.verticalLayout.addLayout(Form.horizontalLayout) | |
Form.Button_Upload = QtGui.QPushButton(Form) | |
Form.Button_Upload.setObjectName(_fromUtf8("Button_Upload")) | |
Form.verticalLayout.addWidget(Form.Button_Upload) | |
Form.ProgressBar = QtGui.QProgressBar(Form) | |
Form.ProgressBar.setProperty("value", 25) | |
Form.ProgressBar.setObjectName(_fromUtf8("ProgressBar")) | |
Form.verticalLayout.addWidget(Form.ProgressBar) | |
Form.Button_View = QtGui.QPushButton(Form) | |
Form.Button_View.setObjectName(_fromUtf8("Button_View")) | |
Form.verticalLayout.addWidget(Form.Button_View) | |
QtCore.QMetaObject.connectSlotsByName(Form) | |
Form.setWindowTitle(translate("Form", "Sketchfab exporter", None)) | |
Form.label_7.setText(translate("Form", "What to upload?", None)) | |
Form.Radio_Selection.setText(translate("Form", "Selection", None)) | |
Form.Radio_Document.setText(translate("Form", "All visible objects", None)) | |
Form.label_3.setText(translate("Form", "Model name", None)) | |
Form.Check_Private.setText(translate("Form", "Private", None)) | |
Form.label_4.setText(translate("Form", "Description", None)) | |
Form.label_5.setText(translate("Form", "Tags (separated by commas)", None)) | |
Form.label_8.setText(translate("Form", "freecad,", None)) | |
Form.label_6.setText(translate("Form", "Format", None)) | |
Form.Combo_Filetype.setItemText(0, translate("Form", "OBJ + MTL (Arch exporter)", None)) | |
Form.Combo_Filetype.setItemText(1, translate("Form", "OBJ (standard Mesh exporter)", None)) | |
Form.Combo_Filetype.setItemText(2, translate("Form", "DAE (Collada)", None)) | |
Form.Combo_Filetype.setItemText(3, translate("Form", "STL", None)) | |
Form.Combo_Filetype.setItemText(4, translate("Form", "IGES (shapes only)", None)) | |
Form.Combo_Filetype.setItemText(5, translate("Form", "IV", None)) | |
Form.label_2.setText(translate("Form", "Sketchfab API token", None)) | |
Form.Button_Token.setText(translate("Form", "Obtain", None)) | |
Form.Button_Upload.setText(translate("Form", "Upload", None)) | |
Form.ProgressBar.setFormat(translate("Form", "uploading", None)) | |
Form.Button_View.setText(translate("Form", "Success! View uploaded online", None)) | |
class SketchfabTaskPanel: | |
'''The TaskPanel for Sketchfab upload''' | |
def __init__(self): | |
self.url = None | |
self.form = QtGui.QWidget() | |
ui = Ui_Form() | |
ui.setupUi(self.form) | |
self.form.ProgressBar.hide() | |
self.form.Button_View.hide() | |
QtCore.QObject.connect(self.form.Button_Token,QtCore.SIGNAL("pressed()"),self.getToken) | |
QtCore.QObject.connect(self.form.Button_Upload,QtCore.SIGNAL("pressed()"),self.upload) | |
QtCore.QObject.connect(self.form.Button_View,QtCore.SIGNAL("pressed()"),self.viewModel) | |
self.form.Text_Name.setText(FreeCAD.ActiveDocument.Label) | |
self.form.Text_Token.setText(FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Web").GetString("SketchfabToken","")) | |
def isAllowedAlterSelection(self): | |
return True | |
def isAllowedAlterView(self): | |
return True | |
def getStandardButtons(self): | |
return int(QtGui.QDialogButtonBox.Close) | |
def accept(self): | |
FreeCADGui.ActiveDocument.resetEdit() | |
return True | |
def getToken(self): | |
QtGui.QDesktopServices.openUrl(SKETCHFAB_TOKEN_URL) | |
def get_request_payload(self, token, data={}, files={}, json_payload=False): | |
"""Helper method that returns the authentication token and proper content | |
type depending on whether or not we use JSON payload.""" | |
headers = {'Authorization': 'Token {}'.format(token)} | |
if json_payload: | |
headers.update({'Content-Type': 'application/json'}) | |
data = json.dumps(data) | |
return {'data': data, 'files': files, 'headers': headers} | |
def saveFile(self): | |
import FreeCADGui | |
if self.form.Radio_Selection.isChecked(): | |
objects = FreeCADGui.Selection.getSelection() | |
else: | |
objects = [obj for obj in FreeCAD.ActiveDocument.Objects if obj.ViewObject.isVisible()] | |
if not objects: | |
QtGui.QMessageBox.critical(None,translate("Web","Nothing to upload"),translate("The selection of the document contains no object to upload")) | |
return None | |
filename = os.path.join(tempfile._get_default_tempdir(),next(tempfile._get_candidate_names())) | |
filetype = self.form.Combo_Filetype.currentIndex() | |
# 0 = obj + mtl, 1 = obj, 2 = dae, 3 = stl, 4 = IGES, 5 = iv (currently not working) | |
if filetype == 0: # OBJ + MTL | |
import importOBJ | |
importOBJ.export(objects,filename+".obj") | |
return self.packFiles(filename,[filename+".obj",filename+".mtl"]) | |
elif filetype == 1: # OBJ (mesh exporter) | |
import Mesh | |
Mesh.export(objects,filename+".obj") | |
return self.packFiles(filename,[filename+".obj"]) | |
elif filetype == 2: # DAE | |
import importDAE | |
importDAE.export(objects,filename+".dae") | |
return self.packFiles(filename,[filename+".dae"]) | |
elif filetype == 3: # STL | |
import Mesh | |
Mesh.export(objects,filename+".stl") | |
return self.packFiles(filename,[filename+".stl"]) | |
elif filetype == 4: # IGES | |
import Part | |
Part.export(objects,filename+".iges") | |
return self.packFiles(filename,[filename+".iges"]) | |
elif filetype == 5: # IV | |
import FreeCADGui | |
FreeCADGui.export(objects,filename+".iv") | |
# removing FreeCAD-specific nodes | |
f = open(filename+".iv","rb") | |
s = f.read() | |
f.close() | |
ver = FreeCAD.Version() | |
vinfo = "# Exported by FreeCAD v" + ver[0] + "." + ver[1] + " build" + ver[2] + "\n" | |
vinfo += "# http://www.freecadweb.org\n" | |
s = s.replace("#Inventor V2.1 ascii","#Inventor V2.1 ascii\n"+vinfo) | |
s = s.replace("SoBrepEdgeSet","SoIndexedLineSet") | |
s = s.replace("SoBrepFaceSet","SoIndexedFaceSet") | |
s = s.replace("\n","--endl--") | |
s = re.sub("highlightIndex .*?\]"," ",s) | |
s = re.sub("partIndex .*?\]"," ",s) | |
s = s.replace("--endl--","\n") | |
f = open(filename+".iv","wb") | |
f.write(s) | |
f.close() | |
return self.packFiles(filename,[filename+".iv"]) | |
def packFiles(self,filename,fileslist): | |
for f in fileslist: | |
if not os.path.exists(f): | |
return None | |
z = zipfile.ZipFile(filename+".zip","w") | |
for f in fileslist: | |
z.write(f) | |
z.close() | |
for f in fileslist: | |
os.remove(f) | |
s = os.path.getsize(filename+".zip") | |
if s > 1048576: | |
size = str(s >> 20)+" MB" | |
else: | |
size = str(s >> 10)+" KB" | |
return (filename+".zip",size) | |
def upload(self): | |
if not self.form.Text_Name.text(): | |
QtGui.QMessageBox.critical(None,translate("Web","Model name is empty"),translate("You must provide a name for your model")) | |
return | |
if not self.form.Text_Token.text(): | |
QtGui.QMessageBox.critical(None,translate("Web","No token provided"),translate("The token is empty. Please press the Obtain button to get your user API token from Sketchfab, then copy / paste the API token to the field below")) | |
return | |
FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Web").SetString("SketchfabToken",self.form.Text_Token.text()) | |
pack = self.saveFile() | |
if not pack: | |
QtGui.QMessageBox.critical(None,translate("Web","File packing error"),translate("Unable to save and zip a file for upload")) | |
return | |
if os.path.getsize(pack[0]) >= 52428800: | |
b = QtGui.QMessageBox() | |
b.setText(translate("Web","Big upload")) | |
b.setInformativeText(translate("Web","The file to be uploaded is %s, which is above the maximum 50Mb allowed by free Sketchfab accounts. Pro accounts allow for up to 200Mb. Continue?") % pack[1]) | |
b.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel) | |
b.setDefaultButton(QtGui.QMessageBox.Cancel) | |
ret = b.exec_() | |
if ret != QtGui.QMessageBox.Ok: | |
return | |
data = { | |
"name": self.form.Text_Name.text(), | |
"description": self.form.Text_Description.text(), | |
"tags": ["freecad"]+[t.strip() for t in self.form.Text_Tags.text().split(",")], | |
"private": self.form.Check_Private.isChecked(), | |
"source":"freecad", | |
} | |
files = { | |
"modelFile": open(pack[0], 'rb') | |
} | |
self.form.Button_Upload.hide() | |
# for now this is a fake progress bar, it won't move, just to show the user that the upload is in progress | |
self.form.ProgressBar.setFormat(translate("Web","Uploading")+" "+pack[1]+"...") | |
self.form.ProgressBar.show() | |
QtGui.qApp.processEvents() | |
try: | |
r = requests.post(SKETCHFAB_UPLOAD_URL, **self.get_request_payload(self.form.Text_Token.text(), data, files=files)) | |
except requests.exceptions.RequestException as e: | |
QtGui.QMessageBox.critical(None,translate("Web","Upload error"),translate("Web","Upload failed:")+" "+str(e)) | |
self.form.ProgressBar.hide() | |
self.form.Button_Upload.show() | |
return | |
if r.status_code != requests.codes.created: | |
QtGui.QMessageBox.critical(None,translate("Web","Upload error"),translate("Web","Upload failed:")+" "+str(r.json())) | |
self.form.ProgressBar.hide() | |
self.form.Button_Upload.show() | |
return | |
self.url = r.headers['Location'] | |
if self.form.Combo_Filetype.currentIndex() in [0,1,5]: # OBJ format, sketchfab expects inverted Y/Z axes | |
self.form.ProgressBar.setFormat(translate("Web","Awaiting confirmation...")) | |
self.form.ProgressBar.setValue(75) | |
if self.poll(self.url): | |
self.form.ProgressBar.setFormat(translate("Web","Fixing model...")) | |
QtGui.qApp.processEvents() | |
self.patch(self.url) | |
else: | |
QtGui.QMessageBox.warning(None,translate("Web","Patch error"),translate("Web","Patching failed. The model was successfully uploaded, but might still require manual adjustments:")) | |
self.form.ProgressBar.hide() | |
self.form.Button_View.show() | |
def poll(self,url): | |
"""GET the model endpoint to check the processing status.""" | |
max_errors = 10 | |
errors = 0 | |
retry = 0 | |
max_retries = 50 | |
retry_timeout = 5 # seconds | |
while (retry < max_retries) and (errors < max_errors): | |
try: | |
r = requests.get(url, **self.get_request_payload(self.form.Text_Token.text())) | |
except requests.exceptions.RequestException as e: | |
print ('Sketchfab: Polling failed with error: ',str(e)) | |
errors += 1 | |
retry += 1 | |
continue | |
result = r.json() | |
if "error" in result: | |
e = result["error"] | |
else: | |
e = result | |
if r.status_code != requests.codes.ok: | |
print ('Sketchfab: Polling failed with error: ',str(e)) | |
errors += 1 | |
retry += 1 | |
continue | |
processing_status = result['status']['processing'] | |
if processing_status == 'PENDING': | |
retry += 1 | |
time.sleep(retry_timeout) | |
continue | |
elif processing_status == 'PROCESSING': | |
retry += 1 | |
time.sleep(retry_timeout) | |
continue | |
elif processing_status == 'FAILED': | |
print ('Sketchfab: Polling failed: ',str(e)) | |
return False | |
elif processing_status == 'SUCCEEDED': | |
return True | |
retry += 1 | |
print ('Sketchfab: Stopped polling after too many retries or too many errors') | |
return False | |
def patch(self,url): | |
"applies different fixes to the uploaded model" | |
options_url = os.path.join(url, 'options') | |
data = { | |
'orientation': '{"axis": [1, 0, 0], "angle": 270}' | |
} | |
try: | |
r = requests.patch(options_url, **self.get_request_payload(self.form.Text_Token.text(), data, json_payload=True)) | |
except requests.exceptions.RequestException as e: | |
QtGui.QMessageBox.warning(None,translate("Web","Patch error"),translate("Web","Patching failed. The model was successfully uploaded, but might still require manual adjustments:")+" "+str(e)) | |
else: | |
if r.status_code != 204: | |
QtGui.QMessageBox.warning(None,translate("Web","Patch error"),translate("Web","Patching failed. The model was successfully uploaded, but might still require manual adjustments:")+" "+str(r.content)) | |
def viewModel(self): | |
if self.url: | |
url = self.url.replace("api","www") | |
url = url.replace("/v3","") | |
QtGui.QDesktopServices.openUrl(url) | |
FreeCADGui.Control.showDialog(SketchfabTaskPanel()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment