Maya Particle Instancing Experiment

Maya Python script:

#Audio Driven Particle System 2011.10.27 A1
#Python for Maya
#Code By: Oliver Wolfson
#oliverwolfson.com - wolf@oliverwolfson.com
#You will need keyframes formatted in text files in an exclusive directory to make this work. See:
#http://oliverwolfson.com/animating-with-audio/ for more details

import os
import maya.cmds as mc

##LIGHT
Light = mc.spotLight(ca=120, n="mySpotLight")
mc.setAttr( ("mySpotLightShape.useDepthMapShadows"), 1)
mc.setAttr( ("mySpotLightShape.dmapResolution"), 2048)
mc.setAttr( ("mySpotLightShape.dmapFilterSize"), 6)
mc.setAttr( ("mySpotLightShape.penumbraAngle"), 10)
mc.setAttr( ("mySpotLightShape.dropoff"), 1)
mc.setAttr( ("mySpotLight.translateX"), 125)
mc.setAttr( ("mySpotLight.translateY"), 100)
mc.setAttr( ("mySpotLight.translateZ"), 20)
mc.setAttr( ("mySpotLight.rotateX"), -33)
mc.setAttr( ("mySpotLight.rotateY"), 10)
mc.setAttr( ("mySpotLight.rotateZ"), -45)
mc.setAttr( ("mySpotLight.scaleX"), 30)
mc.setAttr( ("mySpotLight.scaleY"), 30)
mc.setAttr( ("mySpotLight.scaleZ"), 30)

##AUDIO DRIVEN THING
num = 0
for p in range(0, 64, 2):
    print("this is the " + str(num) + " num:" + str(p))
    num += 1
    Locator = mc.spaceLocator(n=("myLocator"+ str(num)))

    Shader = mc.shadingNode('blinn', asShader = True)
    mc.setAttr ((Shader+ ".glowIntensity"), .1)
    SG = mc.sets (renderable=True, noSurfaceShader=True, empty=True)
    mc.connectAttr((Shader +".outColor"), (SG + ".surfaceShader"))
    mc.setAttr((Shader + ".color"),0.203189, 0.252033, 0.57554)

    Plane = mc.polyPlane(h = 10, sw = 1, sh = 15, cuv = 2)
    mc.setAttr(str(Plane[0]) + ".rotateX", 90)
    mc.setAttr(str(Plane[0]) + ".scaleX", .25)
    mc.makeIdentity( apply=True )
    mc.hyperShade(assign = Shader)

    mc.select(clear=True)
    Emitter = mc.emitter (type = 'omni')
    Particles = mc.particle()
    mc.connectDynamic(Particles, em= Emitter )
    mc.setAttr((Particles[1] + ".lifespanMode"), 1)
    mc.select(Plane[0])

    Wave = mc.nonLinear( type= 'wave' )
    mc.setAttr((Wave[0] + ".wavelength"), .4)
    mc.setAttr((Wave[0] + "Handle.rotateZ"), 90)
    mc.setAttr((Wave[0] + "Handle.rotateY"), 50)
    mc.setAttr((Wave[0] + "Handle.translateY"), -7)
    mc.setAttr((Wave[0] + "Handle.translateZ"), 11)
    mc.makeIdentity( apply=True )
    mc.select(clear=True)
    mc.particleInstancer( Particles[1], addObject = True, object = Plane[0], cycle='None', cycleStep=1, cycleStepUnits='Frames', levelOfDetail='Geometry', rotationUnits='Degrees', rotationOrder='XYZ', position='worldPosition', age='age')

    mc.expression ( s = Wave[0] + ".offset = " + Locator[0] + ".translateY/10;\n" + Wave[0] + ".amplitude = " + Locator[0] + ".translateY/10;\n" + Wave[0] + "Handle.rotateX = frame;\n" + Wave[0] + "Handle.rotateZ = " + Locator[0] +  ".translateY*0;\n" + Wave[0] + "Handle.scaleX =" + Locator[0] + ".translateY;\n"  + Wave[0] + "Handle.scaleY = " + Locator[0] + ".translateY;\n" + Wave[0] + "Handle.scaleZ = " +  Locator[0] + ".translateY;\n")
    mc.expression ( s = Emitter[0] + ".translateY = " + Locator[0] + ".translateY;\n" + Emitter[0] + ".rate = " + Locator[0] + ".translateY*20;\n" + Emitter[0] + ".speed = " + Locator[0] + ".translateY;\n")
    mc.expression ( s = Plane[0] + ".rotateY = " + Locator[0] + ".translateY*60;\n" + Plane[0] + ".translateY = " + Locator[0] + ".translateY;\n" + Plane[0] + ".scaleY = " + Locator[0] + ".translateY/;\n" + Plane[0] + ".rotateX = " + Locator[0] + ".translateY;\n")
    mc.expression ( s = Shader + ".incandescenceR =" + Locator[0] + ".translateY/200;\n" + Shader + ".incandescenceG = " + Locator[0] + ".translateY/200;\n" + Shader + ".incandescenceB = " + Locator[0] + ".translateY/100;")

    mc.select(Locator)
    mc.select(Plane, add=True)
    mc.select(Emitter, add=True)
    mc.select(Wave , add=True)
    mc.group(n= ("myGroup" + str(num)) )
    mc.setAttr("myGroup" + str(num) + ".translateZ", p*-10)

mc.select(clear=True)
for l in range(1, 33, 1):
    mc.select(("myLocator" + str(l)), add=True)

## add path to keyframe data files - just the source directory that contains them.
rootdir='/Volumes/2TB/work/3D/Maya/Maya_projects/Mogwai/sound/ranoPanoKeys01/zapped'
## "ty", in line 13, refers to the Maya attribute channel of the selected objects. Modify this attribute as necessary.
objs= mc.ls(sl=True)
for subdir, dirs, files in os.walk(rootdir):
    for thisFile, o in zip(files, objs):
        file = open((os.path.join(rootdir, thisFile)), 'r')
        lines = file.readlines()
        file.close()
        for i in range(len(lines)):
            mc.setKeyframe(o, at='ty', v=float(lines[i]), t=i, itt='linear', ott='linear')

Use this information to build key data for this script
http://oliverwolfson.com/extracting-raw-key-frame-data/

Python, word count

A simple program that will count the words and sentences in a text file. Sightly modified version of the code I found on daniweb.com.

# count lines, sentences, and words of a text file

# set all the counters to zero
lines, blanklines, sentences, words = 0, 0, 0, 0

# write the trs file
fname = "/path/filename.txt"

# read the file back in
textf = open(fname, "r")

# reads one line at a time
for line in textf:
    #print line,   # test
    lines += 1

    if line.startswith('\n'):
        blanklines += 1
    else:
        # assume that each sentence ends with . or ! or ?
        # so simply count these characters
        sentences += line.count('.') + line.count('!') + line.count('?')

        # create a list of words
        # use None to split at any whitespace regardless of length
        # so for instance double space counts as one space
        tempwords = line.split(None)
        #print tempwords  # test

        # word total count
        words += len(tempwords)

textf.close()

print '-' * 50
print "Lines      : ", lines
print "Blank lines: ", blanklines
print "Sentences  : ", sentences
print "Words      : ", words