Skip to content

New function "supportsGLB" - #4842

Open
MartinOtter wants to merge 4 commits into
masterfrom
MartinOtter-patch-supportGLB
Open

MartinOtter wants to merge 4 commits into
masterfrom
MartinOtter-patch-supportGLB

Conversation

@MartinOtter

Copy link
Copy Markdown
Member

The function returns true, if the tool can visualize meshes in GLB format with model Shape. The default implementation is to return false (so adding this function is backwards compatible).

Background:
The only mesh format supported by all Modelica-tools seems to be the STL format - which looks bad and is extremely limited. In order to improve Modelica packages that utilize 3D-meshes in a Modelica compliant way, it is proposed that such libraries (such as the coming Model3D library - the planned successor of MSL multibody) provide meshes in STL and in GLB format and when using them in examples decide with this function, which extension to use for the file name given to model Shape.

There are many mesh formats. Here it is explained why GLB should be used (especially for an open source activity). Copied from this link:

GLB is the binary version of glTF (GL Transmission Format), an open standard created and maintained by the Khronos Group, the same industry consortium behind OpenGL, Vulkan, and WebGL. glTF was designed from the ground up with one specific goal in mind: to be the “JPEG of 3D,” a format that transmits and loads 3D scenes and models as efficiently as possible, without any conversion or fuss.

The many software supporting GLB includes Blender and threejs

The function returns true, if the tool can visualize meshes in GLB format with model Shape
@MartinOtter
MartinOtter requested review from a team and HansOlsson and removed request for HansOlsson October 26, 2026 11:39

@sjoelund sjoelund left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the use-case would be to support both formats, could we have 1 example in Modelica/ModelicaTest that uses the GLB format if it's anyway supposed to be supported? So tool vendors could verify that this is working as expected.

I am also wondering if this function should perhaps be more flexible. Like:

function supportedMeshFormats
  output String result[:] = {"stl", "glb"};
end supportedMeshFormats;

@HansOlsson

Copy link
Copy Markdown
Contributor

Currently MSL has dxf-files for the robot, and Modelica.Mechanics.MultiBody.Visualizers.FixedShape says:

The supported file formats are tool dependent. Most tools support at least DXF-files but may support other format as well (such as stl, obj, 3ds). Since visualization files contain color and other data, the corresponding information in the model is usually ignored. For information about DXF files, see Wikipedia. As a default it is assumed that the DXF coordinates are in the "frame_a"-system and in meters, and that the 3dfaces are two-sided. Some tools support only 3dface (for geometry) and layer (for advanced coloring).

I do know that Dymola support dxf, stl, and wrl (for wrml); and similarly for related apps on 3D Experience Platform (in addition to the couplings to actual CAD-models).

I don't know if any tool supports 3ds (which was used by Autodesk the last millennium, not related to www.3ds.com).

I'll check internally about this format.

@HansOlsson

Copy link
Copy Markdown
Contributor

Since the use-case would be to support both formats, could we have 1 example in Modelica/ModelicaTest that uses the GLB format if it's anyway supposed to be supported? So tool vendors could verify that this is working as expected.

The minimum would be to convert existing assets. But we should also check with tool vendors.

I am also wondering if this function should perhaps be more flexible. Like:

function supportedMeshFormats
  output String result[:] = {"stl", "glb"};
end supportedMeshFormats;

Alternatively:

function supportedMeshFormats
 input String format annotation(choices(choice="stl",choice="dxf",choice="glb"));
 output Boolean supported;
end supportedMeshFormats;

@MartinOtter

Copy link
Copy Markdown
Member Author

Since the use-case would be to support both formats, could we have 1 example in Modelica/ModelicaTest that uses the GLB format if it's anyway supposed to be supported? So tool vendors could verify that this is working as expected.

I made pull request #4844 that adds such a test. The default is to show the shape in STL format (should work in any tool). After a function is added that allows to inquire whether GLB format is supported, the GLB format of the mesh is shown.

Note, this pull request could be just accepted already now and then later adapted once there is agreement on the inquiry function.

@MartinOtter

Copy link
Copy Markdown
Member Author

I am also wondering if this function should perhaps be more flexible. Like:
...
Alternatively:

function supportedMeshFormats
 input String format annotation(choices(choice="stl",choice="dxf",choice="glb"));
 output Boolean supported;
end supportedMeshFormats;

Yes, this is good, maybe with slightly different name and providing a default implementation that should work in every tool:

function supportsMeshFormat "Return true, if the provided mesh format is supported"
  input String format = "stl"  annotation(choices(choice="stl", choice="glb"));
  output Boolean result = format == "stl";
end supportsMeshFormat;

@sjoelund

Copy link
Copy Markdown
Member

Yes, this is good, maybe with slightly different name and providing a default implementation that should work in every tool

= false would be an even safer default. But yes, something like that would be good. And then there needs to be no discussion on GLB or actual formats. Easy to approve and tool vendors would provide the list.

@HansOlsson

Copy link
Copy Markdown
Contributor

To me this is still confusing: the current documentation says that dxf is the format that is most likely to be supported, and the r3robot only has dxf-files.

For some reason "dxf" was removed from the choices, and "stl" made the default support one.

I don't know what other tools support, but I think we need to check that first.

@MartinOtter

Copy link
Copy Markdown
Member Author

To me this is still confusing: the current documentation says that dxf is the format that is most likely to be supported, and the r3robot only has dxf-files.

For some reason "dxf" was removed from the choices, and "stl" made the default support one.

I don't know what other tools support, but I think we need to check that first.

Maybe better to state this in a comment and keep the function clean (tool-independent). Next proposal:

function supportsMeshFormat "Return true, if the provided mesh format is supported"
  input String format  "Extension of the mesh file name that shall be used, e.g., \"stl\", \"dxf\", \"glb\"";
  output Boolean result = false "Return true, if the format is supported by the tool";  
end supportsMeshFormat;

A user would typically want to use it in the following way:

Visualizers.FixedShape mesh(shapeType="MyPackage/Resources/meshes/mymesh."+(if supportsMeshFormat("glb") then "glb" else "stl").

This is quite tedious. A user could provide a helper function to move this in a function:

function selectMeshFile
   input String fileName;
   input String ext="glb";
   output String fileName + "." + (if supportsMeshFormat(ext) then ext else "stl").
end selectMeshFile;

Visualizers.FixedShape mesh(shapeType=selectMeshFile("MyPackage/Resources/meshes/mymesh"));

If you agree, I will update the pull request

@sjoelund

Copy link
Copy Markdown
Member

Yes, using a support function (outside of ModelicaServices) would be the way I envisioned it.

@HansOlsson

Copy link
Copy Markdown
Contributor

If you agree, I will update the pull request

I would assume a more generic solution like (or possibly without defaults)

function selectMeshFile
   input String fileName;
    input String extensions[:]={"glb","stl"};
    output String resName;
algorithm
  for ext in extensions loop
      if supportsMeshFormat(ext) then
           resName:=fileName+"."+ext;
           return;
      end if;
    end for;
   assert(false, "No supported format");
end selectMeshFile;

Visualizers.FixedShape mesh(shapeType=selectMeshFile("MyPackage/Resources/meshes/mymesh"));

@MartinOtter

Copy link
Copy Markdown
Member Author

If you agree, I will update the pull request

I would assume a more generic solution like (or possibly without defaults)

function selectMeshFile
   input String fileName;
    input String extensions[:]={"glb","stl"};
    output String resName;
algorithm
  for ext in extensions loop
      if supportsMeshFormat(ext) then
           resName:=fileName+"."+ext;
           return;
      end if;
    end for;
   assert(false, "No supported format");
end selectMeshFile;

Visualizers.FixedShape mesh(shapeType=selectMeshFile("MyPackage/Resources/meshes/mymesh"));

Yes, this is a better solution - but outside of ModelicaServices - This could be included in Multibody. I will update this pull request (to add supportsMeshFormat in ModelicaServices and selectMeshFile in Multibody and use selectMeshFile in the pull request for a test model (probably, I move the test model into this pull request here))

…lica.Mechanics.MultiBody.Visualizers.Advanced/selectMeshFile and the test model ModelicaTest.MultiBody.Visualizers.Mesh with two mesh files Resources/Data/Meshes/GlassHurricane_yUP.glb and .stl
@MartinOtter

Copy link
Copy Markdown
Member Author

Added
"""
// In ModelicaServices:
function supportsMeshFormat "Return true, if the provided mesh format is supported"
input String format "Extension of the mesh file name that shall be used, e.g., "stl", "dxf", "glb"";
output Boolean result = false "Return true, if the format is supported by the tool";
end supportsMeshFormat;

// In Modelica.Mechanics.MultiBody.Visualizers.Advanced.selectMeshFile
function selectMeshFile
"Return the file name of a mesh with an extension that is supported by the tool"
input String fileNameBase "Base part of a mesh file name (e.g. modelica://ModelicaTest/Resources/Data/Meshes/GlassHurricane_yUP)";
input String extensions[:]={"glb","stl"} "Formats provided for the mesh";
output String fileName "File name of the mesh, using the first extension supported by the tool";
algorithm
for ext in extensions loop
if ModelicaServices.Animation.supportsMeshFormat(ext) then
fileName := fileNameBase + "." + ext;
return;
end if;
end for;
Modelica.Utilities.Streams.print("... Mesh "" + fileNameBase + "" is not visualized because\nthe tool does not support one of the provided mesh formats");
end selectMeshFile;

// A test model with two meshes as ModelicaTest.MultiBody.Visualizers.Meshes
"""

return;
end if;
end for;
Modelica.Utilities.Streams.print("... Mesh \"" + fileNameBase + "\" is not visualized because\nthe tool does not support one of the provided mesh formats");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning: Pure function 'selectMeshFile' contains a call to impure function 'Modelica.Utilities.Streams.print'.
Error: Output parameter fileName was not assigned a value

I guess an assertion would be better. The fileName being unassigned is not optional if the function is supposed to work when visualization is disabled. But in those cases, you would not expect a warning, right?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants