ansys-pyensight-core 0.8.11__py3-none-any.whl → 0.8.13__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ansys-pyensight-core might be problematic. Click here for more details.

@@ -1,279 +1,631 @@
1
- import json
2
- import logging
3
- import os
4
- import sys
5
- import time
6
- from typing import Any, Dict, List, Optional
7
-
8
- from ansys.api.pyensight.v0 import dynamic_scene_graph_pb2
9
- import pygltflib
10
-
11
- sys.path.insert(0, os.path.dirname(__file__))
12
- from dsg_server import Part, UpdateHandler # noqa: E402
13
-
14
-
15
- class GLBSession(object):
16
- def __init__(
17
- self,
18
- verbose: int = 0,
19
- normalize_geometry: bool = False,
20
- time_scale: float = 1.0,
21
- handler: UpdateHandler = UpdateHandler(),
22
- ):
23
- """
24
- Provide an interface to read a GLB file and link it to an UpdateHandler instance
25
-
26
- This class reads GLB files and provides the data to an UpdateHandler instance for
27
- further processing.
28
-
29
- Parameters
30
- ----------
31
- verbose : int
32
- The verbosity level. If set to 1 or higher the class will call logging.info
33
- for log output. The default is ``0``.
34
- normalize_geometry : bool
35
- If True, the scene coordinates will be remapped into the volume [-1,-1,-1] - [1,1,1]
36
- The default is not to remap coordinates.
37
- time_scale : float
38
- Scale time values by this factor after being read. The default is ``1.0``.
39
- handler : UpdateHandler
40
- This is an UpdateHandler subclass that is called back when the state of
41
- a scene transfer changes. For example, methods are called when the
42
- transfer begins or ends and when a Part (mesh block) is ready for processing.
43
- """
44
- super().__init__()
45
- self._callback_handler = handler
46
- self._verbose = verbose
47
- self._normalize_geometry = normalize_geometry
48
- self._time_scale = time_scale
49
- self._time_limits = [
50
- sys.float_info.max,
51
- -sys.float_info.max,
52
- ] # Min/max across all time steps
53
- self._mesh_block_count = 0
54
- self._node_idx: int = -1
55
- self._variables: Dict[int, Any] = dict()
56
- self._groups: Dict[int, Any] = dict()
57
- self._part: Part = Part(self)
58
- self._scene_bounds: Optional[List] = None
59
- self._cur_timeline: List = [0.0, 0.0] # Start/End time for current update
60
- self._callback_handler.session = self
61
- # log any status changes to this file. external apps will be monitoring
62
- self._status_file = os.environ.get("ANSYS_OV_SERVER_STATUS_FILENAME", "")
63
- self._status = dict(status="idle", start_time=0.0, processed_buffers=0, total_buffers=0)
64
- self._gltf: pygltflib.GLTF2 = pygltflib.GLTF2()
65
- self._id_num: int = 0
66
-
67
- def _reset(self):
68
- self._variables = dict()
69
- self._groups = dict()
70
- self._part = Part(self)
71
- self._scene_bounds = None
72
- self._cur_timeline = [0.0, 0.0] # Start/End time for current update
73
- self._status = dict(status="idle", start_time=0.0, processed_buffers=0, total_buffers=0)
74
- self._gltf = pygltflib.GLTF2()
75
- self._node_idx = -1
76
- self._mesh_block_count = 0
77
- self._id_num = 0
78
-
79
- def _next_id(self) -> int:
80
- self._id_num += 1
81
- return self._id_num
82
-
83
- @property
84
- def scene_bounds(self) -> Optional[List]:
85
- return self._scene_bounds
86
-
87
- @property
88
- def mesh_block_count(self) -> int:
89
- return self._mesh_block_count
90
-
91
- @property
92
- def normalize_geometry(self) -> bool:
93
- return self._normalize_geometry
94
-
95
- @normalize_geometry.setter
96
- def normalize_geometry(self, value: bool) -> None:
97
- self._normalize_geometry = value
98
-
99
- @property
100
- def variables(self) -> dict:
101
- return self._variables
102
-
103
- @property
104
- def groups(self) -> dict:
105
- return self._groups
106
-
107
- @property
108
- def part(self) -> Part:
109
- return self._part
110
-
111
- @property
112
- def time_limits(self) -> List:
113
- return self._time_limits
114
-
115
- @property
116
- def cur_timeline(self) -> List:
117
- return self._cur_timeline
118
-
119
- @cur_timeline.setter
120
- def cur_timeline(self, timeline: List) -> None:
121
- self._cur_timeline = timeline
122
- self._time_limits[0] = min(self._time_limits[0], self._cur_timeline[0])
123
- self._time_limits[1] = max(self._time_limits[1], self._cur_timeline[1])
124
-
125
- @property
126
- def vrmode(self) -> bool:
127
- """No camera support for the present."""
128
- return True
129
-
130
- def log(self, s: str, level: int = 0) -> None:
131
- """Log a string to the logging system
132
-
133
- If the message level is less than the current verbosity,
134
- emit the message.
135
- """
136
- if level < self._verbose:
137
- logging.info(s)
138
-
139
- def _update_status_file(self, timed: bool = False):
140
- """
141
- Update the status file contents. The status file will contain the
142
- following json object, stored as: self._status
143
-
144
- {
145
- 'status' : "working|idle",
146
- 'start_time' : timestamp_of_update_begin,
147
- 'processed_buffers' : number_of_protobuffers_processed,
148
- 'total_buffers' : number_of_protobuffers_total,
149
- }
150
-
151
- Parameters
152
- ----------
153
- timed : bool, optional:
154
- if True, only update every second.
155
-
156
- """
157
- if self._status_file:
158
- current_time = time.time()
159
- if timed:
160
- last_time = self._status.get("last_time", 0.0)
161
- if current_time - last_time < 1.0: # type: ignore
162
- return
163
- self._status["last_time"] = current_time
164
- try:
165
- message = json.dumps(self._status)
166
- with open(self._status_file, "w") as status_file:
167
- status_file.write(message)
168
- except IOError:
169
- pass # Note failure is expected here in some cases
170
-
171
- def _parse_mesh(self, meshid: Any) -> None:
172
- mesh = self._gltf.meshes[meshid]
173
- logging.warning(f"mesh id: {meshid}, {mesh}")
174
- for prim in mesh.primitives:
175
- # TODO: GLB Prim -> DSG Part
176
- self.log(f"prim {prim}")
177
- # TODO: GLB Attributes -> DSG Geom
178
-
179
- # mesh.mode, mesh.indices
180
- # mesh.attributes(POSITION, NORMAL, COLOR_0, TEXCOORD_0, TEXCOORD_1)
181
- # mesh.material
182
- # mesh.images
183
-
184
- def _walk_node(self, nodeid: Any) -> None:
185
- node = self._gltf.nodes[nodeid]
186
- self.log(f"node id: {nodeid}, {node}")
187
- # TODO: GLB node -> DSG Group
188
-
189
- if node.mesh is not None:
190
- self._parse_mesh(node.mesh)
191
-
192
- # Handle node.rotation, node.translation, node.scale, node.matrix
193
- for child_id in node.children:
194
- self._walk_node(child_id)
195
-
196
- def upload_file(self, glb_filename: str) -> bool:
197
- """Parse a GLB file and call out to the handler to present the data
198
- to another interface (e.g. Omniverse)
199
-
200
- Parameters
201
- ----------
202
- glb_filename : str
203
- The name of the GLB file to parse
204
-
205
- Returns
206
- -------
207
- bool:
208
- returns True on success, False otherwise
209
- """
210
- try:
211
- ok = True
212
- self._gltf = pygltflib.GLTF2().load(glb_filename)
213
- self.log(f"File: {glb_filename} Info: {self._gltf.asset}")
214
-
215
- self._callback_handler.begin_update()
216
- self._update_status_file()
217
-
218
- # TODO: Variables, Textures
219
-
220
- # TODO: GLB Scene -> DSG View
221
-
222
- # for present, just the default scene
223
- for node_id in self._gltf.scenes[self._gltf.scene].nodes:
224
- self._walk_node(node_id)
225
-
226
- self._finish_part()
227
- self._callback_handler.end_update()
228
-
229
- except Exception as e:
230
- self.log(f"Error: Unable to process: {glb_filename} : {e}")
231
- ok = False
232
-
233
- self._reset()
234
- self._update_status_file()
235
- return ok
236
-
237
- def _finish_part(self) -> None:
238
- """Complete the current part
239
-
240
- There is always a part being modified. This method completes the current part, committing
241
- it to the handler.
242
- """
243
- self._callback_handler.finalize_part(self.part)
244
- self._mesh_block_count += 1
245
-
246
- def _name(self, node: Any) -> str:
247
- if node.name:
248
- return node.name
249
- self._node_idx += 1
250
- if self._node_idx == 0:
251
- return "Root"
252
- return f"Node_{self._node_idx}"
253
-
254
- def _create_pb(
255
- self, cmd_type: str, parent_id: int = -1, name: str = ""
256
- ) -> "dynamic_scene_graph_pb2.SceneUpdateCommand":
257
- cmd = dynamic_scene_graph_pb2.SceneUpdateCommand()
258
- if cmd_type == "PART":
259
- cmd.command_type = dynamic_scene_graph_pb2.SceneUpdateCommand.UPDATE_PART
260
- subcmd = cmd.update_part
261
- elif cmd_type == "GROUP":
262
- cmd.command_type = dynamic_scene_graph_pb2.SceneUpdateCommand.UPDATE_GROUP
263
- subcmd = cmd.update_group
264
- elif cmd_type == "VARIABLE":
265
- cmd.command_type = dynamic_scene_graph_pb2.SceneUpdateCommand.UPDATE_VARIABLE
266
- subcmd = cmd.update_variable
267
- elif cmd_type == "GEOM":
268
- cmd.command_type = dynamic_scene_graph_pb2.SceneUpdateCommand.UPDATE_GEOM
269
- subcmd = cmd.update_geom
270
- elif cmd_type == "VIEW":
271
- cmd.command_type = dynamic_scene_graph_pb2.SceneUpdateCommand.UPDATE_VIEW
272
- subcmd = cmd.update_view
273
- subcmd.id = self._next_id()
274
- if parent_id >= 0:
275
- subcmd.parent_id = parent_id
276
- if cmd_type not in ("GEOM", "VIEW"):
277
- if name:
278
- subcmd.name = name
279
- return cmd, subcmd
1
+ import io
2
+ import logging
3
+ import os
4
+ import sys
5
+ from typing import Any, List, Optional
6
+ import uuid
7
+
8
+ from PIL import Image
9
+ from ansys.api.pyensight.v0 import dynamic_scene_graph_pb2
10
+ import ansys.pyensight.core.utils.dsg_server as dsg_server
11
+ import numpy
12
+ import pygltflib
13
+
14
+ sys.path.insert(0, os.path.dirname(__file__))
15
+ from dsg_server import UpdateHandler # noqa: E402
16
+
17
+
18
+ class GLBSession(dsg_server.DSGSession):
19
+ def __init__(
20
+ self,
21
+ verbose: int = 0,
22
+ normalize_geometry: bool = False,
23
+ time_scale: float = 1.0,
24
+ vrmode: bool = False,
25
+ handler: UpdateHandler = UpdateHandler(),
26
+ ):
27
+ """
28
+ Provide an interface to read a GLB file and link it to an UpdateHandler instance
29
+
30
+ This class reads GLB files and provides the data to an UpdateHandler instance for
31
+ further processing.
32
+
33
+ Parameters
34
+ ----------
35
+ verbose : int
36
+ The verbosity level. If set to 1 or higher the class will call logging.info
37
+ for log output. The default is ``0``.
38
+ normalize_geometry : bool
39
+ If True, the scene coordinates will be remapped into the volume [-1,-1,-1] - [1,1,1]
40
+ The default is not to remap coordinates.
41
+ time_scale : float
42
+ Scale time values by this factor after being read. The default is ``1.0``.
43
+ vrmode : bool
44
+ If True, do not include the camera in the output.
45
+ handler : UpdateHandler
46
+ This is an UpdateHandler subclass that is called back when the state of
47
+ a scene transfer changes. For example, methods are called when the
48
+ transfer begins or ends and when a Part (mesh block) is ready for processing.
49
+ """
50
+ super().__init__(
51
+ verbose=verbose,
52
+ normalize_geometry=normalize_geometry,
53
+ time_scale=time_scale,
54
+ vrmode=vrmode,
55
+ handler=handler,
56
+ )
57
+ self._gltf: pygltflib.GLTF2 = pygltflib.GLTF2()
58
+ self._id_num: int = 0
59
+ self._node_idx: int = -1
60
+ self._glb_textures: dict = {}
61
+ self._scene_id: int = 0
62
+
63
+ def _reset(self) -> None:
64
+ """
65
+ Reset the current state to prepare for a new dataset.
66
+ """
67
+ super()._reset()
68
+ self._cur_timeline = [0.0, 0.0] # Start/End time for current update
69
+ self._status = dict(status="idle", start_time=0.0, processed_buffers=0, total_buffers=0)
70
+ self._gltf = pygltflib.GLTF2()
71
+ self._node_idx = -1
72
+ self._id_num = 0
73
+ self._glb_textures = {}
74
+ self._scene_id = 0
75
+
76
+ def _next_id(self) -> int:
77
+ """Simple sequential number source
78
+ Called whenever a unique integer is needed.
79
+
80
+ Returns
81
+ -------
82
+ int
83
+ A unique, monotonically increasing integer.
84
+ """
85
+ self._id_num += 1
86
+ return self._id_num
87
+
88
+ def _map_material(self, glb_materialid: int, part_pb: Any) -> None:
89
+ """
90
+ Apply various material properties to part protocol buffer.
91
+
92
+ Parameters
93
+ ----------
94
+ glb_materialid : int
95
+ The GLB material ID to use as the source information.
96
+ part_pb : Any
97
+ The DSG UpdatePart protocol buffer to update.
98
+ """
99
+ mat = self._gltf.materials[glb_materialid]
100
+ color = [1.0, 1.0, 1.0, 1.0]
101
+ # Change the color if we can find one
102
+ if hasattr(mat, "pbrMetallicRoughness"):
103
+ if hasattr(mat.pbrMetallicRoughness, "baseColorFactor"):
104
+ color = mat.pbrMetallicRoughness.baseColorFactor
105
+ part_pb.fill_color.extend(color)
106
+ part_pb.line_color.extend(color)
107
+ # Constants for now
108
+ part_pb.ambient = 1.0
109
+ part_pb.diffuse = 1.0
110
+ part_pb.specular_intensity = 1.0
111
+ # if the material maps to a variable, set the variable id for coloring
112
+ glb_varid = self._find_variable_from_glb_mat(glb_materialid)
113
+ if glb_varid:
114
+ part_pb.color_variableid = glb_varid
115
+
116
+ def _parse_mesh(self, meshid: int, parentid: int, parentname: str) -> None:
117
+ """
118
+ Walk a mesh id found in a "node" instance. This amounts to
119
+ walking the list of "primitives" in the "meshes" list indexed
120
+ by the meshid.
121
+
122
+ Parameters
123
+ ----------
124
+ meshid: int
125
+ The index of the mesh in the "meshes" list.
126
+
127
+ parentid: int
128
+ The DSG parent id.
129
+
130
+ parentname: str
131
+ The name of the GROUP parent of the meshes.
132
+ """
133
+ mesh = self._gltf.meshes[meshid]
134
+ for prim_idx, prim in enumerate(mesh.primitives):
135
+ # POINTS, LINES, LINE_LOOP, LINE_STRIP, TRIANGLES, TRIANGLE_STRIP, TRIANGLE_FAN
136
+ mode = prim.mode
137
+ if mode not in (pygltflib.TRIANGLES, pygltflib.LINES, pygltflib.POINTS):
138
+ self.warn(
139
+ f"Unhandled connectivity {mode}. Currently only TRIANGLE and LINE connectivity is supported."
140
+ )
141
+ continue
142
+ glb_materialid = prim.material
143
+
144
+ # GLB Prim -> DSG Part
145
+ part_name = f"{parentname}_prim{prim_idx}_"
146
+ cmd, part_pb = self._create_pb("PART", parent_id=parentid, name=part_name)
147
+ part_pb.render = dynamic_scene_graph_pb2.UpdatePart.RenderingMode.CONNECTIVITY
148
+ part_pb.shading = dynamic_scene_graph_pb2.UpdatePart.ShadingMode.NODAL
149
+ self._map_material(glb_materialid, part_pb)
150
+ part_dsg_id = part_pb.id
151
+ self._handle_update_command(cmd)
152
+
153
+ # GLB Attributes -> DSG Geom
154
+ conn = self._get_data(prim.indices, 0)
155
+ cmd, conn_pb = self._create_pb("GEOM", parent_id=part_dsg_id)
156
+ if mode == pygltflib.TRIANGLES:
157
+ conn_pb.payload_type = dynamic_scene_graph_pb2.UpdateGeom.ArrayType.TRIANGLES
158
+ elif mode == pygltflib.LINES:
159
+ conn_pb.payload_type = dynamic_scene_graph_pb2.UpdateGeom.ArrayType.LINES
160
+ else:
161
+ conn_pb.payload_type = dynamic_scene_graph_pb2.UpdateGeom.ArrayType.POINTS
162
+ conn_pb.int_array.extend(conn)
163
+ conn_pb.chunk_offset = 0
164
+ conn_pb.total_array_size = len(conn)
165
+ self._handle_update_command(cmd)
166
+
167
+ if prim.attributes.POSITION is not None:
168
+ verts = self._get_data(prim.attributes.POSITION)
169
+ cmd, verts_pb = self._create_pb("GEOM", parent_id=part_dsg_id)
170
+ verts_pb.payload_type = dynamic_scene_graph_pb2.UpdateGeom.ArrayType.COORDINATES
171
+ verts_pb.flt_array.extend(verts)
172
+ verts_pb.chunk_offset = 0
173
+ verts_pb.total_array_size = len(verts)
174
+ self._handle_update_command(cmd)
175
+
176
+ if prim.attributes.NORMAL is not None:
177
+ normals = self._get_data(prim.attributes.NORMAL)
178
+ cmd, normals_pb = self._create_pb("GEOM", parent_id=part_dsg_id)
179
+ normals_pb.payload_type = dynamic_scene_graph_pb2.UpdateGeom.ArrayType.NODE_NORMALS
180
+ normals_pb.flt_array.extend(normals)
181
+ normals_pb.chunk_offset = 0
182
+ normals_pb.total_array_size = len(normals)
183
+ self._handle_update_command(cmd)
184
+
185
+ if prim.attributes.TEXCOORD_0 is not None:
186
+ # Note: texture coords are stored as VEC2, so we get 2 components back
187
+ texcoords = self._get_data(prim.attributes.TEXCOORD_0, components=2)
188
+ # we only want the 's' component of an s,t pairing
189
+ texcoords = texcoords[::2]
190
+ cmd, texcoords_pb = self._create_pb("GEOM", parent_id=part_dsg_id)
191
+ texcoords_pb.payload_type = (
192
+ dynamic_scene_graph_pb2.UpdateGeom.ArrayType.NODE_VARIABLE
193
+ )
194
+ texcoords_pb.flt_array.extend(texcoords)
195
+ texcoords_pb.chunk_offset = 0
196
+ texcoords_pb.total_array_size = len(texcoords)
197
+ glb_varid = self._find_variable_from_glb_mat(glb_materialid)
198
+ if glb_varid:
199
+ texcoords_pb.variable_id = glb_varid
200
+ self._handle_update_command(cmd)
201
+
202
+ def _get_data(
203
+ self,
204
+ accessorid: int,
205
+ components: int = 3,
206
+ ) -> numpy.ndarray:
207
+ """
208
+ Return the float buffer corresponding to the given accessorid. The id
209
+ is usually obtained from a primitive: primitive.attributes.POSITION
210
+ or primitive.attributes.NORMAL or primitive.attributes.TEXCOORD_0.
211
+ It can also come from primitive.indices. In that case, the number of
212
+ components needs to be set to 0.
213
+
214
+ Parameters
215
+ ----------
216
+ accessorid: int
217
+ The accessor index of the primitive.
218
+
219
+ components: int
220
+ The number of floats per vertex for the values 1,2,3 if the number
221
+ of components is 0, read integer indices.
222
+
223
+ Returns
224
+ -------
225
+ numpy.ndarray
226
+ The float buffer corresponding to the nodal data or an int buffer of connectivity.
227
+ """
228
+ dtypes = {}
229
+ dtypes[pygltflib.BYTE] = numpy.int8
230
+ dtypes[pygltflib.UNSIGNED_BYTE] = numpy.uint8
231
+ dtypes[pygltflib.SHORT] = numpy.int16
232
+ dtypes[pygltflib.UNSIGNED_SHORT] = numpy.uint16
233
+ dtypes[pygltflib.UNSIGNED_INT] = numpy.uint32
234
+ dtypes[pygltflib.FLOAT] = numpy.float32
235
+
236
+ binary_blob = self._gltf.binary_blob()
237
+ accessor = self._gltf.accessors[accessorid]
238
+ buffer_view = self._gltf.bufferViews[accessor.bufferView]
239
+ dtype = numpy.float32
240
+ data_dtype = dtypes[accessor.componentType]
241
+ count = accessor.count * components
242
+ # connectivity
243
+ if components == 0:
244
+ dtype = numpy.uint32
245
+ count = accessor.count
246
+ offset = buffer_view.byteOffset + accessor.byteOffset
247
+ blob = binary_blob[offset : offset + buffer_view.byteLength]
248
+ ret = numpy.frombuffer(blob, dtype=data_dtype, count=count)
249
+ if data_dtype != dtype:
250
+ return ret.astype(dtype)
251
+ return ret
252
+
253
+ def _walk_node(self, nodeid: int, parentid: int) -> None:
254
+ """
255
+ Given a node id (likely from walking a scenes array), walk the mesh
256
+ objects in the node. A "node" has the keys "mesh" and "name".
257
+
258
+ Each node has a single mesh object in it.
259
+
260
+ Parameters
261
+ ----------
262
+ nodeid: int
263
+ The node id to walk.
264
+
265
+ parentid: int
266
+ The DSG parent id.
267
+
268
+ """
269
+ node = self._gltf.nodes[nodeid]
270
+ name = self._name(node)
271
+ matrix = self._transform(node)
272
+
273
+ # GLB node -> DSG Group
274
+ cmd, group_pb = self._create_pb("GROUP", parent_id=parentid, name=name)
275
+ group_pb.matrix4x4.extend(matrix)
276
+ self._handle_update_command(cmd)
277
+
278
+ if node.mesh is not None:
279
+ self._parse_mesh(node.mesh, group_pb.id, name)
280
+
281
+ # Handle node.rotation, node.translation, node.scale, node.matrix
282
+ for child_id in node.children:
283
+ self._walk_node(child_id, group_pb.id)
284
+
285
+ def start_uploads(self, timeline: List[float]) -> None:
286
+ """
287
+ Begin an upload process for a potential collection of files.
288
+
289
+ Parameters
290
+ ----------
291
+ timeline : List[float]
292
+ The time values for the files span this range of values.
293
+ """
294
+ self._scene_id = self._next_id()
295
+ self._cur_timeline = timeline
296
+ self._callback_handler.begin_update()
297
+ self._update_status_file()
298
+
299
+ def end_uploads(self) -> None:
300
+ """
301
+ The upload process for the current collection of files is complete.
302
+ """
303
+ self._reset()
304
+ self._update_status_file()
305
+
306
+ def _find_variable_from_glb_mat(self, glb_material_id: int) -> Optional[int]:
307
+ """
308
+ Given a glb_material id, find the corresponding dsg variable id
309
+
310
+ Parameters
311
+ ----------
312
+ glb_material_id : int
313
+ The material id from the glb file.
314
+
315
+ Returns
316
+ -------
317
+ Optional[int]
318
+ The dsg variable id or None, if no variable is found.
319
+ """
320
+ value = self._glb_textures.get(glb_material_id, None)
321
+ if value is not None:
322
+ return value["pb"].id
323
+ return None
324
+
325
+ def upload_file(self, glb_filename: str, timeline: List[float] = [0.0, 0.0]) -> bool:
326
+ """
327
+ Parse a GLB file and call out to the handler to present the data
328
+ to another interface (e.g. Omniverse)
329
+
330
+ Parameters
331
+ ----------
332
+ timeline : List[float]
333
+ The first and last time value for which the content of this file should be
334
+ visible.
335
+
336
+ glb_filename : str
337
+ The name of the GLB file to parse
338
+
339
+ Returns
340
+ -------
341
+ bool:
342
+ returns True on success, False otherwise
343
+ """
344
+ try:
345
+ ok = True
346
+ self._gltf = pygltflib.GLTF2().load(glb_filename)
347
+ self.log(f"File: {glb_filename} Info: {self._gltf.asset}")
348
+
349
+ # check for GLTFWriter source
350
+ if (self._gltf.asset.generator is None) or (
351
+ ("GLTF Writer" not in self._gltf.asset.generator)
352
+ and ("Ansys Ensight" not in self._gltf.asset.generator)
353
+ ):
354
+ self.error(
355
+ f"Unable to process: {glb_filename} : Not written by GLTF Writer library"
356
+ )
357
+ return False
358
+
359
+ # Walk texture nodes -> DSG Variable buffers
360
+ for tex_idx, texture in enumerate(self._gltf.textures):
361
+ image = self._gltf.images[texture.source]
362
+ if image.uri is None:
363
+ bv = self._gltf.bufferViews[image.bufferView]
364
+ raw_png = self._gltf.binary_blob()[
365
+ bv.byteOffset : bv.byteOffset + bv.byteLength
366
+ ]
367
+ else:
368
+ raw_png = self._gltf.get_data_from_buffer_uri(image.uri)
369
+ png_img = Image.open(io.BytesIO(raw_png))
370
+ raw_rgba = png_img.tobytes()
371
+ raw_rgba = raw_rgba[0 : len(raw_rgba) // png_img.size[1]]
372
+ var_name = "Variable_" + str(tex_idx)
373
+ cmd, var_pb = self._create_pb("VARIABLE", parent_id=self._scene_id, name=var_name)
374
+ var_pb.location = dynamic_scene_graph_pb2.UpdateVariable.VarLocation.NODAL
375
+ var_pb.dimension = dynamic_scene_graph_pb2.UpdateVariable.VarDimension.SCALAR
376
+ var_pb.undefined_value = -1e38
377
+ var_pb.pal_interp = (
378
+ dynamic_scene_graph_pb2.UpdateVariable.PaletteInterpolation.CONTINUOUS
379
+ )
380
+ var_pb.sub_levels = 0
381
+ var_pb.undefined_display = (
382
+ dynamic_scene_graph_pb2.UpdateVariable.UndefinedDisplay.AS_ZERO
383
+ )
384
+ var_pb.texture = raw_rgba
385
+ colors = numpy.frombuffer(raw_rgba, dtype=numpy.uint8)
386
+ colors.shape = (-1, 4)
387
+ num = len(colors)
388
+ levels = []
389
+ for i, c in enumerate(colors):
390
+ level = dynamic_scene_graph_pb2.VariableLevel()
391
+ level.value = float(i) / float(num - 1)
392
+ level.red = float(c[0]) / 255.0
393
+ level.green = float(c[1]) / 255.0
394
+ level.blue = float(c[2]) / 255.0
395
+ level.alpha = float(c[3]) / 255.0
396
+ levels.append(level)
397
+ var_pb.levels.extend(levels)
398
+ # create a map from GLB material index to glb
399
+ d = dict(pb=var_pb, idx=tex_idx)
400
+ # Find all the materials that map to this texture
401
+ for mat_idx, mat in enumerate(self._gltf.materials):
402
+ if not hasattr(mat, "pbrMetallicRoughness"):
403
+ continue
404
+ if not hasattr(mat.pbrMetallicRoughness, "baseColorTexture"):
405
+ continue
406
+ if not hasattr(mat.pbrMetallicRoughness.baseColorTexture, "index"):
407
+ continue
408
+ if mat.pbrMetallicRoughness.baseColorTexture.index == tex_idx:
409
+ material_index = mat_idx
410
+ # does this Variable/texture already exist?
411
+ duplicate = None
412
+ saved_id = var_pb.id
413
+ saved_name = var_pb.name
414
+ for key, value in self._glb_textures.items():
415
+ var_pb.name = value["pb"].name
416
+ var_pb.id = value["pb"].id
417
+ if value["pb"] == var_pb:
418
+ duplicate = key
419
+ break
420
+ var_pb.id = saved_id
421
+ var_pb.name = saved_name
422
+ # if a new texture, add the Variable and create an index to the material
423
+ if duplicate is None:
424
+ self._handle_update_command(cmd)
425
+ self._glb_textures[material_index] = d
426
+ else:
427
+ # create an additional reference to this variable from this material
428
+ self._glb_textures[material_index] = self._glb_textures[duplicate]
429
+
430
+ # GLB file: general layout
431
+ # scene: "default_index"
432
+ # scenes: [scene_index].nodes -> [node ids]
433
+ # was scene_id = self._gltf.scene
434
+ num_scenes = len(self._gltf.scenes)
435
+ for scene_idx in range(num_scenes):
436
+ # GLB Scene -> DSG View
437
+ cmd, view_pb = self._create_pb("VIEW", parent_id=self._scene_id)
438
+ view_pb.lookat.extend([0.0, 0.0, -1.0])
439
+ view_pb.lookfrom.extend([0.0, 0.0, 0.0])
440
+ view_pb.upvector.extend([0.0, 1.0, 0.0])
441
+ view_pb.timeline.extend(self._build_scene_timeline(scene_idx, timeline))
442
+ if len(self._gltf.cameras) > 0:
443
+ camera = self._gltf.cameras[0]
444
+ if camera.type == "orthographic":
445
+ view_pb.nearfar.extend(
446
+ [float(camera.orthographic.znear), float(camera.orthographic.zfar)]
447
+ )
448
+ else:
449
+ view_pb.nearfar.extend(
450
+ [float(camera.perspective.znear), float(camera.perspective.zfar)]
451
+ )
452
+ view_pb.fieldofview = camera.perspective.yfov
453
+ view_pb.aspectratio = camera.aspectratio.aspectRatio
454
+ self._handle_update_command(cmd)
455
+ for node_id in self._gltf.scenes[scene_idx].nodes:
456
+ self._walk_node(node_id, view_pb.id)
457
+ self._finish_part()
458
+
459
+ self._callback_handler.end_update()
460
+
461
+ except Exception as e:
462
+ import traceback
463
+
464
+ self.error(f"Unable to process: {glb_filename} : {e}")
465
+ traceback_str = "".join(traceback.format_tb(e.__traceback__))
466
+ logging.debug(f"Traceback: {traceback_str}")
467
+ ok = False
468
+
469
+ return ok
470
+
471
+ def _build_scene_timeline(self, scene_idx: int, input_timeline: List[float]) -> List[float]:
472
+ """
473
+ For a given scene and externally supplied timeline, compute the timeline for the scene.
474
+
475
+ If the ANSYS_scene_time extension is present, use that value.
476
+ If there is only a single scene, return the supplied timeline.
477
+ If the supplied timeline is empty, use an integer timeline based on the number of scenes in the GLB file.
478
+ Carve up the timeline into chunks, one per scene.
479
+
480
+ Parameters
481
+ ----------
482
+ scene_idx: int
483
+ The index of the scene to compute for.
484
+
485
+ input_timeline: List[float]
486
+ An externally supplied timeline.
487
+
488
+ Returns
489
+ -------
490
+ List[float]
491
+ The computed timeline.
492
+ """
493
+ # if ANSYS_scene_time is used, time ranges will come from there
494
+ if "ANSYS_scene_time" in self._gltf.scenes[scene_idx].extensions:
495
+ return self._gltf.scenes[scene_idx].extensions["ANSYS_scene_time"]
496
+ # if there is only one scene, then use the input timeline
497
+ num_scenes = len(self._gltf.scenes)
498
+ if num_scenes == 1:
499
+ return input_timeline
500
+ # if the timeline has zero length, we make it the number of scenes
501
+ timeline = input_timeline
502
+ if timeline[1] - timeline[0] <= 0.0:
503
+ timeline = [0.0, float(num_scenes - 1)]
504
+ # carve time into the input timeline.
505
+ delta = (timeline[1] - timeline[0]) / float(num_scenes)
506
+ output: List[float] = []
507
+ output.append(float(scene_idx) * delta + timeline[0])
508
+ output.append(output[0] + delta)
509
+ return output
510
+
511
+ @staticmethod
512
+ def _transform(node: Any) -> List[float]:
513
+ """
514
+ Convert the node "matrix" or "translation", "rotation" and "scale" values into
515
+ a 4x4 matrix representation.
516
+
517
+ "nodes": [
518
+ {
519
+ "matrix": [
520
+ 1,0,0,0,
521
+ 0,1,0,0,
522
+ 0,0,1,0,
523
+ 5,6,7,1
524
+ ],
525
+ ...
526
+ },
527
+ {
528
+ "translation":
529
+ [ 0,0,0 ],
530
+ "rotation":
531
+ [ 0,0,0,1 ],
532
+ "scale":
533
+ [ 1,1,1 ]
534
+ ...
535
+ },
536
+ ]
537
+
538
+ Parameters
539
+ ----------
540
+ node: Any
541
+ The node to compute the matrix transform for.
542
+
543
+ Returns
544
+ -------
545
+ List[float]
546
+ The 4x4 transformation matrix.
547
+
548
+ """
549
+ identity = numpy.identity(4)
550
+ if node.matrix:
551
+ tmp = numpy.array(node.matrix)
552
+ tmp.shape = (4, 4)
553
+ tmp = tmp.transpose()
554
+ return list(tmp.flatten())
555
+ if node.translation:
556
+ identity[3][0] = node.translation[0]
557
+ identity[3][1] = node.translation[1]
558
+ identity[3][2] = node.translation[2]
559
+ if node.rotation:
560
+ # In GLB, the null quat is [0,0,0,1] so reverse the vector here
561
+ q = [node.rotation[3], node.rotation[0], node.rotation[1], node.rotation[2]]
562
+ rot = numpy.array(
563
+ [
564
+ [q[0], -q[1], -q[2], -q[3]],
565
+ [q[1], q[0], -q[3], q[2]],
566
+ [q[2], q[3], q[0], -q[1]],
567
+ [q[3], -q[2], q[1], q[0]],
568
+ ]
569
+ )
570
+ identity = numpy.multiply(identity, rot)
571
+ if node.scale:
572
+ s = node.scale
573
+ scale = numpy.array(
574
+ [
575
+ [s[0], 0.0, 0.0, 0.0],
576
+ [0.0, s[1], 0.0, 0.0],
577
+ [0.0, 0.0, s[2], 0.0],
578
+ [0.0, 0.0, 0.0, 1.0],
579
+ ]
580
+ )
581
+ identity = numpy.multiply(identity, scale)
582
+ return list(identity.flatten())
583
+
584
+ def _name(self, node: Any) -> str:
585
+ """
586
+ Given a GLB node object, return the name of the node. If the node does not
587
+ have a name, give it a generated node.
588
+
589
+ Parameters
590
+ ----------
591
+ node: Any
592
+ The GLB node to get the name of.
593
+
594
+ Returns
595
+ -------
596
+ str
597
+ The name of the node.
598
+ """
599
+ if hasattr(node, "name") and node.name:
600
+ return node.name
601
+ self._node_idx += 1
602
+ return f"Node_{self._node_idx}"
603
+
604
+ def _create_pb(
605
+ self, cmd_type: str, parent_id: int = -1, name: str = ""
606
+ ) -> "dynamic_scene_graph_pb2.SceneUpdateCommand":
607
+ cmd = dynamic_scene_graph_pb2.SceneUpdateCommand()
608
+ if cmd_type == "PART":
609
+ cmd.command_type = dynamic_scene_graph_pb2.SceneUpdateCommand.UPDATE_PART
610
+ subcmd = cmd.update_part
611
+ subcmd.hash = str(uuid.uuid1())
612
+ elif cmd_type == "GROUP":
613
+ cmd.command_type = dynamic_scene_graph_pb2.SceneUpdateCommand.UPDATE_GROUP
614
+ subcmd = cmd.update_group
615
+ elif cmd_type == "VARIABLE":
616
+ cmd.command_type = dynamic_scene_graph_pb2.SceneUpdateCommand.UPDATE_VARIABLE
617
+ subcmd = cmd.update_variable
618
+ elif cmd_type == "GEOM":
619
+ cmd.command_type = dynamic_scene_graph_pb2.SceneUpdateCommand.UPDATE_GEOM
620
+ subcmd = cmd.update_geom
621
+ subcmd.hash = str(uuid.uuid1())
622
+ elif cmd_type == "VIEW":
623
+ cmd.command_type = dynamic_scene_graph_pb2.SceneUpdateCommand.UPDATE_VIEW
624
+ subcmd = cmd.update_view
625
+ subcmd.id = self._next_id()
626
+ if parent_id >= 0:
627
+ subcmd.parent_id = parent_id
628
+ if cmd_type not in ("GEOM", "VIEW"):
629
+ if name:
630
+ subcmd.name = name
631
+ return cmd, subcmd