Friday, 30 March 2012

Python: Writing nested dictionaries to file

Best suggestion I could think of was using cPickle (if available).
try:
    import cPickle as pickle
except:
    import pickle

# nested DICTIONARY
data = { 'one': {'label': 'This is shot 001', 'start': 1, 'end': 10},
         'two': {'label': 'This is shot 002', 'start': 11, 'end': 25},
         'three': {'label': 'This is shot 003 - Needs Editing', 'start': 26, 'end': 50} }

# write to file with cPickle/pickle (as binary)
def ld_writeDicts(filePath,dict):
    f=open(filePath,'w')
    newData = pickle.dumps(dict, 1)
    f.write(newData)
    f.close()

ld_writeDicts('C:/Users/Lee/Desktop/test2.dta',data)

#############

# read file decoding with cPickle/pickle (as binary)
def ld_readDicts(filePath):
    f=open(filePath,'r')
    data = pickle.load(f)
    f.close()
    return data

# return dict data to new dict
newDataDict = ld_readDicts('C:/Users/Lee/Desktop/test2.dta')
# test nesting
print newDataDict['one']['label']
As you can see its very simple to use and very quick. There are loads of alternatives if you wanted a nicely arranged, readable file, including json:
import json

# nested DICTIONARY
data = { 'one': {'label': 'This is shot 001', 'start': 1, 'end': 10},
         'two': {'label': 'This is shot 002', 'start': 11, 'end': 25},
         'three': {'label': 'This is shot 003 - Needs Editing', 'start': 26, 'end': 50} }

# write to file with json encoding
def ld_writeDicts(filePath,dict):
    f=open(filePath,'w')
    newData = json.dumps(dict, sort_keys=True, indent=4)
    f.write(newData)
    f.close()

ld_writeDicts('C:/Users/Lee/Desktop/test',data)


# read file decoding with json
def ld_readDicts(filePath):
    f=open(filePath,'r')
    data = json.loads(f.read())
    f.close()
    return data

# return dict data to new dict
newDataDict = ld_readDicts('C:/Users/Lee/Desktop/test.dta')
# test nesting
print newDataDict['one']['label']

However im all for cPickle.

Thursday, 15 March 2012

Mel: Invert Selection from a render layer

Thought I might post up this useful code someone asked for...

//  ld_invertSel_rlyr.mel  0.2
// 
//  Authors:     Lee Dunham
//  Licence:     Creative Commons, Attribution, Share Alike
//  About:       Inverts selection for visible objects in current render layer.
//  Usage:
//      ld_invertSel_rlyr ;
global proc ld_invertSel_rlyr(){
string $currentRenderLayer=`editRenderLayerGlobals -q -currentRenderLayer` ;
string $objsRenLayer[]=stringArrayRemoveDuplicates(`editRenderLayerMembers -q $currentRenderLayer`) ;
for($obj in $objsRenLayer)
    if(`getAttr ($obj+".v")`==1 && `objectType -isType "transform" $obj`)
        select -tgl $obj ;
    else
        select -d $obj ;
}

It'll invert the selection for whatever visible transform objects are in the current render layer. Its not fantastic but it was a 5-minute job.

Wednesday, 14 March 2012

ld_mirrorMe v1.5.0

I was surprised by the response this tool had, so I'm releasing v1.5.0 to help some people out, it'll be good to hear how you guys use it.
Alongside my softCluster script, you'll definitely notice a speed increase in your workflow.

Download it here (mediafire link).
Download, extract and place script in maya's script directory and use
import ld_mirrorMe
ld_mirrorMe.GUI()


It's features are shown on this much quicker demo (v1.0.0), supporting multi-mirroring for all modes.

Full details:
 - 3 modes:
     1. Curve - mirrors nurbs curves, ideal for mirroring rig controls,
     2. Mesh - mirrors geo, ideal for mirroring blendshape targets,
     3. Deformer - mirrors only clusters (at present), uses object's pivot for mirroring.
 - multi-mirroring on all 3 modes,
 - choose mirror axis,
 - search and replace,
 - alter new objects position (currently either world 0, target, mirror or original - depending on mode),
 - world position/rotation based, will work wherever or whatever the targets connected to,
 - entry objects names retained, if multiple copies needed (ideal for multi-clustering),
 - colourize option for Curves, if given curve is coloured (transform or shape), its mirror can be assigned a different colour,
 - quick and simple to use.

Updates:
09/03/12 - 0.5.0
    Initial working version.
10/03/12 - 0.9.5
    Tidied UI and reduced/cleaned code.
13/03/12 - 1.0.0
    Added colour option for curves.
    Removed all pymel (speed issues).
    Allowed multi-mirroring.
14/03/12 - 1.5.0
    Thanks to Matt Murray for feedback for further improvements.
    Added option to mirror world position of mirrored curve.
    Added further error-checking for all modes.
    Fixed bug causing unwanted locking of attributes.
    Added option to disable colouring of mirrored curve.

Again, any feedback, problems/bugs or ideas lemmie know and I'll do what I can.

Saturday, 10 March 2012

ld_mirrorMe tool demo

Just made a quick demo for one of my 3 main toolsets, (ld_animateMe v2, ld_rigMe v1 and ld_mirrorMe v.9.5) the ld_mirrorMe tool, something I started a while ago but put off.



This demo primarily shows mirroring on objects from world 0, but everything is calculated from worldspace and works accordingly, it doesn't matter where it is or what its connected/parented to, the mirror will work as expected.

Details:
3 modes - Curve, Mesh and Cluster (at present),
 - Curve - Primarily for mirroring nurbs curves for controllers, Mesh - Primarily for mirroring blendshapes (can mirror multiple targets at the same time), and Cluster - (developing for other deformers), given a deformer and its mesh will mirror the deformer over giving a correctly mirrored pivot and origin based on the objects pivot.
Search and replace feature,
Choose mirror axis,
Choose new objects position,
World based - will work wherever or whatever the targets connected to,
Entries retained, no need to keep adding same object of multiple copies needed,
Simple to use.

This was initially going to just be a tool for my rigging UI (ld_rigMe), but I prefer having it separate.

Any ideas/comments welcome.

Wednesday, 7 March 2012

Python: Create Cluster from soft selection

Get the script here.
or from creativecrash here.


All thanks to Brian Escribano for his softSelection() code that gathers the id and weights of the current soft selection (basically, all the hard work).
It was simple enough to apply that information to a new cluster.

Usage:
Extract and place script file into a maya script directory (or python path directory) and run with this
import ld_createSoftCluster as sc
sc.ld_createSoftCluster()

and thanks to Valen Wagner for the icon.

Update:
07/03/12 - 0.4.0
    Alters cluster position to match selection rather than influenced average.

Sunday, 4 March 2012

Showreel 2011-12

Latest work from previous project, Ha Ha Hairies, where I worked for Red Wire Media as the Character TD/Animator.



Reel Breakdown:

Overview:
Rigged all shown characters, scripts and GUI's.

Sequence 1 (00:00:10):
Rigged main character 'Kwiff';
- Rig fully scalable and modularly built to provide easy updating of rig if needed.
- Simple Nurbs-based selection GUI for easy interaction.

Sequence 2 (00:00:31):
Hybrid IK/FK spine for extreme posing and toony nature of show;
- Broken hierarchies to separate upper and lower posing and flexibility.
- Traditional FK control during rotation, controllable volume preservation during translation.
- Position and orient switching for full IK control.

Sequence 3 (00:00:39):
Stretchy, bendy, volume-preserving limbs;
- With custom foot-roll, banking and various pivoting attributes.

Sequence 4 (00:01:18):
Various scripts to assist posing and animation;
- Ik/Fk blending, with snapping between the two over a single frame or a range of frames.
- Fk setup to mirror live rotation.
- Script to mirror or flip any part of the rig, including entire poses.

Sequence 5 (00:01:31):
Animator-friendly finger controls;
- Setup to allow quick posing of fingers with 2 attributes, as well as individual control over each finger via attributes and individual controllers.

Sequence 6 (00:01:36):
Insert-blink script;
- Based on animator designs, for a time-saving script.


Sequence 7 (00:01:41):
Selection GUI;
- Interchangeable between full rig and facial controls.
- Varying levels of detail.

Sequence 8 (00:02:19):
Animator friendly facial rig;
- Minimal, joystick inspired controls with multiple functions to provide alot of interactivity with minimal effort.
- Joint and blendshape based, with interactivity between the controls for quicker, stronger posing.
- Tweaking controls for more specific expressions and fine-tuning.

Sequence 9 (00:02:44):
Test facial rig;
- Use of multiple, individually controllable deformers, built into the rig.
- FK beak with tweaking lip controls.
- Various deformers on expressive areas of the face.
- Re-postionable eye sockets.
- Detachable upper/lower head for extreme posing.

Sequence 10 (00:03:12):
Test facial rig - Deform test;
- All deformers work together, rather than prioritising over each other.
- Complete squash, stretching and deformation over the head rig whilst maintaining any pose, blendshape or joint-based.
- Whilst maintaining that important ability to scale.