| 1 |
"""Convert VRML files to X3D format, with optional HTML embedding and viewing. |
| 2 |
|
| 3 |
It includes a graphical interface using wxPython for file selection and |
| 4 |
conversion, as well as a command-line mode for batch processing. The module |
| 5 |
supports features like caching, forced downloads, and embedding viewer |
| 6 |
components in the output. |
| 7 |
""" |
| 8 |
|
| 9 |
import os |
| 10 |
import sys |
| 11 |
from functools import partial |
| 12 |
|
| 13 |
from DisplayCAL import config, x3dom |
| 14 |
from DisplayCAL import localization as lang |
| 15 |
from DisplayCAL.meta import NAME as APPNAME |
| 16 |
from DisplayCAL.util_os import launch_file, make_win32_compatible_long_path, waccess |
| 17 |
|
| 18 |
gui = "wx" in sys.modules |
| 19 |
|
| 20 |
if gui: |
| 21 |
from DisplayCAL.worker import Worker, show_result_dialog |
| 22 |
from DisplayCAL.wx_addons import wx |
| 23 |
from DisplayCAL.wx_fixes import GenBitmapButton as BitmapButton |
| 24 |
from DisplayCAL.wx_windows import BaseApp, BaseFrame, FileDrop |
| 25 |
|
| 26 |
class VRML2X3DFrame(BaseFrame): |
| 27 |
"""Frame for the VRML to X3D converter.""" |
| 28 |
|
| 29 |
def __init__(self, html, embed, view, force, cache): |
| 30 |
BaseFrame.__init__( |
| 31 |
self, |
| 32 |
None, |
| 33 |
wx.ID_ANY, |
| 34 |
lang.getstr("vrml_to_x3d_converter"), |
| 35 |
style=wx.DEFAULT_FRAME_STYLE & ~(wx.MAXIMIZE_BOX | wx.RESIZE_BORDER), |
| 36 |
name="vrml2x3dframe", |
| 37 |
) |
| 38 |
self.SetIcons( |
| 39 |
config.get_icon_bundle( |
| 40 |
[256, 48, 32, 16], f"{APPNAME}-VRML-to-X3D-converter" |
| 41 |
) |
| 42 |
) |
| 43 |
self.Bind(wx.EVT_CLOSE, self.OnClose) |
| 44 |
self.cache = cache |
| 45 |
self.embed = embed |
| 46 |
self.force = force |
| 47 |
self.html = html |
| 48 |
self.worker = Worker(self) |
| 49 |
sizer = wx.BoxSizer(wx.HORIZONTAL) |
| 50 |
self.SetSizer(sizer) |
| 51 |
panel = wx.Panel(self) |
| 52 |
sizer.Add(panel) |
| 53 |
panelsizer = wx.BoxSizer(wx.HORIZONTAL) |
| 54 |
panel.SetSizer(panelsizer) |
| 55 |
self.btn = BitmapButton( |
| 56 |
panel, |
| 57 |
wx.ID_ANY, |
| 58 |
config.get_icon(256, "3d-primitives"), |
| 59 |
style=wx.NO_BORDER, |
| 60 |
) |
| 61 |
self.btn.SetToolTipString(lang.getstr("file.select")) |
| 62 |
self.btn.Bind( |
| 63 |
wx.EVT_BUTTON, |
| 64 |
lambda event: vrmlfile2x3dfile( |
| 65 |
None, |
| 66 |
html=html, |
| 67 |
embed=embed, |
| 68 |
view=view, |
| 69 |
force=force, |
| 70 |
cache=cache, |
| 71 |
worker=self.worker, |
| 72 |
), |
| 73 |
) |
| 74 |
self.droptarget = FileDrop(self) |
| 75 |
vrml_drop_handler = partial( |
| 76 |
vrmlfile2x3dfile, |
| 77 |
html=html, |
| 78 |
embed=embed, |
| 79 |
view=view, |
| 80 |
force=force, |
| 81 |
cache=cache, |
| 82 |
worker=self.worker, |
| 83 |
) |
| 84 |
self.droptarget.drophandlers = { |
| 85 |
".vrml": vrml_drop_handler, |
| 86 |
".vrml.gz": vrml_drop_handler, |
| 87 |
".wrl": vrml_drop_handler, |
| 88 |
".wrl.gz": vrml_drop_handler, |
| 89 |
".wrz": vrml_drop_handler, |
| 90 |
} |
| 91 |
self.btn.SetDropTarget(self.droptarget) |
| 92 |
panelsizer.Add(self.btn, flag=wx.ALL, border=12) |
| 93 |
self.Fit() |
| 94 |
self.SetMinSize(self.GetSize()) |
| 95 |
self.SetMaxSize(self.GetSize()) |
| 96 |
|
| 97 |
def OnClose(self, event): |
| 98 |
"""Handle the close event for the frame. |
| 99 |
|
| 100 |
Args: |
| 101 |
event (wx.Event): The close event. |
| 102 |
""" |
| 103 |
# Hide first (looks nicer) |
| 104 |
self.Hide() |
| 105 |
# Need to use CallAfter to prevent hang under Windows if minimized |
| 106 |
wx.CallAfter(self.Destroy) |
| 107 |
|
| 108 |
def get_commands(self): |
| 109 |
"""Get the list of commands for the command line interface. |
| 110 |
|
| 111 |
Returns: |
| 112 |
list: A list of command strings that can be used in the command |
| 113 |
line. |
| 114 |
""" |
| 115 |
return [ |
| 116 |
*self.get_common_commands(), |
| 117 |
"VRML-to-X3D-converter [filename...]", |
| 118 |
"load <filename...>", |
| 119 |
] |
| 120 |
|
| 121 |
def process_data(self, data): |
| 122 |
"""Process data received from the command line or drag-and-drop. |
| 123 |
|
| 124 |
Args: |
| 125 |
data (tuple): A tuple containing the command and optional |
| 126 |
filename(s). |
| 127 |
|
| 128 |
Returns: |
| 129 |
str: "ok" if the command was processed successfully, "invalid" |
| 130 |
otherwise. |
| 131 |
""" |
| 132 |
if data[0] in ("VRML-to-X3D-converter", "load"): |
| 133 |
if self.IsIconized(): |
| 134 |
self.Restore() |
| 135 |
self.Raise() |
| 136 |
if len(data) > 1: |
| 137 |
self.droptarget.OnDropFiles(0, 0, data[1:]) |
| 138 |
return "ok" |
| 139 |
return "invalid" |
| 140 |
|
| 141 |
|
| 142 |
def main(): |
| 143 |
"""Main function to handle command line arguments and initiate conversion.""" |
| 144 |
if "--help" in sys.argv[1:] or (not sys.argv[1:] and not gui): |
| 145 |
print("Convert VRML file to X3D") |
| 146 |
print("Author: Florian Hoech, licensed under the GPL version 3") |
| 147 |
print(f"Usage: {os.path.basename(sys.argv[0])} [OPTION]... FILE...") |
| 148 |
print("The output is written to FILENAME.x3d(.html)") |
| 149 |
print("") |
| 150 |
print( |
| 151 |
" --embed Embed viewer components in HTML instead of referencing them" |
| 152 |
) |
| 153 |
print(" --force Force fresh download of viewer components") |
| 154 |
print( |
| 155 |
" --no-cache Don't use viewer components cache " |
| 156 |
"(only uses existing cache if" |
| 157 |
) |
| 158 |
print(" embedding components, can be overridden with --force)") |
| 159 |
if gui: |
| 160 |
print(" --no-gui Don't use GUI (console mode)") |
| 161 |
print(" --no-html Don't generate HTML file") |
| 162 |
print(" --view View the generated file (if no GUI)") |
| 163 |
if not gui: |
| 164 |
print(" --batch Don't pause after processing") |
| 165 |
print(" FILE Filename of VRML file to convert") |
| 166 |
if gui: |
| 167 |
return |
| 168 |
if gui: |
| 169 |
config.initcfg("VRML-to-X3D-converter") |
| 170 |
lang.init() |
| 171 |
lang.update_defaults() |
| 172 |
cache = "--no-cache" not in sys.argv[1:] |
| 173 |
embed = "--embed" in sys.argv |
| 174 |
force = "--force" in sys.argv |
| 175 |
html = "--no-html" not in sys.argv[1:] |
| 176 |
if not gui: |
| 177 |
result = None |
| 178 |
view = "--view" in sys.argv[1:] |
| 179 |
for arg in sys.argv[1:]: |
| 180 |
if not arg.startswith("--"): |
| 181 |
result = vrmlfile2x3dfile( |
| 182 |
str(arg), |
| 183 |
html=html, |
| 184 |
embed=embed, |
| 185 |
view=view, |
| 186 |
force=force, |
| 187 |
cache=cache, |
| 188 |
gui=gui, |
| 189 |
) |
| 190 |
if result is None: |
| 191 |
print("No filename given.") |
| 192 |
if ( |
| 193 |
sys.stdout |
| 194 |
and hasattr(sys.stdout, "isatty") |
| 195 |
and sys.stdout.isatty() |
| 196 |
and "--batch" not in sys.argv[1:] |
| 197 |
): |
| 198 |
input("Press RETURN to exit") |
| 199 |
sys.exit(int(not result)) |
| 200 |
else: |
| 201 |
view = "--no-view" not in sys.argv[1:] |
| 202 |
app = BaseApp(0) |
| 203 |
app.TopWindow = VRML2X3DFrame(html, embed, view, force, cache) |
| 204 |
if sys.platform == "darwin": |
| 205 |
app.TopWindow.init_menubar() |
| 206 |
wx.CallLater(1, _main, app) |
| 207 |
app.MainLoop() |
| 208 |
|
| 209 |
|
| 210 |
def _main(app): |
| 211 |
app.TopWindow.listen() |
| 212 |
app.process_argv() |
| 213 |
app.TopWindow.Show() |
| 214 |
|
| 215 |
|
| 216 |
def vrmlfile2x3dfile( |
| 217 |
vrmlpath=None, |
| 218 |
x3dpath=None, |
| 219 |
html=True, |
| 220 |
embed=False, |
| 221 |
view=False, |
| 222 |
force=False, |
| 223 |
cache=True, |
| 224 |
worker=None, |
| 225 |
gui=True, |
| 226 |
): |
| 227 |
"""Convert VRML to HTML. |
| 228 |
|
| 229 |
Output is written to <vrmlfilename>.x3d.html unless you set x3dpath to |
| 230 |
desired output path, or False to be prompted for an output path. |
| 231 |
|
| 232 |
Args: |
| 233 |
vrmlpath (str): Path to the VRML file to convert. If None, a file |
| 234 |
dialog will be shown to select the file. |
| 235 |
x3dpath (str): Path to the output X3D file. If None, it will be derived |
| 236 |
from vrmlpath. If False, a file dialog will be shown to select the |
| 237 |
output path. |
| 238 |
html (bool): Whether to generate an HTML file with the X3D viewer. |
| 239 |
embed (bool): Whether to embed the X3D viewer components in the HTML. |
| 240 |
view (bool): Whether to view the generated file after conversion. |
| 241 |
force (bool): Whether to force a fresh download of viewer components. |
| 242 |
cache (bool): Whether to use the viewer components cache. |
| 243 |
worker (Worker): Worker instance for asynchronous processing. |
| 244 |
gui (bool): Whether to use GUI for file selection. |
| 245 |
|
| 246 |
Returns: |
| 247 |
bool: True if the conversion was successful, False otherwise. |
| 248 |
None: If the user cancels the file selection dialog. |
| 249 |
""" |
| 250 |
while not vrmlpath or not os.path.isfile(vrmlpath): |
| 251 |
if not gui: |
| 252 |
if not vrmlpath or vrmlpath.startswith("--"): |
| 253 |
print("No filename given.") |
| 254 |
else: |
| 255 |
print(f"{vrmlpath!r} is not a file.") |
| 256 |
return False |
| 257 |
if not wx.GetApp(): |
| 258 |
_ = BaseApp(0) |
| 259 |
defaultDir, defaultFile = config.get_verified_path("last_vrml_path") |
| 260 |
dlg = wx.FileDialog( |
| 261 |
None, |
| 262 |
lang.getstr("file.select"), |
| 263 |
defaultDir=defaultDir, |
| 264 |
defaultFile=defaultFile, |
| 265 |
wildcard=lang.getstr("filetype.vrml") |
| 266 |
+ "|*.vrml;*.vrml.gz;*.wrl.gz;*.wrl;*.wrz", |
| 267 |
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST, |
| 268 |
) |
| 269 |
dlg.Center(wx.BOTH) |
| 270 |
result = dlg.ShowModal() |
| 271 |
vrmlpath = dlg.GetPath() |
| 272 |
dlg.Destroy() |
| 273 |
if result != wx.ID_OK: |
| 274 |
return None |
| 275 |
config.setcfg("last_vrml_path", vrmlpath) |
| 276 |
config.writecfg(module="VRML-to-X3D-converter", options=("last_vrml_path",)) |
| 277 |
filename, ext = os.path.splitext(vrmlpath) |
| 278 |
if x3dpath is None: |
| 279 |
x3dpath = f"{filename}.x3d" |
| 280 |
if x3dpath: |
| 281 |
dirname = os.path.dirname(x3dpath) |
| 282 |
while not x3dpath or not waccess(dirname, os.W_OK): |
| 283 |
if not gui: |
| 284 |
if not x3dpath: |
| 285 |
print("No HTML output filename given.") |
| 286 |
else: |
| 287 |
print(f"{dirname!r} is not writable.") |
| 288 |
return False |
| 289 |
if not wx.GetApp(): |
| 290 |
_ = BaseApp(0) |
| 291 |
if x3dpath: |
| 292 |
defaultDir, defaultFile = os.path.split(x3dpath) |
| 293 |
else: |
| 294 |
defaultFile = os.path.basename(filename) + ".x3d" |
| 295 |
dlg = wx.FileDialog( |
| 296 |
None, |
| 297 |
lang.getstr("error.access_denied.write", dirname), |
| 298 |
defaultDir=defaultDir, |
| 299 |
defaultFile=defaultFile, |
| 300 |
wildcard=lang.getstr("filetype.x3d") + "|*.x3d", |
| 301 |
style=wx.SAVE | wx.FD_OVERWRITE_PROMPT, |
| 302 |
) |
| 303 |
dlg.Center(wx.BOTH) |
| 304 |
result = dlg.ShowModal() |
| 305 |
dlg.Destroy() |
| 306 |
if result != wx.ID_OK: |
| 307 |
return None |
| 308 |
x3dpath = dlg.GetPath() |
| 309 |
dirname = os.path.dirname(x3dpath) |
| 310 |
vrmlpath, x3dpath = [str(path) for path in (vrmlpath, x3dpath)] |
| 311 |
if sys.platform == "win32": |
| 312 |
vrmlpath = make_win32_compatible_long_path(vrmlpath) |
| 313 |
x3dpath = make_win32_compatible_long_path(x3dpath) |
| 314 |
if html: |
| 315 |
finalpath = f"{x3dpath}.html" |
| 316 |
if sys.platform == "win32": |
| 317 |
finalpath = make_win32_compatible_long_path(finalpath) |
| 318 |
x3dpath = finalpath[:-5] |
| 319 |
else: |
| 320 |
finalpath = x3dpath |
| 321 |
if worker: |
| 322 |
worker.clear_cmd_output() |
| 323 |
worker.start( |
| 324 |
lambda result: ( |
| 325 |
show_result_dialog(result, wx.GetApp().GetTopWindow()) |
| 326 |
if isinstance(result, Exception) |
| 327 |
else result and view and launch_file(finalpath) |
| 328 |
), |
| 329 |
x3dom.vrmlfile2x3dfile, |
| 330 |
wargs=(vrmlpath, x3dpath, html, embed, force, cache, worker), |
| 331 |
progress_title=lang.getstr("vrml_to_x3d_converter"), |
| 332 |
progress_start=1, |
| 333 |
resume=worker.progress_wnd and worker.progress_wnd.IsShownOnScreen(), |
| 334 |
fancy=False, |
| 335 |
) |
| 336 |
else: |
| 337 |
result = x3dom.vrmlfile2x3dfile( |
| 338 |
vrmlpath, x3dpath, html, embed, force, cache, None |
| 339 |
) |
| 340 |
if not isinstance(result, Exception) and result: |
| 341 |
if view: |
| 342 |
launch_file(finalpath) |
| 343 |
else: |
| 344 |
return False |
| 345 |
return True |
| 346 |
|
| 347 |
|
| 348 |
if __name__ == "__main__": |
| 349 |
main() |