username

Chad | Python, Technical Art | Thursday, May 22nd, 2008

Python is cross platform, and powerful enough to dig into the operating system. Here is a neat example of that. I use this in a lot of my scripts be they Maya, Motion Builder or standalone. It adds a little personal touch when sending messages to the users of the scripts.


#-----------------------------------------------------------------#
# SCRIPT: cmGetUser.py
# AUTHOR: Chad Moore
# DESCRIPTION: Prints the username
#-----------------------------------------------------------------#
import os
import getpass

currentUser = getpass.getuser()
print currentUser

The ‘#’ that starts a line indicates a comment, a place for you to write notes to yourself and the people looking at the code of your script. Commenting your scripts is an art unto itself, I have a post planned just for that topic which highlights my comment conventions. It is wise to include a header that shows the title, author, usage, date, version number, etc, etc.

os is the module which allows for communication with the operating system. Whichever platform you are running this on, the os module will call the specific junk you need.

getpass has the info for the currently logged in user. I haven’t explored that module much other than the .getuser component. It is pretty self explanatory. Stick that into a string variable, and you can call it wherever you’d like.

Let’s say your login name is something unfriendly like “cmoore”. Well the above script would return cmoore, which isn’t as friendly as it could be. so why not use a simple if to get more personal.

if currentUser == 'cmoore':
niceName = 'Chad'

print niceName


If you know all the names of the people on your team, you can add this functionality into your tools. It’s not “mission critical” but it helps people feel comfortable using the tools. Or people get paranoid, because they wonder how the computer knows their name. ;)

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress | Theme by Roy Tanck