Namespaces and Autocomplete
 
 
 

Namespaces

In Python, all variables, functions and module names are in either the local namespace (restricted to a function) or in the global namespace. To see what names are available in the current namespace, type dir() in the interactive console.

The editor stores the Python global namespace. That is, all names that are available. When you execute a script, names in your script replace those in the global namespace. For example: in the interactive console define the following string.

s = "mystring"

Next, create a new script and execute it (any script will do, even an empty one). After this script executes, if you try to access your variable, the name is now undefined.

Traceback (most recent call last):File "<MotionBuilder>", line 1, in <module>NameError: name 's' is not defined

The names available in the editor and the Python global namespace are always the names defined in the last script executed plus all names defined after executing a script. This affects the Python object system and the autocomplete feature.

Autocomplete

In the interactive console, access auto complete with Ctrl+Space.

Auto completion uses the Python global namespace to make a suggestion. That means, any name that can be found by typing dir() in the interactive console can be auto completed or examined to suggest completion for its member. However, how the auto complete list is populated depends on the state of the interactive console:

From a name contained in the global Python namespace, you can also use auto completion to know which functions and properties are available for an object. For example:

  1. In the interactive console, type from pyfbsdk import *.
  2. Type FB, then press Ctrl+Space. Note that suggestions are case-sensitive.
  3. Choose one of the suggestions, and type a period after it in the interactive console.
  4. Press Ctrl+Space again to pop up a section of completions relevant to that member.

For example, entering FBEdit.On and pressing Ctrl+Space pops up a list of available events for FBEdit, since all property events begin with "On".

Autocompletion also works in the work area, in the same way as in the interactive console. It always checks the Python global namespace. To get the most out of the autocompletion feature, execute the script often, as in the following example.

  1. Create a new script, and in the first two lines add:
    from pyfbsdk import *
    from pyfbsdk_additions import *
    Execute the script. Although it doesn’t do anything yet, executing it adds the SDK names to the global namespace, and they can then be used in autocompletion.
  2. On the next line: type tool = FBCreate. Press Ctrl+Space and choose FBCreateUniqueTool(. Complete it as tool = FBCreateUniqueTool("tool example").
  3. Execute the script. This adds the tool variable to the global namespace.
  4. On the next line type tool. then Ctrl+Space to see all the functions and properties available in the object named tool. This is the same output you would get from the command print str(dir(tool)).
  5. Choose StartSizeX, and complete with the following lines:
    tool.StartSizeX = 800
    tool.StartSizeY = 200
    ShowTool(tool)
  6. Execute the script to display the tool.

It is good practice to execute a script each time you add new variables, and use autocompletion. If autocompletion does not work, either the name you are trying to complete is not in the global namespace, or you have a typing error.

External Custom Python Libraries

When setting up external custom Python libraries for use with MotionBuilder, if the application is having trouble finding the external Python libraries, ensure the environment variables contain a path to the appropriate directory, and that this path ends with a forward slash "/".