Archive for August, 2009

Weak References

Last year while I was working at Red Rover, I heard the term “weak reference” in reference to a technique for referencing objects in 3DS Max. The Max TD used them for a variety of things. I didn’t quite understand what he was talking about at the time, since the last version of Max I used was 2.5 and I never did rigging or coding for it then.

More recently I’ve come to the same technique on my own both in Maya and in Cinema 4D, and was reminded of the name for it by that same TD over beers a few weeks back.

Essentially, weak references are variables on objects that contain a pointer to an object, and not a reference to it’s name. In Maya, for example, you may see rigging scripts written by TDs referencing specific objects by name or saving names of objects as they’re created and using those names to connect attributes or add constraints. In a clean scene this works fine, as long as the rigger is meticulous in their naming scheme and runs different stages of the rigging script in proper order.

But what happens when Maya namespaces become involved? As soon as you reference in an asset, every object that makes up that asset gets a namespace prefixed onto it’s name. If you’ve written scripts that require specific names, they break. If your layout files aren’t perfect and the namespace is different between two or more shots (as Maya is wont to append a number, regardless of what you specify), useful tools like character GUIs and the like break and you’re left doing namespace surgery in a text editor.

Weak references sidestep all this by giving you a permanent connection to an object regardless of name changes or namespace prefixes.

A good example is how I’m currently handling cameras in scenes. A decision was made early on, on the current project at work, to name cameras in layout files by the name of the shot / sequence. Normally this isnt a problem, but we’re using a renderer that’s not linked into Maya directly and therefore needs a command line batch exporter written. If all the cameras are named differently, and the camera’s animation has to be baked and exported as well, how do you go about picking the right object?

Using weak references, the problem becomes trivial. You create them as follows:

addAttr -ln "cameraObj" -at "message";

You’ve probably seen attribute connections of type message into IK handles and other things. The message attribute carries no data– that is, it never changes and causes no DAG recalculations. (This is doubly useful because you can connect the message attributes of two objects to message-typed user attributes in a cycle without causing a cycle error– more on that later.) However, the attribute can be used to get the name of the connected object like so:

listConnections object.messageAttribute;

It will return an array of strings. If you rename an object, you can get the object’s current name through the above command.

So where do you store these attributes? For the moment I’m using a trick I saw on the Siggraph ’08 talk by Blue Sky on procedural rigging: I create non-executing script nodes and store connections on them. In the camera example above, every scene has a master script node. On that node are a few attributes, including its “type” and a .message connection to the render camera. It’s them trivial to find the camera’s name:

string $sel[] = `ls -type "script"`;
for ($s in $sel) {
	if (`attributeQuery -node $s -ex "snCamera"`) {
		// this should be the one you need
		// normally I search for the type, but this is an example
		string $conn[] = `listConnections ($s + ".snCamera")`;
		// if it's only one connection incoming, then you're done.
		print("Camera is named " + ($conn[0]) + "\n");
	}
}

This technique can be extended to include all kinds of objects. It can also be very helpful for scripts like character GUIs that need to know what characters are present in a scene, and be able to change the positions of all those controls.

One final note on this for now: In Cinema 4D, every object and tag in a scene can be named the same. Searches for objects or tags by name are often fruitless because of this; if two objects or tags have the same name there’s really no easy way to tell which is which in a COFFEE script. What you can do, however, is create a user data variable that is of the Link type. This allows you to drag and drop an object into that variable’s edit field, and provides a permanent pointer to that object regardless of name. This is very useful in rigging; for example, you can always tell which joints in a leg are control joints, and which are bind joints, by creating links. You can also expose the links in XPresso and use the pointers as if you’d dragged an object onto the XPresso node window.

Jaspercation

I’ve never really taken vacations. There were a few done with the family as a kid, but we all know how those are– it’s not the same as when you choose destinations for yourself.

About a month and a half ago Trez asked if I could pop out to join her and her family for a vacation to Jasper. I had planned on being in Calgary for my birthday but she had other plans, so I made arrangements. I can’t express how glad I am that I did.

Part of it is that I haven’t really gotten to see Trez outside of weddings we were both attending or Kilbourne-related outings; while I enjoy both, neither are conducive to chatting with your best friend for a real length of time. This was different; all of us went for bike rides, swimming, and even managed a canoe row on Lac Moraine. I finally got to hang out with Franni’s hubby, and Trez and Franni’s parents, and felt the whole time like part of the family.

But that’s not all of it. I’ve been back to Alberta a few times now since returning from Japan. It’s different, don’t get me wrong — Calgary’s C-Train goes all the way past Crowfoot now, for frack’s sake — but I get there and feel the prairie wind on my face and the sun on my arms and see my darling Rockies to the West and I wonder how anyone lives anywhere else.

On Sunday afternoon while we were grabbing supplies for soap making, half the sky went dark and those large drops of rain you don’t get outside the prairies came down, fat and cold and lazy-slow. I swear it smells different out there, the rain, dustier and sharper than what we get in Toronto. Or maybe it’s just that my allergies never flared up despite how green everything was. Maybe it’s all in my head. In my head or not, it was like slipping into a warm bath after a long day. I chased the sun westward for three and a half hours and ended up the only place I’ve ever felt home.

So as I started typing this I was on the plane back to Toronto and I, yet again, wondered why I’m living in the East. Sure there are a lot of animation and game studios in Toronto and Montréal. Sure there are those who say that going back would be like living backwards, like regressing. Maybe they’re right. Thing is, as smart as living in Toronto is for me right now, smart hasn’t been winning out in my mind.

Okay, enough with all that. How about pictures? They’ll be up on my Flickr account soon enough; I had to edit through pics from Jamie’s wedding first. I’m actually two sets of photos behind now, with the shots from my and Cory’s birthdays.