Pages

Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Friday, February 10, 2012

Code of the Day: Playblast Window

Playblast Window 0.0.2 script by Seamus O'Hearn: http://www.creativecrash.com/maya/downloads/scripts-plugins/camera/c/playblast-window

Playblast Window 0.0.2 is a simple script UI for creating specific playblast options.

Language: Python
Type: Maya Editor Script
Script Files: 1
Number of lines: 119

Functionality: 4.5
The code only offers a few options, mostly related to size which isn't a lot considering that many studios want to display certain elements or use certain compressions, formats etc. The code writes specifically to qt, with compression "H.264", everything off in the display except for polymeshes (with an option for ornaments) and using frame padding = 4. If those are the desired specs (with options for quality and size in the UI), than perhaps the functionality is enough. There do seems to be issues when attempting to add a file path (such as .avi) where the code cuts the last two digits off, the code should instead split on the . or preferably use os.path.splitext. It also looks like the code removes any spaces in directory names, which can invalid paths such as when used for example on a path that include "Program Files".

Design: 5.5
The code base is quite very small, and the functions are separated into UI, getting the file path and initializing the playblast. The code could be split into a few more functions and do a bit more error checking. Attempting to cancel out of the directory selection generates an error. In the UI there is a large warning mentioning which camera to use which is fine, but instead of the message, setting the camera and panel as options may be more intuitive and reusable.

Style: 5
The code comments itself says "it's pretty sloppy", which is honest. Overall, python by its nature does a pretty good job of keeping indentation and formatting similar, but there are a few places where variable names (such as "TheMagicalDirectory") are a little unusual and may not be entirely clear. The code only saw 12 style errors when run against pep8, but for the size of the file that is approximately 10%.

Documentation: 6.5
The code documentation in the header is pretty good. The code itself seems to be a bit overly commented (two of the functions have every line commented). In a few places the comments are useful, however in other places such as the example below, they are not necessary, and end up just cluttering the code.

#printed statement
print("Saved To")

Reusability: 5.5
The code base is tiny so it will be easy to maintain and extend. There are a few places that should have error checks, and the file extension code should be modified as I mentioned.

Overall: 4.5
Overall, the script is just too specific and with a few too many errors on basic use to be too reusable. I imagine that certain studios/artists would use it if there needs exactly matched the specifics of this file, but otherwise needs a few more options for widespread use (perhaps enabling the artist to choose to use the current playblast and/or modelPanel settings to override the builtin script settings).

Until next time,
Michael Hubbard
http://michaelhubbard.ca

Thursday, February 9, 2012

Code of the Day: PAIE

PAIE 1.3.2 script by Jakob Welner: http://www.creativecrash.com/maya/downloads/scripts-plugins/animation/c/paie

PAIE 1.3.2 (Python Attribute Import/Export) is a script for exporting animation and pose information to a file, and importing them onto the same (or similar) character.

Language: Python
Type: Maya Editor Script
Script Files: 1
Number of lines: 2293

Functionality: 8
The code is quite useful is saving out and loading poses and attributes onto similar (at least in name) objects. It does rely heavily on naming conventions, but is overall a useful tool (giving information about when a file was saved, as well as uses the python pickle module to save out its data.

Design: 8
The code base makes good use of classes and object oriented programming techniques. The code does a good job of checking for exceptions, although in a few places I think checking the input may well prevent an exception from being thrown (such as initializing the GUI class), but some programmers do prefer exceptions in some cases (I prefer not to see it on class initialization without any variables however). There are a few places where both evalDeferred are used to spawn MEL code, but in unusual ways that seem like they might have other solutions (especially when creating the UI). While the data uses the pickle module, I can't help but feeling that using a standard format (like XML or JSON) might make the data more reusable and extendable than writing a custom format.

Style: 8
The code follows pep8 pretty closely (only ~20 warnings from running the code). However there are a few areas that would perhaps benefit from moving the code out into separate files. While the class architecture does help, the overall size of the file is bordering on being a bit larger than it needs to be, especially with standalone classes available which would promote reuse of the package/module. A few of the functions (especially reading and writing) are quite large and the code path can be a bit complex in these cases.

Documentation: 7
The header information is quite good, and there are comments through the code base, but not every class is documented, and doc strings are not consistently used, which would help improve the readability of the code. The example documentation of the structure is good and it helps that most of the code has good variable and class names.

Reusability: 8
The code has a number of pieces that could be reusable as the class architecture and OO development approach naturally does. There are a few places that the code may benefit from additional comments, and there are a couple global variables (debugger and UI) that would make debugging a bit more complicated. Using a custom file format means more care has to be done on writing any extensions, but otherwise is quite straightforward.

Overall: 8.5
Overall, nice functionality implemented in a useful and elegant way. The functionality is pretty useful, and while there are some tools that do similar things, I think the underlying architecture of this script is for the most part well thought through.

Until next time,
Michael Hubbard
http://michaelhubbard.ca

Book Review: More Python Programming for the Absolute Beginner

One thing I always want to do is try and look at programming from a fresh pair of eyes. While I am certainly not an absolute beginner it is sometimes interesting to see how a development book tries to instill the core concepts of programming into the new reader. In More Python Programming for the Absolute Beginner by Jonathan S. Harbour the focus is to hit the ground running. The book focuses on the pygame module to create 13 small games/demos including: Pie Game, Trivia, Bomb Catcher, Analog Clock, Orbiting Spaceship, Escape the Dragon, Zombie Mob, Block Breaker, Oil Spill, Snake, Tank Battle, Artillery Gunner and Dungeon RPG.

Overall, the code is quite straightforward and well contained, although in a few places it seemed like a little extra engineering might go over a few people's heads, but still produce more concrete code. The code examples actually get better with more classes and better encapsulation later on, and I suppose it can be a bit much to introduce everything to an "absolute beginner". One thing that I thought was interesting was the workarounds necessary to deal with the pygames sprite rotations: "Pygame does not correctly adjust for the change in image size when a sprite is rotated", the fix is to "shift the image by the amount of change in dimensions from the normal image to the rotated image" (pg 203, 205).

Overall, this is a pretty entertaining read, and gives some good quick examples of pygame. The book also links to the author's forum where there is more information and updates: http://jharbour.com/forum/index.php?PHPSESSID=f8s6pkk6tdvb47ullmqormult4&board=23.0

One of the most expansive topics was the Dungeon "Roguelike" RPG. For an example of that, see the link posted in the above forum at: http://roguebasin.roguelikedevelopment.org/index.php/Main_Page

It may be fun to experiment a bit more with pygame for some simple examples, but for now, I am focused far more on other python modules.

Until next time,
Michael Hubbard
http://michaelhubbard.ca

Saturday, April 2, 2011

Python Design Patterns

There is an intersting talk about Python design patterns at http://www.youtube.com/watch?v=4KZx8bATBFs&feature=related from Google Students. I really liked the comment that "Design Patterns must be studied in the context on the language in which they'll get implemented" as some patterns can be change or disappear entirely if there is a better way to do something that the language supports.

One of the interesting talks on everybody's favorite/hated pattern is how to deal with a singleton. Singleton's are conceptually good (just one of something) but in practice they so often become just global variables as noted here http://c2.com/cgi/wiki?GlobalVariablesAreBad I personally prefer using dependency injection whenever appropriate, as it gives a lot more flexibility in what class the objects are actually interacting with. However with python there is an intersting approach in using the module as a singleton. This has its own issues (no subclassing modules, etc.) but does provide an interesting approach to think about (especially with the lack of private methods/members in python).

Good luck,
Michael Hubbard
http://michaelhubbard.ca

Friday, April 1, 2011

Book Review: Python Developer Handbook

A worthwhile tome of knowledge, Python Developer's Handbook by Andre Lessa is a good introduction to Python in all forms. While it does not deal with Python 3.X it still gives quite a lot of examples about how complete python is as a language, and how useful it is for tools. As one of the big three languages used by Google, one of the most popular languages in academia, API implementations for some of the big 3D tools (Maya, Blender, Fusion, etc.) and one of the most talked about languages in the world http://langpop.com/ Python is likely going to be around for a while.

For those that don't know, Python is also named after Monty Python (the greatest sketch comedy show and some of my favorite movies of all time), and throughout the book (as is Python tradition) to use examples from the sketches as part of the fun. Such as:
print "I'm a lumberjack and I'm ok!"
The book start with the basic syntax, exception handling, and object oriented programming and then moves to more advanced topis such as extending and embedding python, distribution, databases, and threads. There is a significant section on network programming, web development data manipulation with XML. The book covers both some higher level modules and lower level details. Along with the network programming is some information on GUI and graphic elements, as well as UI elements with Tkinter.

The development tools mentioned in the book are IDLE, but I would recommend Eclipse + Pydev as the way to go. Pydev is a great IDE, and the debugging options from Eclipse are really great.

I use Python for creation of tools, Maya API scripts, and more, but learning more of the networking tools makes for a more complete game programmer/technical artist :)

Bye for now,
Michael Hubbard
http://michaelhubbard.ca

Monday, October 18, 2010

Coding Style and Stylecop

How important is coding style? This debate is something that really draws out the "artist" and critic in every programmer. In many ways the style of writing code is similar to writing poetry, and what makes the subject so hotly debated is that the beauty of the code, sentence or poem is all in the eye of the beholder. Just like those critics of of prose and poetry, the serious critiques of coding style come from understanding the limits and history of the form. Practice reading and writing lots of code, in different languages if possible, and acknowledging how many ways there are to write the same functionality.

In the same way that experimentation in poetry and prose makes for a better understanding of the overall form, understanding how different coding standards can make for better (or worse) code. With every coding standard the code will be limited but also potentially clarified in a new way.

Organization of code one way can make code reviews easier, but potentially making maintanability more difficult, code may be separated into different sections that are ordered in a certain way that causes significant scrolling, opening of multiple files and jumping through layers of code, that structured a different way may minimize bouncing around.

Coding standards for teams is especially important, and should be strictly adhered to as best as possible, for the overall benefit of the team. Consistency is very important as being able to quickly jump into a new class, module or section of the code "should" be as easy as jumping into a section that you wrote yourself. Coding standards are an agreement between programmers to create a cohesive work, much like any collaborative effort, the final product should be a uniform work of art.

The most diplomatic way to handle this is often through adopting a tool that lets catches errors quickly and consistently. This also frees up code reviews to be on more important aspects like architecture, re-usability, error checking, design patterns etc. as non-adherence to coding standards often ends up becoming the main focus if the code strays too far from the standards outlined. These tools should also be part of the commit process, so that the code is rejected if it does not pass the coding standards (in certain cases some of these tools may have to have slight adjustments, as desired, but with a focus on consistency).

For those C# Unity or XNA projects some guys really like Resharper (30 day trial at http://www.jetbrains.com/resharper/) For a free style specific checker I like StyleCop: http://stylecop.codeplex.com/ there is a good amount of customization and easily embeds into Visual Studio. The information that is provided from the code checks make it easy to know the issue and change the code to follow the coding standards that have been setup.

For python there is pep8 http://pypi.python.org/pypi/pep8 and pychecker http://pychecker.sourceforge.net/ as well as a few more.

In general the style and syntax checkers are becoming quite popular (especially with scripting languages that don't compile) but regardless a good "style cop" will improve code quality, readability of all sections of the code, and allows everyone to focus on writing great code, instead of what it looks like in the end.

Until next time, StyleCop: that is all.
Michael Hubbard
http://michaelhubbard.ca