Python Quick Tip – dir() function

**This tutorial was done by python guru Matt Pearson. Check out his website for more Python goodness**

So you get a buddies python script that he promised would solve all of your problems. You import it into C4D but either you can’t remember, or you were never instructed which function to call that will save your life. What the flip! Well the nicest little, rarely remembered, function exists to help you. Python has a built in function known simply as dir(). This little guy will return to you a list of names of all attributes found in the called script or module. There we go, I found it, I probably have to run createUI to get access to this tool.

C4D Python - dir() function

If you use it without an argument it will return you a list of modules or variables existing in the local scope. I use this all the time to make sure that I have a specific module imported before I start to write a ton of code and when I go to execute it I get an error dropped on my head.

C4D Python dir() function

 

Other tutorials

  • PythonObjectID
  • http://goo.gl/mJh20 Sanjeev Kumar

    or you can also do help(imported module name)

    • http://www.muttsy.net/blog Matt Pearson

      That is a good point, the help function can be a ton of… help. But that assumes your friend or the random guy-on-the-internet’s module contains a docstring. I will be honest and say that I commonly won’t put a docstring in many of my modules unless they are super solid and ready to be distributed. So if help doesn’t return anything helpful, it’s good to have dir() to fall back upon. Thanks for the comment!