Export Part as gltf

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
AlfVII
Posts: 3
Joined: Fri Nov 18, 2022 12:31 am

Export Part as gltf

Post by AlfVII »

Hi!

I am trying to export one 3D object as .gltf using FreeCAD 0.20.
If I do it with the GUI it is saved fine, but how canI do it with a Python script?

Thanks!
chrisb
Veteran
Posts: 53920
Joined: Tue Mar 17, 2015 9:14 am

Re: Export Part as gltf

Post by chrisb »

Hi and welcome to the forum!

Did you try the code from the Python console?
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
AlfVII
Posts: 3
Joined: Fri Nov 18, 2022 12:31 am

Re: Export Part as gltf

Post by AlfVII »

I recorded a Macro while I was doing it manually with the GUI, the resulting code used ImportGUI, which is not available in my Python code.

Maybe I should also clarify that the script I am running is executed in Ubuntu Console with FreeCAD imported as a library; not in FreeCAD Console.

In the past, for exporting in STEP format, I replaced ImportGUI.export by Import.export in my code and worked, but if I run

Code: Select all

Import.export(__my_objs__, "file.gltf") 
it does not produce any file
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Export Part as gltf

Post by onekk »

AlfVII wrote: Fri Nov 18, 2022 10:31 am ...
Hello

Speaking about code, probably posting a complete example would be better, as copying and pasting a single line of code is not telling as example what __myobjs__ is, and this could be crucial to solve you problem.

And even the full FreeCAD version info as suggested in: https://forum.freecadweb.org/viewtopic.php?f=3&t=2264

As example I export a part as a STEP file with:

Code: Select all

f_name = "proper_file_name_with_extension"
obj = rear_wheel()  # Here the object is created as a TopoShape
obj_do = Part.show(obj, "rear_wheel")  # And then converted as a "DocumentObject" to be passed to the export Part
Part.export([obj_do], f_name)
Not that as example STP file exporter want that the solid is passed as a list of "DocumentObject" if not it will export a void file, in my case I'm exporting a "single element" list and the file is created in a correct way.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
AlfVII
Posts: 3
Joined: Fri Nov 18, 2022 12:31 am

Re: Export Part as gltf

Post by AlfVII »

Thanks for the example!

I am using the following daily version:

Code: Select all

OS: Ubuntu 20.04.2 LTS
Word size of FreeCAD: 64-bit
Version: 0.21.
Build type: Release
Branch: unknown
Hash: 45d7151dfa89e974e20fbcfae096b65ed406da23
Python 3.8.10, Qt 5.12.8, Coin 4.0.0, Vtk 7.1.1, OCC 7.5.2
Locale: C/Default (C)
And I created a little example reporducing my problem, just paste in a python file and run it with the python interpreter:

Code: Select all

import sys
sys.path.append("/usr/lib/freecad-daily/lib")
import FreeCAD  # noqa: E402
import Import  # noqa: E402


FreeCAD.newDocument("ea")
document = FreeCAD.getDocument("ea")

cube = document.addObject("Part::Box", "cube")
cube.Length = 1
cube.Width = 1
cube.Height = 1
document.recompute()
Import.export([cube], "ea.step")
Import.export([cube], "ea.gltf")
That code should produce a STEP and an GLTF file, but only ea.step is produced, while no error is thrown.

Bets regards,
Alfonso
meelad
Posts: 1
Joined: Tue Dec 06, 2022 1:37 pm

Re: Export Part as gltf

Post by meelad »

AlfVII wrote: Mon Nov 21, 2022 5:16 pm Thanks for the example!

I am using the following daily version:

Code: Select all

OS: Ubuntu 20.04.2 LTS
Word size of FreeCAD: 64-bit
Version: 0.21.
Build type: Release
Branch: unknown
Hash: 45d7151dfa89e974e20fbcfae096b65ed406da23
Python 3.8.10, Qt 5.12.8, Coin 4.0.0, Vtk 7.1.1, OCC 7.5.2
Locale: C/Default (C)
And I created a little example reporducing my problem, just paste in a python file and run it with the python interpreter:

Code: Select all

import sys
sys.path.append("/usr/lib/freecad-daily/lib")
import FreeCAD  # noqa: E402
import Import  # noqa: E402


FreeCAD.newDocument("ea")
document = FreeCAD.getDocument("ea")

cube = document.addObject("Part::Box", "cube")
cube.Length = 1
cube.Width = 1
cube.Height = 1
document.recompute()
Import.export([cube], "ea.step")
Import.export([cube], "ea.gltf")
That code should produce a STEP and an GLTF file, but only ea.step is produced, while no error is thrown.

Bets regards,
Alfonso
Hi,
Same problem here with Import.export .
ImportGui exports the GLTF file, but it requires to import FreeCADGui first.

Even after changing this line:
https://github.com/FreeCAD/FreeCAD/blob ... Gui.py#L36
to:

Code: Select all

FreeCAD.addExportType("glTF (*.gltf *.glb)","Import")
It does not work.

But it think i found the problem:

https://github.com/FreeCAD/FreeCAD/blob ... y.cpp#L383
https://github.com/FreeCAD/FreeCAD/blob ... y.cpp#L721

AppImportGuiPy.cpp has imported and used RWGltf_CafWriter but AppImportPy.cpp doesn't do that.

I can create a pull request but I'm not 100% sure that it will fix the problem.
Thank you FreeCAD community!
Post Reply