Basic scripting question.. 2005-04-15 - By Matt Lowery
Back Hey glad to see that see someone has taken the time to up date nullifier. ( the first piece of code I ever wrote.) I wrote it way back in 2000 ( long before the match all transforms command) and was still on a steep learning curve with scripting when I did it, but it ended up being a useful tool and I still use it on an almost daily basis. Anyway, no offence taken at all Kim. Thanks for the suggestions. -- --Original Message-- -- From: owner-xsi@(protected) [mailto:owner-xsi@(protected)]On Behalf Of kim aldis Sent: Friday, April 15, 2005 12:50 PM To: XSI@(protected) Subject: RE: Basic scripting question..
I'm waiting for stuff to happen so I figured I'd convert it to the OM and JS, add a few bells and whistles and pass some comments. No offence intended to matt or anything but I thought it'd be a good opportunity to add some notes on good coding and tool building practice. The finished piece is below:-
*
I'm just copying the transform, as already suggested. This will copy scales and rotates too. It's equally possible to copy just posx, et al, if you just want positions. *
I've used an Enumerator loop. As Brad points out in his 'Loopin Lizards' piece, this is by ar the quickest way to loop through a list. Add ro this, it's all OM so now command logging = fast. You'll really notice the difference when you're up to 500 objects, it may even make the difference between you doing it and deciding it'snot worth it. *
code is commented. Doesn't matter how short the piece is, *always* meaningfully comment your code. *
I've used meaningful names. var A = and var B = is just going to become a nightmare in anything longer than 20 lines or so. Note also, I've prefixed variable names with an 'o', for 'Object'. adding meaningful prefixes to variables tells you what type they are. s for string, a for array, yadda, yadda. *
Short, isn't it. But it does show how much you can simplify things with a bit of understanding and forethought. And, short code means legible code and legible code is god. *
I've turned it into a function which returns the created hierarchy root. This makes it modular, easy to include in other scripts, lets you start library-fying your work so you can reuse it. It also makes it easier to manipulate the result later in the script - see the shift line just ater the function call. *
I've gone to a bit of trouble to name the nulls after the objects they've taken their transforms from. I can't emphasise this enough, ALLWAYS MEANINGFULLY NAME YOUR OBJECTS. especially when you're potentially making a lot of them, as in this script. *
Lee was absolutely right to ask how to make this script parent the nulls and name the root. You simply can't have hundreds of objects, "Null1, NUll2 ......." kicking around in the root of your scene. This is just common sense even if you're not scripting. Sorry to bang on, but I see *so* many people just build flat, non-hierarchical models. It's sloppy and it'll slow you down more than you could possibly imagine. *
Jscript is easier to work with when your code starts getting longer.
Enjoy. __ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ___ ___ // nullifier v1.1 by Matt Lowery // // adapted to OM and JS by Kim Aldis, 15/4/05. // <http://www.cg-soup.com> www.cg-soup.com //////////////////////////////// var oNulls = Nullifier( Selection ); oNulls. posx = 123; function Nullifier( oSel ) {
// create the root null for our hierarchy of nulls. var oRootNull = ActiveProject.ActiveScene.Root.AddNull( "RootNull" ); // way fastest way of looping through a selection. for (var i = new Enumerator( oSel ); ! i.atEnd(); i.moveNext() ) { var oSourceObject = i.item(); // make a null, name it after this selection item. var oNull = oRootNull.AddNull( oSourceObject.name + "_Null" ); //copy the transform over from selection item to the new null oNull.kinematics.local.transform = f.item().kinematics.local.transform; } return oRootNull; }
|
|