Archive for September, 2011

fun with python modules

New scripters I speak with don’t often know how to easily load code from external folders / modules in Python scripts. Today’s post will give you a few pointers in both Cinema 4D and Maya.

Setting Your Script Paths

Python’s import command should be no stranger to you if this topic interests you– it was probably the second thing most people learn in Python, after writing “Hello, World!” to the console. Import allows you to bring in code from libraries external to the source file in which you’re currently working. For example:

# commonly seen in Cinema 4D
import c4d
from c4d import *

# commonly seen in Maya
import maya
from maya import cmds as mc
import pymel.all as pm

These statements bring whatever module you specify into the current namespace, or bring in functions, classes, and variables. But where are the libraries that the import method references? Where do they live on disk?

The easiest way to find out is to list Python’s sys.path:

import sys

In my case, in Maya, it outputs the following:




Looking in the folders that are listed, you may find any number of Python source files and Python object files (*.pyc). sys.path is great because it’s just a regular Python list, which means it’s editable. You can append any number of paths that you like to it, and when import is called it will look through each and every path for the name specified.

Let’s try this in Cinema 4D. First, make a folder somewhere on your machine. I’m going to use /Users/Carney/Desktop/scripts for this example (I’m on a Mac.) In that folder, create a new file called and add the following text:

# simple utility functions

def ut_log(*args):
    msg =''
    for arg in args:
        msg += ' ' + str(arg)

Now back in Cinema 4D, open the Console and the Script Manager from the Scripts menu. In the Script Manager, paste the following and hit Execute, replacing the path with the folder you chose above:

import sys

import util
util.ut_log('Fun', 'Bunnies', 'are', 'fun.')

If all’s well, you should get a single sentence printed to the console output. If you made a mistake, you may get an error about not being able to import the util file. Check your path and try again.

With this setup, you can write scripts in your application that reference lengthy libraries of external code or other utility functions that you’ve created. At the moment I’m organizing code into single files with multiple functions for shorter utilities. Any function that’s more complex gets it’s own file and a class to contain it. This leads to funny lines like the following:

import module_generic
mod = module_generic.GenericModule()

But the organization and the ability to quickly update code without having to sift through a monolithic file inside the C4D Script Manager is worth the headache.

Automatically Adding Python Search Paths in Maya

The only thing to remember about this setup is that you have to make sure you add that folder you created to sys.path every time your application starts. In Maya this is a simple and well-known trick, but I had to do some digging to find out the same for Cinema 4D.

In both applications there’s a special Python file that gets loaded every time the Python VM restarts. In Maya, the file is called

Automatically Adding Search Paths in Maya

and it must live in one of three places:

  • ~/Library/Preferences/Autodesk/maya/2012-x64/prefs/scripts
  • ~/Library/Preferences/Autodesk/maya/2012-x64/scripts
  • ~/Library/Preferences/Autodesk/maya/scripts

You may notice that these are listed in the above printout of sys.path. When Maya starts it will execute files it finds. If you put the file in a folder that has the version of Maya specified (for example, in maya/2012-x64/scripts), then only that version will load the userSetup. If you place it in the general scripts folder — the last folder listed above — then it will be loaded by all Maya versions that support Python.

You can also use the MEL command internalVar -usd to find a folder that will work.

The userSetup file can execute any code you like when Maya loads, allowing you to have custom menus auto-created or to modify your sys.path variable so that you can access your code at any time. Try this: save a file into , and make sure it contains the following (after modifying the path to point to where you saved your file):

import sys

Now in Maya, open the script editor and copy, paste, and run the following:

import util
util.ut_log('Fun', 'Bunnies', 'are', 'funner.')

If all worked, you’ll see a line printed from your file letting you know that the custom setup succeeded on Maya startup, and you’ll see the proper output from this file.

Automatically Adding Python Search Paths in Cinema 4D

The way to do the above in Cinema 4D seems to be less well-known: you have to find the folder that is appropriate for your particular version of Cinema 4D and copy in a file called

The easiest way to find this folder is to go to the Scripts menu and choose User Scripts > Script Folder. This is the folder where scripts you’ve edited and saved through the Script Manager live. Now, go up one folder to Library, then go up again. This is the main folder for your version of Cinema 4D; it may have an oddly long name, but it contains your prefs and libraries. The file belongs in prefs/python. As an example, my folder is:

  • /Users/Carney/Library/MAXON/Cinema 4D R13/prefs/python/

Final Notes

This is just the start of organizing your code. If you want to know more, I recommend reading the official documentation on Python modules.

Tags: , , ,

maya input mapping for blender

A friend and fellow Blenderhead, Rob Garlington, spent a great deal of time working on an input remapping for Blender 2.5. What’s neat about his solution is that he’s also gone in and remapped the mouse inputs with the keyboard, so users coming over from Maya will really have a much easier time selecting objects or components and getting around the scene in general.

He doesn’t have a website set up yet, so I’ve offered to host his file here. It requires and has been updated for Blender 2.59.

After the download link, I’ve posted notes on usage from his email.

Download: Maya Input Remapping for Blender 2.59 (724kb)


The easiest way to install is to open this startup file in Blender 2.59, then go to ‘file\user preferences’ go to the bottom of the preferences screen and click the ‘save as default’ at the bottom. And that’s it, all the keys have been loaded into blender so that when you open any new scene it will use my key setup. If you ever want to go back to blender default simply click ‘file\load factory settings’.


  • One thing to note despite my many changes to animation there was no option to deselect keys by simply clicking on blank space in the f-curve editor. So just press ‘A’ to deselect everything that way.
  • Most default blender settings that have been changed to accommodate this new setup usually require a ‘shift’ to work now. For instance to extrude a face press ‘shift e’ to work now.
  • Animation Changes:

  • ‘left click drag’ to marquee select keys
  • ‘middle mouse button’ moves the keys
  • ‘right click mouse’ will scrub the dope sheet and f-curve editor.
  • ‘s’ set to insert keyframe
  • ‘ctrl + alt + left mouse’ button for border zoom
  • ‘f’ will view all
  • ‘alt + right mouse drag’ for view zooming
  • ‘alt + middle mouse’ to move all 2d screens
  • ‘k + left mouse’ to scrub timeline in 2d screens
  • ‘, .’ goes to next or previous keyframe
  • ‘alt , or .’ goes to next or previous frame
  • 3D View Changes:

  • ‘left click drag’ select
  • ‘middle mouse button’ moves everything
  • ‘f’ will view selected
  • ‘left click’ outside of the object, deselects in object, edit mesh and pose mode
  • ‘spacebar’ brings up the dynamic spacebar menu
  • ‘right click’ (in edit mesh mode) brings up the mesh selection of faces/verts/edges
  • ‘4,5,6’ are mapped to the corresponding shading modes
  • ‘ctrl d ‘ duplicates objects/faces and everything else in Blender that can be duplicated
  • ‘g’ for repeat action
  • ‘shift + LMB’ will add more objects/verts/faces/edges to your selection
  • ‘ctrl + LMB’ will deselect objects/verts/faces/edges
  • ‘shift right click’ for 3d cursor placement
  • ‘shift space’ for search
  • ‘z’ also does an undo function in addition to the standard ‘ctrl z’
  • ‘ctrl + right click’ will enable lasso mode.
  • ‘alt + s and r’ go into scale and rotation mode
  • ‘ctrl + alt + s,r or g’ have been changed to clear the scale rotation or translation on any given object.
  • ‘y’ will bring up a special material selection toolbox.
  • Timeline:

  • ‘left click drag’ to scrub the main timeline
  • Things that remain the Blender default:

    Get used to using ‘a’ to either select or deselect everything as it is Blender’s default. UV view remains mostly the same as before.

    Tags: ,