Though VRED has all sorts of tools for working with your files, there is so much more you can do once you start scripting in Python. Customize VRED to your liking using logic written in Python. Make something work differently or add custom operations, menus, or options with the help of Python.
There are an assortment of Python examples provided with VRED to help you understand how Python works. In the Menu Bar, select Help > Python Documentation to access the Python documentation.
In 2019, we added HTML 5 integration. Using HTML5, you can create overlays and more, then use Python to talk with VRED and execute the action.
Use the Python docs (Help > Python Documentation) to find the Python command you want, such as a create, edit, or toggle command. Let's start with a small tutorial to walk you through an example of how you might use HTML5 with Python in VRED.
In this example, we will add code to HTML5 and Java Script files to add a button to a pre-existing interface that will create a cone when clicked. The changes we will do will affect the Plane node, found under the Perspective node. The files needed for this can be found here. Unzip and save the files to C:\ProgramData\Autodesk\VREDPro-<internalVersion>\examples\scripts
.
We will get the Python command createCone
from here: Help > Python Documentation. Search the list along the left for createCone
. Click the createCone
link for an explanation of the command and information about its parameters.
createCone ( height , radius , sides , create\_side , create\_bottom , red , green , blue )
Open VRED and select File > Open, then browse C:\ProgramData\Autodesk\VREDPro-<internalVersion>\examples\script
, select fullscreenmenu
, and click Open.
We want to add a button, labeled Cone, to the existing HTML5 code. When you click this button, it will create a cone. All the information about the cone, such as its size, color, and placement will all be defined in your script.js file.
Open a text editor (I used Notepad for this), then open index_html (C:\ProgramData\Autodesk\VREDPro-<internalVersion>\examples\script\)
.
If you don't see any files, change the type of file from Text Documents (*.txt) to All Files.
Add this line just above ``````:
<button class="button" onclick="createCone()">Cone</button><br>
Save your changes.
In the Python documentation, we are given this: createCone ( height , radius , sides , create\_side , create\_bottom , red , green , blue )
. We need to enter values for each of these parameters.
In the text editor, open script.js (C:\ProgramData\Autodesk\VREDPro-<internalVersion>\examples\script\)
.
If you don't see any files, change the type of file from Text Documents (*.txt) to All Files.
Scroll to the bottom and place your cursor after };
Press your Enter key 3 time to add empty lines after it.
Paste the following Python code from the Python documentation at the bottom of the file.
function createCone() {
sendPython("createCone( height , radius , sides , create\_side , create\_bottom , red , green , blue );");
};
Set following values:
Your final code will look like this:
function createCone() {
sendPython("createCone(1000, 150, 30, true, false, 0.5, 0.3, 0.4);");
};
Save your changes and close the editor.
When using HTML5 and Python, you need to enable the Web Server to see your work.
Since changes were made to the HTML5 file, you will need to reload it into the WebEngine.
If you want to test the function to see if it works, in VRED, there are two ways:
Open the Scenegraph, then at the bottom of the window, click (Terminal) to open it. In the root: field, enter:
createCone(1000,150,30,true,false,0.5,0.3,0.4);
A Geometry node should appear in the Scenegraph. This lets you know it worked. Select the node and press your Delete key to remove it.
In VRED, click the new Cone button. If it works, in the Scenegraph, a new Geometry node appears.