How can I open the Import File dialog from within a script

How can I open the Import File dialog from within a script

steve_k2400
Participant Participant
919 Views
14 Replies
Message 1 of 15

How can I open the Import File dialog from within a script

steve_k2400
Participant
Participant

Hi!

 

Is there a way to open the import dialog window from within a script?

vrGUIService.openImportDialog(file)

needs a file but I don't have a file yet.

I would like to import Alias Files (I don't know which ones at script start) from a script and I don't wan't to construct the import window myself.

Can someone show a sample script how to do this? Thanks.

 

0 Likes
Accepted solutions (1)
920 Views
14 Replies
Replies (14)
Message 2 of 15

__daniel.lincoln__
Autodesk
Autodesk
Hello,

This will open the import dialog window...
vrGUIService.openImportDialog(['C:/TEMP/*.wire'], vrScenegraphService.getRootNode())
Message 3 of 15

steve_k2400
Participant
Participant

Thank you!
As you might guess, I'm new to VRED Scripts. Sorry if I ask dumb questions.

I have two problems if I try to execute this:

NameError: name 'vrScenegraphService' is not defined. Do I have to import a module for this?

- If I execute only vrGUIService.openImportDialog(['C:/TEMP/*.wire']), I get the import dialog, but without the file selection Windows window.

Would it be possible to emulate the same behaviour as if I would click on Import in the main VRED interface?

(First the the file selection and then the import dialog.)

0 Likes
Message 4 of 15

Christian_Garimberti
Advisor
Advisor

Hi, you can add:

QFileDialog.getOpenFileName(None, "Select File", "<your file path>", "*.wire") for a single file selection

or

QFileDialog.getOpenFileNames(None, "Select Files", "<your file path>", "*.wire") for a multiple selection.

the use the @__daniel.lincoln__  code passing the selected file.

selectedFile = QFileDialog.getOpenFileName(None, "Select File", "<your file path>", "*.wire")
vrGUIService.openImportDialog(selectedFile, vrScenegraphService.getRootNode())

# or 

selectedFiles = QFileDialog.getOpenFileNames(None, "Select Files", "<your file path>", "*.wire")
vrGUIService.openImportDialog(selectedFiles, vrScenegraphService.getRootNode())

 

if use them in a scriptplugin you have to import both QFileDialog and vrScenegraphService with

import vrScenegraphService

from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtWidgets import QFileDialog

 

Check  the VRED python help for more information, and the QT Help for the dialog

https://doc.qt.io/qt-6/qfiledialog.html

 

i didn't test the code provided.... so check it when you use it

 

Best

Chris

Christian Garimberti
Technical Manager and Visualization Enthusiast
Qs Informatica S.r.l. | Qs Infor S.r.l. | My Website
Facebook | Instagram | Youtube | LinkedIn

EESignature

Message 5 of 15

steve_k2400
Participant
Participant

Hi and thank you! 

We are getting closer. 😊

Importing the Qt modules works and so the File Selector Window shows up.

Importing vrScenegraphService throws an error:  ModuleNotFoundError: No module named 'vrScenegraphService'

Without importing it, it can't find it: NameError: name 'vrScenegraphService' is not defined

 

If I leave out vrScenegraphService it kind of works but there is another Problem:

In the Import dialog there are now two entries. The previously selected file and "*.wire"

*.wire can't be imported so either the import stops with an error or *.wire has to be manually removed first which is not very satisfying in a script.

 

I'm experimenting in the Script window currently. It's not yet a plugin.

Thanks for any help!

 

0 Likes
Message 6 of 15

Christian_Garimberti
Advisor
Advisor

Which Vred version are you using? vrScenegraphService if i think is only in the 2023.3. If older Try to use vrScenegraph without importing it, while you are working in the script editor.

for the file double entry, could you share the code you wrote?

best

chris

Christian Garimberti
Technical Manager and Visualization Enthusiast
Qs Informatica S.r.l. | Qs Infor S.r.l. | My Website
Facebook | Instagram | Youtube | LinkedIn

EESignature

0 Likes
Message 7 of 15

steve_k2400
Participant
Participant

I'm not in the company today so I  can only look tomorrow but I think we have V2022.

I will test your suggestion tomorrow and get back to you.

Thanks and have a nice Sunday,

Cheers,

Steve

 

0 Likes
Message 8 of 15

steve_k2400
Participant
Participant

We have VRED 2022.2

I could solve the issue with the double entry by replacing "*.wire" with an empty "")

This way only the selected file is shown in the Import Dialog.

(With the drawback that it shows all files not just wires, but I can live with that.)

 

In this older VRED version the openImportDialog seems to require only one argument. 

If I try to put vrScenegraph.getRootNode() as second argument it complains:

TypeError: openImportDialog() takes exactly one argument (2 given)

 

Without the getRootNode this code seems to work.

from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtWidgets import QFileDialog

selectedFile = QFileDialog.getOpenFileName(None, "Select File", "C:\", "")
vrGUIService.openImportDialog(selectedFile)

 

0 Likes
Message 9 of 15

Christian_Garimberti
Advisor
Advisor
Accepted solution

Hi, be careful with the "\". in python \ is a special char, so you have to double it if you want to use it or just use the "/".

but this is not your fault

Checking what return from QFileDialog.getOpenFileName and QFileDialog.getOpenFileNames, i saw that you have do differentiate your script

from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtWidgets import QFileDialog

selectedFile = QFileDialog.getOpenFileName(None, "Select File", "C:\\", "*.wire")
print(selectedFile[0])
vrGUIService.openImportDialog([selectedFile[0]])

# OR
selectedFiles = QFileDialog.getOpenFileNames(None, "Select File", "C:\\", "*.wire")
print(selectedFiles[0])
vrGUIService.openImportDialog(selectedFiles[0])

For the openImportDialog you are right. in the 2022 you have to pass only the file selection

 

Best

Chris

Christian Garimberti
Technical Manager and Visualization Enthusiast
Qs Informatica S.r.l. | Qs Infor S.r.l. | My Website
Facebook | Instagram | Youtube | LinkedIn

EESignature

0 Likes
Message 10 of 15

steve_k2400
Participant
Participant

I see now! Thank you, I really appreciate it!

I didn't realise that it could be an array.

Now it works as intended. Fantastic, thanks! 👍

Message 11 of 15

steve_k2400
Participant
Participant

Now that it works, I see another problem. 

Is there a way to stop the script execution until the import is completely finished.

Currently my script continues as soon as the ImportDialog is displayed but it should wait until the import is finished and process the imported data. 

I guess for a script it gets now too complicated...

 

0 Likes
Message 12 of 15

Christian_Garimberti
Advisor
Advisor

Oh, that could be a very different approach...

Try to explain all that you want to do with your script, just to have a better view of all the process.

You can start from

 vrFileIO.load(filename, filenames, parent, newFile, showImportOptions) instead of vrGUIService.openImportDialog

See the Api V1 help for more info

Then you can go on with your code.

 

Best

Chris

 

 

 

Christian Garimberti
Technical Manager and Visualization Enthusiast
Qs Informatica S.r.l. | Qs Infor S.r.l. | My Website
Facebook | Instagram | Youtube | LinkedIn

EESignature

0 Likes
Message 13 of 15

steve_k2400
Participant
Participant

It's actually a very simple little Script which is already working.

Import an Alias file into a VRED template file and then comes the script: rename all of the Alias Material names according to a translation table (out of a text file) and apply a material asset over the renamed materials.

It works fine. The only problem is, that the script doesn't know which geometry belongs to the template file (no change on those materials) and which to the Alias file.

In the current form, I have to assume that all geometry in the template file have VRED materials which are in the translation table and so I can find and safely ignore them. If the template file creator doesn't follow the rules, then the assumption no longer works...

I wrote an Alias data visualization Add-On for Blender (via FBX) where I load the FBX files from within the Add-On (a bit more complex Python script) and this way I know exactly what came out of the Alias file.

I wanted to do quickly something similar here but my knowledge of the VRED API is far away from what I know of the Blender API. 

I assumed that I can call a comfortable file browser the same way but I didn't expected a parallel execution.

I don't have the time to work myself into the the VRED API, so if it's too complicated, I will give up on it. 

0 Likes
Message 14 of 15

Christian_Garimberti
Advisor
Advisor

Hi, the Import phase in Vred is via parallel thread. i don't remember if the new api i wrote (vrFileIO.Load) is synchronous or not. You have to try.

For the imported data to manage with a new Vred is easier. The new parameter that @__daniel.lincoln__  put (the parent node) is useful to manage that.just create a new node an import your data inside it.

Also the vrFileIO.load(filename, filenames, parent, newFile, showImportOptions) has the Parent parameter in the 2023. You could check in the 2022 api help.

Then manage all that is inside that node

Or you can try to manage the references. While importing the Wire file it should maintain the reference with the original file. So once imported, scan the the tree to find the node with the reference to that file.

And then work on that tree.

from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtWidgets import QFileDialog

selectedFile = QFileDialog.getOpenFileName(None, "Select File", "C:\\", "*.iam")
print(selectedFile[0])
# vrGUIService.openImportDialog([selectedFile[0]])
importedParent = findNode("Imported Data")
imported = vrFileIO.load([selectedFile[0]], importedParent, False, False)
print(imported)
print(importedParent.getNChildren())

With the showImportOptions False the execution is synchronous. if you have to put it to True, just add a Loop to wait until the importParent node has some child. it should work.

 

I don't think it will be too difficult. just take some time to read the Api docs.

 

Best

Chris

Christian Garimberti
Technical Manager and Visualization Enthusiast
Qs Informatica S.r.l. | Qs Infor S.r.l. | My Website
Facebook | Instagram | Youtube | LinkedIn

EESignature

Message 15 of 15

steve_k2400
Participant
Participant

Thank you! I will try to find some time to read a bit more of the API and then test "around".

I think at the end I will probably postpone it until we also get VRED 2023 as it doesn't really makes sense to invest a lot of time in a 2022 script which might not work under 2023.

Thanks anyway for your time and help. I really appreciate it! 👍