Using Python in VRED

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.

HTML5

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.

The Python Command

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 )

How to Load the VRED File

Open VRED and select File > Open, then browse C:\ProgramData\Autodesk\VREDPro-<internalVersion>\examples\script, select fullscreenmenu, and click Open.

How to Add Code to index_html

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.

  1. Open a text editor (I used Notepad for this), then open index_html (C:\ProgramData\Autodesk\VREDPro-<internalVersion>\examples\script\).

    Note:

    If you don't see any files, change the type of file from Text Documents (*.txt) to All Files.

  2. Add this line just above ``````:

    
    <button class="button" onclick="createCone()">Cone</button><br>
    
  3. Save your changes.

How to Add the Cone Code to script.js

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.

  1. In the text editor, open script.js (C:\ProgramData\Autodesk\VREDPro-<internalVersion>\examples\script\).

    Note:

    If you don't see any files, change the type of file from Text Documents (*.txt) to All Files.

  2. Scroll to the bottom and place your cursor after };

  3. Press your Enter key 3 time to add empty lines after it.

  4. 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 );");
    
    };
    
  5. Set following values:

    • For height = 1000
    • For radius = 150
    • For sides = 30
    • For create_side, since it is a type bool, it needs a true or false = true
    • For create_bottom, since it is a type bool, it needs a true or false = false
    • For red = 0.5
    • For green = 0.3
    • For blue = 0.4

    Your final code will look like this:

    
    function createCone() {
    
    sendPython("createCone(1000, 150, 30, true, false, 0.5, 0.3, 0.4);");
    
    };
    
  6. Save your changes and close the editor.

How to Enable the Web Server

When using HTML5 and Python, you need to enable the Web Server to see your work.

  1. Select Edit > Preferences > General Settings > Web Interface.
  2. Check Enable Web Server. A dialog appears.
  3. Click Enable.
  4. Click Apply and OK.

How to Reload the HTML5 Changes

Since changes were made to the HTML5 file, you will need to reload it into the WebEngine.

  1. Select Scene > Media Editor to open the Media Editor.
  2. Select WebEngine.
  3. In the parameters to the right, click Reload Disabled (Redo the Web Page). The new button should appear in your scene.
  4. Close the editor.

How to Test Things

If you want to test the function to see if it works, in VRED, there are two ways: