Skip to content

Commit

Permalink
Add time scaling to temporal export
Browse files Browse the repository at this point in the history
Improve error handling
Add "time_scale" mechanism to allow timeline adjustment during export.
  • Loading branch information
randallfrank committed Jul 12, 2024
1 parent bc75539 commit bbeb54d
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 18 deletions.
53 changes: 47 additions & 6 deletions exts/ansys.geometry.service/ansys/geometry/service/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

try:
import ansys.pyensight.core
import ansys.pyensight.core.utils.dsg_server as tmp_dsg_server # noqa: F401
import ansys.pyensight.core.utils.omniverse_dsg_server as tmp_ov_dsg_server # noqa: F401
except ModuleNotFoundError:
logging.warning("ansys.geometry.server - Installing ansys-pyensight-core")
omni.kit.pipapi.install("ansys-pyensight-core")
Expand Down Expand Up @@ -82,6 +84,11 @@ def __init__(self, *args, **kwargs) -> None:
self._security_token = self._setting("securityCode", "ENSIGHT_SECURITY_TOKEN")
self._temporal = self._setting("temporal") != "0"
self._vrmode = self._setting("vrmode") != "0"
try:
scale = float(self._setting("timeScale"))
except ValueError:
scale = 1.0
self._time_scale = scale
self._normalize_geometry = self._setting("normalizeGeometry") != "0"
self._version = "unknown"
self._shutdown = False
Expand Down Expand Up @@ -141,6 +148,15 @@ def normalize_geometry(self) -> bool:
def normalize_geometry(self, val: bool) -> None:
self._normalize_geometry = val

@property
def time_scale(self) -> float:
"""Value to multiply DSG time values by before passing to Omniverse"""
return self._time_scale

@time_scale.setter
def time_scale(self, value: float) -> None:
self._time_scale = value

@classmethod
def get_instance(cls) -> Optional["AnsysGeometryServiceServerExtension"]:
return cls._service_instance
Expand Down Expand Up @@ -260,18 +276,40 @@ def help(self) -> None:
self.warning(
f" If non-zero, remap the geometry to the domain [-1,-1,-1]-[1,1,1]. (default: {self.normalize_geometry})"
)
self.warning(" --/exts/ansys.geometry.service/timeScale=FLOAT")
self.warning(
f" Multiply all DSG time values by this value. (default: {self.time_scale})"
)

def is_server_running(self) -> bool:
"""
Returns True if the server is running.
Returns
-------
bool
True if the server is running.
"""
if self._server_process:
if psutil.pid_exists(self._server_process.pid):
return True
return False

def stop_server(self) -> None:
"""
If a DSG server connection has been started, stop it. It could be in
process or a subprocess.
"""
self._shutdown = True
if self._server_process:
for child in psutil.Process(self._server_process.pid).children(recursive=True):
child.kill()
self._server_process.kill()
self._server_process = None
try:
self._shutdown = True
if self._server_process:
for child in psutil.Process(self._server_process.pid).children(recursive=True):
child.kill()
self._server_process.kill()
except psutil.NoSuchProcess:
pass
self._server_process = None
self._shutdown = False

def launch_server(self) -> None:
"""
Expand Down Expand Up @@ -300,6 +338,8 @@ def launch_server(self) -> None:
cmd.append("--/exts/ansys.geometry.service/vrmode=1")
if self.normalize_geometry:
cmd.append("--/exts/ansys.geometry.service/normalizeGeometry=1")
if self.time_scale != 1.0:
cmd.append(f"--/exts/ansys.geometry.service/timeScale={self.time_scale}")
cmd.append(f"--/exts/ansys.geometry.service/omniUrl={self.omni_uri}")
cmd.append(f"--/exts/ansys.geometry.service/dsgUrl={self.dsg_uri}")
cmd.append("--/exts/ansys.geometry.service/run=1")
Expand Down Expand Up @@ -344,6 +384,7 @@ def run_server(self) -> None:
security_code=self.security_token,
verbose=1,
normalize_geometry=self.normalize_geometry,
time_scale=self.time_scale,
handler=update_handler,
)

Expand Down
3 changes: 2 additions & 1 deletion exts/ansys.geometry.service/config/extension.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
# Semantic Versioning is used: https://semver.org/
version = "0.8.5"
version = "0.8.6"

# Lists people or organizations that are considered the "authors" of the package.
authors = ["ANSYS"]
Expand Down Expand Up @@ -56,3 +56,4 @@ exts."ansys.geometry.service".securityCode = ""
exts."ansys.geometry.service".temporal = "0"
exts."ansys.geometry.service".vrmode = "0"
exts."ansys.geometry.service".normalizeGeometry = "0"
exts."ansys.geometry.service".timeScale = "1.0"
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __init__(self, *args, **kwargs) -> None:
self._temporal_w = None
self._vrmode_w = None
self._normalize_w = None
self._time_scale_w = None
self._connect_w = None
self._update_w = None
self._connected = False
Expand All @@ -38,15 +39,22 @@ def error(self, text: str) -> None:
self._logger.error(text)

def launch_server(self) -> None:
if self._connected:
if self.service.is_server_running():
return
self.service.dsg_uri = self._dsg_uri_w.model.as_string
self.service.security_token = self._dsg_token_w.model.as_string
self.service.omni_uri = self._omni_uri_w.model.as_string
self.service.temporal = self._temporal_w.model.as_bool
self.service.vrmode = self._vrmode_w.model.as_bool
self.service.normalize_geometry = self._normalize_w.model.as_bool
scale = self._time_scale_w.model.as_float
if scale <= 0.0:
scale = 1.0
self.service.time_scale = scale
self.service.launch_server()
if not self.service.is_server_running():
self.error("Failed to launch omniverse service.")
return

# parse the DSG USI
parsed = urlparse(self.service.dsg_uri)
Expand All @@ -60,6 +68,9 @@ def launch_server(self) -> None:
host=host, port=port, secret_key=self.service.security_token
)
self._grpc.connect()
if not self._grpc.is_connected():
self.error(f"Failed to connect to DSG service {host}:{port}")
return

self.info("Connected to DSG service")
self._connected = True
Expand All @@ -69,6 +80,7 @@ def stop_server(self) -> None:
return
self.service.stop_server()
self._grpc.shutdown()
self._grpc = None

self.info("Disconnect from DSG service")
self._connected = False
Expand Down Expand Up @@ -104,12 +116,15 @@ def on_startup(self, ext_id: str) -> None:
def update_ui(self) -> None:
if self._connected:
self._connect_w.text = "Disconnect from DSG Server"
self._label_w.text = f"Connected to: {self.service.dsg_uri}"
else:
self._connect_w.text = "Connect to DSG Server"
self._label_w.text = "No connected DSG server"
self._update_w.enabled = self._connected
self._temporal_w.enabled = True
self._vrmode_w.enabled = not self._connected
self._normalize_w.enabled = not self._connected
self._time_scale_w.enabled = not self._connected
self._dsg_uri_w.enabled = not self._connected
self._dsg_token_w.enabled = not self._connected
self._omni_uri_w.enabled = not self._connected
Expand Down Expand Up @@ -151,6 +166,13 @@ def build_ui(self) -> None:
self._normalize_w.model.set_value(self.service.normalize_geometry)
ui.Label("Normalize", alignment=ui.Alignment.LEFT_CENTER)

with ui.HStack(spacing=5):
ui.Label(
"Temporal scaling factor:", alignment=ui.Alignment.RIGHT_CENTER, width=0
)
self._time_scale_w = ui.FloatField()
self._time_scale_w.model.as_float = self.service.time_scale

with ui.HStack():
self._connect_w = ui.Button("Connect to DSG Server", clicked_fn=self.connect_cb)
self._update_w = ui.Button("Request Update", clicked_fn=self.update_cb)
Expand All @@ -166,5 +188,6 @@ def on_shutdown(self) -> None:
self._temporal_w = None
self._vrmode_w = None
self._normalize_w = None
self._time_scale_w = None
self._connect_w = None
self._update_w = None
2 changes: 1 addition & 1 deletion exts/ansys.geometry.serviceui/config/extension.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
# Semantic Versioning is used: https://semver.org/
version = "0.8.5"
version = "0.8.6"

# Lists people or organizations that are considered the "authors" of the package.
authors = ["ANSYS"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

try:
import ansys.pyensight.core
import ansys.pyensight.core.utils.dsg_server as tmp_dsg_server # noqa: F401
import ansys.pyensight.core.utils.omniverse_dsg_server as tmp_ov_dsg_server # noqa: F401
except ModuleNotFoundError:
logging.warning("ansys.geometry.server - Installing ansys-pyensight-core")
omni.kit.pipapi.install("ansys-pyensight-core")
Expand Down Expand Up @@ -82,6 +84,11 @@ def __init__(self, *args, **kwargs) -> None:
self._security_token = self._setting("securityCode", "ENSIGHT_SECURITY_TOKEN")
self._temporal = self._setting("temporal") != "0"
self._vrmode = self._setting("vrmode") != "0"
try:
scale = float(self._setting("timeScale"))
except ValueError:
scale = 1.0
self._time_scale = scale
self._normalize_geometry = self._setting("normalizeGeometry") != "0"
self._version = "unknown"
self._shutdown = False
Expand Down Expand Up @@ -141,6 +148,15 @@ def normalize_geometry(self) -> bool:
def normalize_geometry(self, val: bool) -> None:
self._normalize_geometry = val

@property
def time_scale(self) -> float:
"""Value to multiply DSG time values by before passing to Omniverse"""
return self._time_scale

@time_scale.setter
def time_scale(self, value: float) -> None:
self._time_scale = value

@classmethod
def get_instance(cls) -> Optional["AnsysGeometryServiceServerExtension"]:
return cls._service_instance
Expand Down Expand Up @@ -260,18 +276,40 @@ def help(self) -> None:
self.warning(
f" If non-zero, remap the geometry to the domain [-1,-1,-1]-[1,1,1]. (default: {self.normalize_geometry})"
)
self.warning(" --/exts/ansys.geometry.service/timeScale=FLOAT")
self.warning(
f" Multiply all DSG time values by this value. (default: {self.time_scale})"
)

def is_server_running(self) -> bool:
"""
Returns True if the server is running.
Returns
-------
bool
True if the server is running.
"""
if self._server_process:
if psutil.pid_exists(self._server_process.pid):
return True
return False

def stop_server(self) -> None:
"""
If a DSG server connection has been started, stop it. It could be in
process or a subprocess.
"""
self._shutdown = True
if self._server_process:
for child in psutil.Process(self._server_process.pid).children(recursive=True):
child.kill()
self._server_process.kill()
self._server_process = None
try:
self._shutdown = True
if self._server_process:
for child in psutil.Process(self._server_process.pid).children(recursive=True):
child.kill()
self._server_process.kill()
except psutil.NoSuchProcess:
pass
self._server_process = None
self._shutdown = False

def launch_server(self) -> None:
"""
Expand Down Expand Up @@ -300,6 +338,8 @@ def launch_server(self) -> None:
cmd.append("--/exts/ansys.geometry.service/vrmode=1")
if self.normalize_geometry:
cmd.append("--/exts/ansys.geometry.service/normalizeGeometry=1")
if self.time_scale != 1.0:
cmd.append(f"--/exts/ansys.geometry.service/timeScale={self.time_scale}")
cmd.append(f"--/exts/ansys.geometry.service/omniUrl={self.omni_uri}")
cmd.append(f"--/exts/ansys.geometry.service/dsgUrl={self.dsg_uri}")
cmd.append("--/exts/ansys.geometry.service/run=1")
Expand Down Expand Up @@ -344,6 +384,7 @@ def run_server(self) -> None:
security_code=self.security_token,
verbose=1,
normalize_geometry=self.normalize_geometry,
time_scale=self.time_scale,
handler=update_handler,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
# Semantic Versioning is used: https://semver.org/
version = "0.8.5"
version = "0.8.6"

# Lists people or organizations that are considered the "authors" of the package.
authors = ["ANSYS"]
Expand Down Expand Up @@ -56,3 +56,4 @@ exts."ansys.geometry.service".securityCode = ""
exts."ansys.geometry.service".temporal = "0"
exts."ansys.geometry.service".vrmode = "0"
exts."ansys.geometry.service".normalizeGeometry = "0"
exts."ansys.geometry.service".timeScale = "1.0"
Loading

0 comments on commit bbeb54d

Please sign in to comment.