How can I export a GLB / GLTF file format with PyDPF?

Options
Ayush Kumar
Ayush Kumar Member, Moderator, Employee Posts: 408
First Anniversary Ansys Employee Solution Developer Community of Practice Member First Answer
edited September 2023 in General Language Questions

How can I export a GLB / GLTF file format with PyDPF?

Tagged:

Comments

  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 408
    First Anniversary Ansys Employee Solution Developer Community of Practice Member First Answer
    edited November 2023
    Options

    You will need to install any Python lib. to convert GLTF to GLB format because PyVista only exports GLTF. An example would be pip installing pygltflib

    import os
    from ansys.dpf import core as dpf
    
    from ansys.dpf.core.plotter import DpfPlotter
    from pygltflib.utils import gltf2glb
    
    out_path = r"\FOLDER\PATH\TO\RESULT\FILE"
    rst_path = os.path.join(out_path, "file.rst")
    
    model = dpf.Model(rst_path)
    mesh = model.metadata.meshed_region
    
    print(mesh)
    
    displacement = model.results.displacement()
    
    plotter = DpfPlotter()
    plotter.add_field(displacement.outputs.fields_container()[0])
    
    gltf_path = os.path.join(out_path, r"test_export.gltf")
    gltf = plotter._internal_plotter._plotter.export_gltf(gltf_path)
    
    glb_path = os.path.join(out_path, r"test_export.glb")
    """Convert GLTF to GLB File"""
    gltf2glb(gltf_path, glb_path)
    
    """
    Export just legend as an image with white Background
    Can be used as a background in a 3D viewer where the GLB is imported.
    """
    plotter = DpfPlotter()
    plotter.add_field(disp_f, opacity=0.0)
    path_to_legend = os.path.join(out_path, "legend.png")
    plotter.show_figure(screenshot=path_to_legend)
    
  • kanchan_cadfem
    kanchan_cadfem Member Posts: 17
    First Anniversary First Comment 5 Likes First Answer
    Options

    There is a custom operator created as an example for this

    https://dpf.docs.pyansys.com/version/stable/examples/08-python-operators/02-python_operators_with_dependencies.html

    once loaded the usage is straightforward

    new_operator = dpf.Operator("gltf_export")
    import os
    
    model = dpf.Model(dpf.upload_file_in_tmp_folder(examples.find_static_rst()))
    
    mesh = model.metadata.meshed_region
    skin_mesh = dpf.operators.mesh.tri_mesh_skin(mesh=mesh)
    
    displacement = model.results.displacement()
    displacement.inputs.mesh_scoping(skin_mesh)
    displacement.inputs.mesh(skin_mesh)
    new_operator.inputs.path(os.path.join(tmp, "out"))
    new_operator.inputs.mesh(skin_mesh)
    new_operator.inputs.field(displacement.outputs.fields_container()[0])
    new_operator.run()
    
    print("operator ran successfully")
    
    dpf.download_file(os.path.join(tmp, "out.glb"), os.path.join(os.getcwd(), "out.glb"))
    
    
  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 408
    First Anniversary Ansys Employee Solution Developer Community of Practice Member First Answer
    Options

    Just wanted to point out that the DPF's gltf_export also uses pygltflib behind the scenes.