@@ -243,14 +243,24 @@ def add_body(self, body: Body, merge: bool = False, **plotting_options: dict | N
243243
244244 if self .use_service_colors :
245245 faces = body .faces
246- dataset = body .tessellate ()
247- for i , block in enumerate (dataset ):
248- if faces [i ].color != Color .DEFAULT .value :
249- plotting_options ["color" ] = faces [i ].color
250- else :
251- plotting_options ["color" ] = body .color
252- self ._backend .pv_interface .plot (block , ** plotting_options )
253- return
246+ dataset = body .tessellate (merge = merge )
247+ body_color = body .color
248+ if not merge :
249+ # ASSUMPTION: the faces returned by the service are in the same order
250+ # as the tessellation information returned by the service...
251+ elems = [elem for elem in dataset ]
252+ for face , block in zip (faces , elems ):
253+ face_color = face .color
254+ if face_color != Color .DEFAULT .value :
255+ plotting_options ["color" ] = face_color
256+ else :
257+ plotting_options ["color" ] = body_color
258+ self ._backend .pv_interface .plot (block , ** plotting_options )
259+ return
260+ else :
261+ plotting_options ["color" ] = body_color
262+ self ._backend .pv_interface .plot (dataset , ** plotting_options )
263+ return
254264 # WORKAROUND: multi_colors is not properly supported in PyVista PolyData
255265 # so if multi_colors is True and merge is True (returns PolyData) then
256266 # we need to set the color manually
@@ -481,27 +491,55 @@ def show(
481491 return lib_objects
482492
483493 def export_glb (
484- self , plotting_object : Any = None , screenshot : str | None = None , ** plotting_options
494+ self , plotting_object : Any = None , filename : str | Path | None = None , ** plotting_options
485495 ) -> None :
486496 """Export the design to a glb file. Does not support picked objects.
487497
488498 Parameters
489499 ----------
490500 plotting_object : Any, default: None
491501 Object you can add to the plotter.
492- screenshot : str, default: None
493- Path to save a screenshot of the plotter. Do not include file extension.
502+ filename : str | ~pathlib.Path, default: None
503+ Path to save a GLB file of the plotter. If None, the file will be saved as
504+ temp_glb.glb.
494505 **plotting_options : dict, default: None
495506 Keyword arguments for the plotter. Arguments depend of the backend implementation
496507 you are using.
508+
509+ Returns
510+ -------
511+ ~pathlib.Path
512+ Path to the exported glb file.
497513 """
498514 if plotting_object is not None :
499515 self .plot (plotting_object , ** plotting_options )
500516
501- gltf_filepath = str (screenshot ) + ".gltf"
517+ # Depending on whether a name is provided, the file will be saved with the name
518+ # provided or with a default name (temp_glb). If a name is provided, the file will
519+ # be saved in the current working directory. If a path is provided, the file will be
520+ # saved in the provided path. We will also make sure that the file has the .glb extension,
521+ # and if not, we will add it.
522+ if filename is None :
523+ glb_filepath = Path ("temp_glb.glb" )
524+ else :
525+ glb_filepath = filename if isinstance (filename , Path ) else Path (filename )
526+ if glb_filepath .suffix != ".glb" :
527+ glb_filepath = glb_filepath .with_suffix (".glb" )
502528
529+ # Temporary gltf file to export the scene
530+ gltf_filepath = glb_filepath .with_suffix (".gltf" )
531+
532+ # Hide the axes before exporting the gltf and then export it
503533 self .backend ._pl ._scene .hide_axes ()
504534 self .backend ._pl ._scene .export_gltf (gltf_filepath )
505535
536+ # Convert the gltf to glb and remove the gltf file
506537 gltf2glb (gltf_filepath )
507538 Path .unlink (gltf_filepath )
539+
540+ # Check if the file was exported correctly
541+ if not glb_filepath .exists (): # pragma: no cover
542+ raise FileNotFoundError (f"GLB file not found at { glb_filepath } . Export failed." )
543+
544+ # Finally, return the path to the exported glb file
545+ return glb_filepath
0 commit comments