Skip to content

Commit

Permalink
Merge pull request #393 from howetuft/materialx
Browse files Browse the repository at this point in the history
Materialx
  • Loading branch information
howetuft committed May 2, 2024
2 parents 193c3cf + 38d6ff5 commit 735d316
Showing 1 changed file with 59 additions and 11 deletions.
70 changes: 59 additions & 11 deletions Render/materialx/materialx_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
from .materialx_importer import MaterialXImporter
from .materialx_profile import WEBPROFILE

from Render import ImageLight

# Remark: please do not use:
# - QWebEngineProfile.setDownloadPath
Expand Down Expand Up @@ -99,7 +100,6 @@ def __init__(self, fcdoc, parent, disp2bump=False):
# Set download manager
WEBPROFILE.downloadRequested.connect(self.download_requested)
self._download_required.connect(self.run_download, Qt.QueuedConnection)
self.win = None

# Add actions to toolbar
self.toolbar.addAction(self.view.pageAction(QWebEnginePage.Back))
Expand Down Expand Up @@ -159,16 +159,17 @@ def run_download(self, download):
"""
# Special case: HDRI
if self._is_hdri_download(download):
print("HDRI download is not implemented yet.")
win = HdriDownloadWindow(download, self.fcdoc, self)
win.open()
return

# Nominal materialx import
# Nominal case: materialx import
polyhaven_actual_size = polyhaven_getsize(self.page)

self.win = DownloadWindow(
win = MaterialXDownloadWindow(
download, self.fcdoc, self, self.disp2bump, polyhaven_actual_size
)
self.win.open()
win.open()

def _is_hdri_download(self, download):
"""Check whether download is HDRI (rather than MaterialX)."""
Expand All @@ -192,25 +193,21 @@ def _is_hdri_download(self, download):


class DownloadWindow(QProgressDialog):
"""A simple widget to handle MaterialX download and import from the web.
"""A simple widget to handle download and import from the web.
This is mainly a QProgressDialog, with ability to trace progress of a
download from WebEngineProfile and handle import afterwards.
download from WebEngineProfile and trigger import afterwards.
"""

def __init__(
self,
download,
fcdoc,
parent,
disp2bump=False,
polyhaven_actual_size=None,
):
super().__init__(parent)
self._download = download
self._fcdoc = fcdoc
self._disp2bump = disp2bump
self._polyhaven_size = polyhaven_actual_size
_, filename = os.path.split(download.path())
self.setWindowTitle("Import from MaterialX Library")
self.setLabelText(f"Downloading '{filename}'...")
Expand Down Expand Up @@ -253,6 +250,39 @@ def finished_download(self):
)
_, filenameshort = os.path.split(self._download.path())
self.setLabelText(f"Importing '{filenameshort}'...")
self.do_import()

def do_import(self):
"""Do importation of downloaded file (callback).
To be overriden by specialized widgets.
"""


class MaterialXDownloadWindow(DownloadWindow):
"""A simple widget to handle MaterialX download and import from the web."""

def __init__(
self,
download,
fcdoc,
parent,
disp2bump=False,
polyhaven_actual_size=None,
):
super().__init__(download, fcdoc, parent)
self._disp2bump = disp2bump
self._polyhaven_size = polyhaven_actual_size

self.thread = None
self.worker = None

def do_import(self):
"""Do import of MaterialX downloaded file.
This function handles import. Import is executed in a separate thread
to avoid blocking UI.
"""
filename = self._download.path()

# Start import
Expand Down Expand Up @@ -341,6 +371,24 @@ def cancel(self):
self.importer.cancel()


class HdriDownloadWindow(DownloadWindow):
"""A simple widget to handle HDRI download and import from the web."""

def do_import(self):
"""Do import of HDRI downloaded file."""
filepath = self._download.path()
basename = os.path.basename(filepath)
_, fpo, _ = ImageLight.create(self._fcdoc)
fpo.Label = basename
fpo.ImageFile = filepath

# Finalize (success)
os.remove(filepath)
self.setLabelText("Done")
self.canceled.connect(self.cancel)
self.setCancelButtonText("Close")


def open_mxdownloader(url, doc, disp2bump=False):
"""Open a downloader."""
if not App.GuiUp:
Expand Down

0 comments on commit 735d316

Please sign in to comment.