Community
VRED Forum
Welcome to Autodesk’s VRED Forums. Share your knowledge, ask questions, and explore popular VRED topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Import python APIv2 modules

19 REPLIES 19
SOLVED
Reply
Message 1 of 20
f.grz
1286 Views, 19 Replies

Import python APIv2 modules

Hey guys!

 

I developed some working code within VRED, using API v1 and v2. Now i want to place it somewhere on a central place, and the users just import it from there, which makes maintaining the code very easy.

I instantly have the following problem: the code imported by a "from importtest import" doesn't know any of the VRED API commands. 

 

For the APIv1 commands i could fix it by importing a certain module, like for findNode() i have to use "from vrScenegraph import *in my module (which is not undocumented but working).

 

But for the APIv2 there are no modules like "vrd*Service" which i can import, so i can't use all of these vrd*Service modules to find nodes, make annotations and so on.

 

Is there a nice way to get rid of all of this?

Thank you in advance!

19 REPLIES 19
Message 2 of 20
chr33z
in reply to: f.grz

Hi,

 

which version are you working on? In versions older than 2021.3 (or 2021.2?) you have to import the APIv2 namespaces explicitly, but this is a bit tricky.

 

All API v2 modules and class are in the namespace "vrKernelServices" so a valid import statement would look like

 

from vrKernelServices import vrCameraService

 

In version 2021.3 the namespaces are imported automatically when VRED loads them so you don't have to import anything for APIv2.

 

Cheers!

Message 3 of 20
f.grz
in reply to: f.grz

Hey,

 

thank you, i got it working like that and we will switch to V2021.3 in the next time. As expected, we were using 2021.2.

 

All the best!

Message 4 of 20
Anonymous
in reply to: chr33z

Hi guys,
I am actually using VRED 2021.3.
I need to import the API2.0 module "vrWebEngineService" into a class script.
I followed the instructions of chr33z:
from vrKernelServices import vrWebEngineService
but I don't get the desired result:
when I call the function vrWebEngineService.getWebEngine(name) [where name must be a string, as per documentation], in VRED I get the error: "TypeError: descriptor 'getWebEngine' requires a 'vrKernelServices.vrWebEngineService' but received a 'str' ".
Can you help me please?

Message 5 of 20
f.grz
in reply to: Anonymous

Hey carser,

 

so your imports work fine i think - Autodesk support has to help you here...

 

Best,

Florian

Message 6 of 20
Anonymous
in reply to: f.grz

Thanks for the reply. The weird thing is that inside VRED, if I write the following script:
from vrKernelServices import *
myWebEngine = vrWebEngineService.createWebEngine ("newWebEngine")
i get the same error:
'TypeError: descriptor' createWebEngine 'requires a' vrKernelServices.vrWebEngineService 'object but received a' str.

 

How can I get help drectly from Autodesk support?

Message 7 of 20

Hi, i think that for API V2 you have not import, just use it in your script.

i tried directly in the terminal and in the script editor and it worked for me

 

Immagine 2021-03-10 094046.png

 

Could you try?

 

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 8 of 20
Anonymous
in reply to: Christian_Garimberti

Ciao c.garimberti,

 

Yes, that way it works for me too.
The problem is that I am developing a VRED plugin; in this plugin I added a class for creating and managing webEngine.
To use the VRED API, in version-1, I used for example:
import vrScenegraph
vrScengraph.findNode ("NodeName")
e tutto è ok.

Now, with the version-2 API, to use the vrWebEngineService module in my class, I write:
from vrKernelServices import vrWebEngineService
myWebEngine = vrWebEngineService.createWebEngine ("newWebEngine")
then i compile, after which in VRED I get the "TypeError" indicated above.

Message 9 of 20
chr33z
in reply to: f.grz

In VRED there are two ways to use python scripts:

1. Use python code by writing it directly in VRED (script editor, variant sets, terminal, ...)

2. Use python scripts in Script Plugins or in custom modules

 

(1.)

---------------

When using python directly in VRED, you do not have to import any VRED specific modules. They are already injected in the python namespace and can be used directly.

 

(2.)

---------------

When writing Script Plugins or custom modules you want to use in VRED, we have to further differentiate:

 

Optimal case: You are on version 2021.3 or above

- You have to import all API v1 modules.

- You can use VRED API v2 modules without explicitly importing them in the script. VRED will do that for you.

 

Less optimal case: You are on version 2021.2 or below

- You have to import all API v1 modules.

- You may have to import API v2 modules. This was a bug with their API v2 and I had inconsistent results between versions 2021.1 and 2021.2.

 

Unfortunately, when you wrote a module for 2021.1 or 2021.2 with explicit imports for API v2 modules, it will break these scripts when using them in 2021.3. Somehow the imports overwrite the internal VRED python namespaces.

Message 10 of 20
Anonymous
in reply to: chr33z

Ok, thanks: everything is known and clear.
But if in my plugin class I directly use an API-v2 module without importing the appropriate space name, for example:
myWebEngine = vrWebEngineService.createWebEngine ("newWebEngine")
I get a class compile error:
"undeclared name not builtin: vrWebEngineService

Message 11 of 20
chr33z
in reply to: f.grz

Can you tell me, which code editor you are using and how use your plugin in VRED?

Message 12 of 20
Anonymous
in reply to: chr33z

I use VisualStudioCode to write the software.
Then, to encrypt the code, I compile it using Cython.
Large plugin development.
I get a main.py and many .pyd modules for each of my classes.
Everything has always worked perfectly with the API-v1; now with the API-v2 i have these problems.

Message 13 of 20
chr33z
in reply to: f.grz

OK, I never tried this workflow with compiling it in cython.

 

Is the error thrown in Cython or in VRED? Have you tried using API v2 modules without compiling them?

Message 14 of 20
Anonymous
in reply to: chr33z

I have implemented a complex but very interesting pipeline.
I confirm: in version 2021.3, if I use the API-v2 modules directly in VRED, everything works. But if I have to use these modules in my plugin class, there is no way that I know.

Message 15 of 20

i didn't compile my script, but i think it is something linked to that operation.

if i use directly in a script

myWebEngine = vrWebEngineService.createWebEngine ("newWebEngine")
print(myWebEngine)

it works.

Immagine 2021-03-10 101704.png

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 16 of 20
Anonymous
in reply to: Christian_Garimberti

This is very simple and i already confirmed that it works.
Try making a main.py script that instantiates a class, in which you run that script of yours where you create the webEngine ... (...Object Oriented Python...)

Message 17 of 20
chr33z
in reply to: f.grz

I never worked with compiled python files in VRED. Do you just copy the compiled files in a ScriptPlugin directory and use it like a regular python file?

 

Maybe compiled files are treated differently than uncompiled files by VRED.

Message 18 of 20
Anonymous
in reply to: chr33z

Yes, I do.
The compiled files have no differences: they are just not readable by third parties.
However, using object oriented python, it is necessary to properly import the modules belonging to external namespaces (such as those of VRED).
I have to find the solution to properly use the API-v2 modules...

Message 19 of 20
chr33z
in reply to: Anonymous

I'm out of ideas here. But I would really like to know whats the problem.
You should maybe open a separate question, so people can directly answer to you specific problem.

 

Edit:

I did not find anything special when working with object oriented python classes in script plugins.

Message 20 of 20
Anonymous
in reply to: chr33z

ok. Thank you very much for your time and for your availability to support.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report