Python in VRED

 

Where to find the API

In Vred to find the API go to your menu bar > Help > Python Documentation. A web page will pop up with all the functions and modules that VRED has given us.

Modules, FUnctions and Parameters 

You can think of modules as a group of functions. Functions you can think of as a tool. So lets say I want to write a code that has to do with variant sets. Their is a couple different Modules that have tools or "functions" that have to do with Variant sets but right now all I want to do is activate a variant set.

I know that this is in the module vrVariants. When I click on the module it opens up the page with the different functions. Here you see their is a funtion called selectVariantSet. This is the tool I am looking for. When I run this code in VRED what I am telling VRED is, I want to select a variant set I have made. Well VRED needs to know which one it is so you have to add the parameters.

If you look at the function you can see that it has this parameters section. This is telling you what option you have for the tool. In this case it ask for a name and if I want to exclude a viewpoint. If you see under them they tell you what type they are. The name is a string. A string is basically just a keyboard input. To input a string you need to place it inside quotes. So lets say you have variant set called Light, all this variant does is toggle a light in your scene. Then the python code for this is:

selectVariantSet('Light')

So where do you put this 

Script_Editor.PNG

Running a Script in Python

Their is two main places to place a script inside of VRED. One is the Script Editor, this can be found in the menu bar Edit > Script Editor. The other is the terminal located in the lower center of the user interface. I place my finished scripts in the script editor and save that file. That way when you open it, the script runs automatically. When I am just testing a code I will just copy and paste it in the terminal so I can see if it works. 

Learning through Examples

If you go to your main menu bar. Go to File > Open Examples, and their is a folder that has a lot of python scripts you can already use. A lot of the time they will have explanations on what is going on. When I was learning python I read all of these and tried to reverse engineer them. It really helped wrap my head around python. Their is some old examples that don't work but their is a lot of useful information there. 

Lets Get Started with a very simple Example

This ends the intro to python. The next page we will write a very simple python script that will give us the ability to hold and turn on a flash light in VR.