+1 tag for Google+

Friday, 23 June 2017

Python Script: Type Like a Human

Here's my latest fun script. A piece of Python code that aims to emulate a human typing (with sounds too!)

(Also available on Vimeo.com)

This is my first attempt using Python to playback audio files on the computer.

Saturday, 20 May 2017

K-Means Clustering in Maya (Python)

Watch this video on Vimeo instead.

In this video, I show the results of me implementing a k-means clustering algorithm coded in Python, inside Maya.

After having read about what the K-means clustering algorithm is, and how it works, I challenged myself to create a Python function to perform that, using primitive objects in Maya to represent data points and clusters centroids.

After I wrote the code, I discovered SciKit Learn python library also has a k-means algorithm. However, there is more satisfaction to be had by the fact that I managed to code it myself, rather than just using whatever has already been written out there.

Monday, 24 April 2017

pymel.core Versus maya.cmds

Is PyMel slower than Maya's cmds module?

PyMel is an alternative to Maya's native maya.cmds. It was developed and maintained by Luma Pictures. It is open source, and lives in GitHub here: https://github.com/LumaPictures/pymel

Pymel was so intuitive and successful that Autodesk has shipped with pymel alongside its maya.cmds.

Today I stumbled across a discussion:

Someone was asking if pymel performs as quickly as native maya.cmds. Another person referenced this article: http://www.macaronikazoo.com/?p=271

The author (Hamish McKenzie) did a comparison with his following code:
import time
import maya.cmds as cmd
MAX = 1000
start = time.clock()
for n in xrange( MAX ):
print 'time taken %0.3f' % (time.clock()-start)

from pymel.core import ls
start = time.clock()
for n in xrange( MAX ):
 ls()  #NOTE: this is using pymel’s wrapping of the ls command
print 'time taken %0.3f' % (time.clock()-start)

When I run the code I get the following print-out.
time taken 3.983
time taken 77.059

The code lists all nodes in the current scene 1000 times with maya.cmds and pymel, then prints the time each method takes. The article was written in 2010. Hamish reports a speed difference of 350 times! It is now 2017, the pymel library has probably undergone iterations of improvements and optimisation. From the figures above, the execution time difference on my work machine is 19.34 times.

After reading that post, I went to modify his code to include a few more actions. Create a cube, rename the cube for a user-specified number of times, list all objects in the scene, then delete all the cubes.

The code now reads like this:

import time
import maya.cmds as mc
from pymel.core import *

def testMayaCmdsPymel(numIterList):
    a function to compare a time taken to perform similar 
    set of actions using maya.cmds and pymel.
    numIterList - a list consisting of the number of iterations
                    which to run the comparison tests on
    def testCmds(numIter):
        MAX = numIter
        start = time.clock()
        objList = []
        print '-- maya.cmds --'
        for n in xrange( MAX ):
            obj = mc.polyCube()[0]
            objList.append(mc.rename(obj, 'cube1'))
        timeCmds = time.clock()-start
        print 'time taken %0.3f' % (timeCmds)
        return timeCmds

    def testPymel(numIter):
        MAX = numIter
        start = time.clock()
        objList = []
        print '-- pymel --'
        for n in xrange( MAX ):
            # using pymel’s wrapping of the polyCube() command
         obj = polyCube()[0]
         # using pymel's wrapping of rename() command
         objList.append(rename(obj.name(), 'cube1')) 
         ls() # using pymel’s wrapping of the ls() command
        delete(objList) # using pymel’s wrapping of the delete() command
        timePymel = time.clock()-start
        print 'time taken %0.3f' % (timePymel)
        return timePymel
    timeDiffDict = {}
    mainStart = time.clock()
    for x in numIterList:
        print 'duplicating %i objects' % (x)
        timeCmds = testCmds(x)
        timePymel = testPymel(x)
        timeDiff = float(timePymel)/float(timeCmds)
        timeDiffDict[x] = [timeCmds, timePymel, timeDiff]
        print 'difference: %0.3f times\n' % (timeDiff)

    print 'Results of test series:'
    print 'num iterations\ttime cmds\ttime pymel\ttime difference'
    print '--------------\t---------\t----------\t---------------\t'
    for x in sorted(timeDiffDict.keys()):
        print '%04i objects\t\t%0.3f s\t\t%0.3f s\t\t%0.3f times' % \
                (x, timeDiffDict[x][0], timeDiffDict[x][1], timeDiffDict[x][2])
    print '\ntotal time taken:', time.clock()-mainStart

Invoking the function with the code:
testMayaCmdsPymel([10, 15, 20])

I get the following print-out:
Results of test series:
num iterations time cmds time pymel time difference
-------------- --------- ---------- ---------------
0010 objects 0.084 s 0.837 s 9.999 times
0015 objects 0.088 s 1.248 s 14.216 times
0020 objects 0.115 s 1.730 s 15.005 times

total time taken: 4.12330471431

Invoking the function with the code:
testMayaCmdsPymel([10, 50, 100, 200, 300, 400, 500, 1000])

I get the following output:
Results of test series:
num iterations time cmds time pymel time difference
-------------- --------- ---------- ---------------
0010 objects 0.085 s 0.847 s 9.980 times
0050 objects 0.294 s 4.299 s 14.628 times
0100 objects 0.610 s 9.391 s 15.391 times
0200 objects 1.280 s 20.519 s 16.030 times
0300 objects 1.963 s 34.734 s 17.692 times
0400 objects 2.617 s 50.107 s 19.150 times
0500 objects 3.484 s 68.700 s 19.720 times
1000 objects 8.033 s 190.390 s 23.700 times

total time taken: 397.417362303

I see that the time difference in execution generally increases, even though the increase starts to slow down after 300 objects. However, the increase in speed that we get from maya.cmds versus pymel is at least 10.8 times, and in the worst cases 24.5 times!

I know it sounds really late to be discovering this, but it is never too late to switch. I really like how pymel makes maya.cmds wrapper more pythonic, readable and consistent to work with.

Now I am deliberating whether to continue with pymel or completely jump back to maya.cmds. My inclination is to use both, native maya.cmds for operations I know will iterate across huge number of objects, and pymel for other less intensive tasks.

Friday, 24 February 2017

New Documentary: Hollywood's GreatestTrick

I just became aware of a 2-day old article about a documentary available in full, showing the current situation of visual effects workers in Hollywood's film industry.

Hollywood's Greatest Trick

It talks about how the industry has been able to continue to raise the bar on the quality and increased number of visual effects shots in an average Hollywood film.

The revenue generated by these films are also discussed. This is put in context when compared to the overheads and marginal profits that can be made from film to film. The documentary then shows that on top of struggling just to break even, what visual effects vendors and companies are up against when bidding for movies to be awarded to them from a limited and fixed pool of client studios.

At the artists level, the film talks about how artists are expected to work long hours, and their passion for their work was being taken for granted, oftentimes for the benefit of the clients.

More and more companies are hiring on a per-project basis, and they set-up offices in places that allow them to operate at the cheapest costs (labour costs and tax breaks offered to film-related business activities). Artists are forced to move with the companies, in order to find work, and that may only last for a few months to a year, as most companies hire on contract terms nowadays.

If you have friends, family or relatives working in the visual effects industry, or if you just enjoy Hollywood films, or maybe you enjoy watching behind the scenes featurettes, fascinated by what visual effects can do, it is definitely worth your time watching this film.

It is my hope that more people become aware that behind the beautifully magical visuals of Hollywood films lie not just advanced image manipulation technology and software, but also a huge team of highly skilled workers who work the software and craft their shots.

In the middle of the article, there is an excellent infographics animation that clearly introduces the major stages of how a visual effects shot is created. It is really enlightening and can be easily understood by the lay person. I highly recommend watching this!

Sunday, 15 January 2017

Maya Expressions Part 04 - Twisting Cubes

Video also available on Vimeo.com.

In Maya Expressions Part 4, we start to use expressions across multiple objects. This results in an interesting rig where the objects driven by expression can arrange themselves in interesting ways.

In my set up, every cube has a slight offset in translation and rotation to the one before, As the first cube moves along its x-axis, the spacing between cubes and each of their rotations change and react to reflect that change.

In the video I also show how I structure my expression so that as much as possible, the same code can be applied to each cube with minimal alteration. With the standardised code and minimal change for each of the cube, it will become relatively easy to replicate this behaviour to as many extra cubes as required without hassle.

Maya Expressions is my running series introducing and using the powerful expression function in Maya to achieve tasks that would otherwise be very manual or inefficient to set up and manage.

Watch the previous videos!
Maya Expressions Part 1
Maya Expressions Part 2
Maya Expressions Part 3