FreeCAD/FreeCAD

CLI: User can't generate an image without using GUI

Aberta

#13.356 aberto em 7 de abr. de 2024

 (1 comentário) (0 reação) (0 responsável)C++ (5.550 forks)batch import
Help wantedMod: CoreTopic: CLIType: Feature

Métricas do repositório

Stars
 (30.913 estrelas)
Métricas de merge de PR
 (Mesclagem média 24d 7h) (217 fundiu PRs em 30d)

Description

Is there an existing issue for this?

  • I have searched the existing issues

Problem description

This issue has been raised and addressed many times on the forum. Existing solutions don't seem to work.

https://forum.freecad.org/viewtopic.php?style=5&t=33389 https://forum.freecad.org/viewtopic.php?t=14691 https://forum.freecad.org/viewtopic.php?style=10&t=78314 https://forum.freecad.org/viewtopic.php?t=32500 https://forum.freecad.org/viewtopic.php?style=1&t=5082

Here's an example script

    def _make_picture_headless(self, obj, image_name):

        # Ensure the 'Part' and 'coin' modules are available, along with necessary attributes/methods.
        if not hasattr(obj, "Shape") or not hasattr(obj.Shape, "writeInventor"):
            print("Object does not have the required attributes.")
            return False

        try:
            # Generate Inventor data from the object's shape
            iv = obj.Shape.writeInventor()

            # Prepare Inventor data for rendering
            inp = coin.SoInput()
            inp.setBuffer(iv)
            data = coin.SoDB.readAll(inp)

            if data is None:
                print("Failed to read Inventor data.")
                return False

            # Setup the scene
            base = coin.SoBaseColor()
            base.rgb.setValue(0.6, 0.7, 1.0)
            data.insertChild(base, 0)

            root = coin.SoSeparator()
            light = coin.SoDirectionalLight()
            cam = coin.SoOrthographicCamera()
            root.addChild(cam)
            root.addChild(light)
            root.addChild(data)

            # Camera and rendering setup
            axo = coin.SbRotation(-0.353553, -0.146447, -0.353553, -0.853553)
            viewport = coin.SbViewportRegion(400, 400)
            cam.orientation.setValue(axo)
            cam.viewAll(root, viewport)
            off = coin.SoOffscreenRenderer(viewport)
            root.ref()
            ret = off.render(root)
            root.unref()

            if ret <= 0:
                print("Failed to render the scene.")
                # return False
                # This always seems to return 0

            # Saving the rendered image
            file_path = os.path.join(self.filelocation, f"{image_name}.png")
            if off.isWriteSupported("PNG"):
                off.writeToFile(file_path, "PNG")
            else:
                print("PNG format is not supported.")
                # return False
            # No file format seems to be supported except maybe .ps

            # Optionally save as PostScript if supported
            ps_file_path = os.path.join(self.filelocation, f"{image_name}.ps")
            off.writeToPostScript(ps_file_path)

            return True
        except Exception as e:
            print(f"An error occurred: {e}")
            return False

This script segfaults

Program received signal SIGSEGV, Segmentation fault. %)		
#0  /lib/x86_64-linux-gnu/libc.so.6(+0x42520) [0x7aeebe642520]
#1  0x7aeeb10d30c8 in SoOffscreenRenderer::writeToPostScript(_IO_FILE*, SbVec2f const&) const from /lib/x86_64-linux-gnu/libCoin.so.80c+0x5f8
#2  0x7aeeb10d32ac in SoOffscreenRenderer::writeToPostScript(_IO_FILE*) const from /lib/x86_64-linux-gnu/libCoin.so.80c+0x2c
#3  0x7aeeb10d3301 in SoOffscreenRenderer::writeToPostScript(char const*) const from /lib/x86_64-linux-gnu/libCoin.so.80c+0x31
#4  /usr/lib/python3/dist-packages/pivy/_coin.cpython-310-x86_64-linux-gnu.so(+0x4b02a9) [0x7aeeb1ab02a9]
#5  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x128008) [0x7aeebef28008]
#6  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyObject_Call+0x5c) [0x7aeebeee1fec]
#7  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x4b16) [0x7aeebee76776]
#8  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x1c23af) [0x7aeebefc23af]
#9  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x829e) [0x7aeebee79efe]
#10  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x1c23af) [0x7aeebefc23af]
#11  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x829e) [0x7aeebee79efe]
#12  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x1c23af) [0x7aeebefc23af]
#13  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0xe23d8) [0x7aeebeee23d8]
#14  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x69de) [0x7aeebee7863e]
#15  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x1c23af) [0x7aeebefc23af]
#16  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x829e) [0x7aeebee79efe]
#17  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x1c23af) [0x7aeebefc23af]
#18  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0xe2358) [0x7aeebeee2358]
#19  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x4b16) [0x7aeebee76776]
#20  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x1c23af) [0x7aeebefc23af]
#21  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyObject_FastCallDictTstate+0x57) [0x7aeebeee1bf7]
#22  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyObject_Call_Prepend+0xe0) [0x7aeebeee1e10]
#23  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x14eaa9) [0x7aeebef4eaa9]
#24  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyObject_MakeTpCall+0x8c) [0x7aeebeedfadc]
#25  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0xa62a) [0x7aeebee7c28a]
#26  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x1c23af) [0x7aeebefc23af]
#27  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0xe2358) [0x7aeebeee2358]
#28  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x4b16) [0x7aeebee76776]
#29  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x1c23af) [0x7aeebefc23af]
#30  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyObject_FastCallDictTstate+0x57) [0x7aeebeee1bf7]
#31  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyObject_Call_Prepend+0xe0) [0x7aeebeee1e10]
#32  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x14eaa9) [0x7aeebef4eaa9]
#33  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyObject_MakeTpCall+0x8c) [0x7aeebeedfadc]
#34  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0xa62a) [0x7aeebee7c28a]
#35  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x1c23af) [0x7aeebefc23af]
#36  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x829e) [0x7aeebee79efe]
#37  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x1c23af) [0x7aeebefc23af]
#38  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(_PyEval_EvalFrameDefault+0x9d68) [0x7aeebee7b9c8]
#39  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x1c23af) [0x7aeebefc23af]
#40  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(PyEval_EvalCode+0xbe) [0x7aeebefbd3de]
#41  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(+0x20c4cd) [0x7aeebf00c4cd]
#42  /lib/x86_64-linux-gnu/libpython3.10.so.1.0(PyRun_StringFlags+0x79) [0x7aeebf00d4f9]
#43  0x7aeebf741386 in Base::InterpreterSingleton::runString[abi:cxx11](char const*) from /home/bradcollette/FCD/FC/lib/libFreeCADBase.so+0x106
#44  0x7aeec09355e3 in App::Application::runApplication() from /home/bradcollette/FCD/FC/lib/libFreeCADApp.so+0x1af
#45  ./bin/FreeCADCmd(+0x2b06e) [0x59574f83706e]
#46  /lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x7aeebe629d90]
#47  /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80) [0x7aeebe629e40]
#48  ./bin/FreeCADCmd(+0x2ac45) [0x59574f836c45]

@wmayer or anyone, Is there a working solution?

Full version info

OS: Pop!_OS 22.04 LTS (pop:GNOME/pop)
Word size of FreeCAD: 64-bit
Version: 0.22.0dev.36784 +3 (Git)
Build type: Unknown
Branch: translation
Hash: 94ec77a2e57b472788c24aa9e7e50b7f8348d214
Python 3.10.12, Qt 5.15.3, Coin 4.0.0, Vtk 7.1.1, OCC 7.5.1
Locale: English/United States (en_US)

Subproject(s) affected?

None

Anything else?

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Guia do colaborador