Scripting help - PathCns - Dan 's attempt in Jscript.... 2005-06-02 - By Matt Lind
Back >The thing is that is does not cost more to enumerate, as you pointed out. >So why not using it?
[Matt] There are other concerns besides speed, such as legibility. Even though an Enumerator may be faster, I still use the traditional 'for' loop as it makes my code more readable which can be important when I have to re-visit a script I haven't seen in a while. A 'for' loop also has predictability as the increment goes in the order specified. The Enumerator is platform specific and a bit of a black box in my opinion. I tend to port functions of my code back n' forth to other languages, so having something simple like a 'for' loop simplifies matters without surprises. For example, if it runs in a script without error, then it will run in my shader without error too (all else being the same, that is). When something fails, I'd like the reason to be readily visible.
To be completely honest, I've written many tools doing heavy iteration (subcomponent traversing, for example) and I have yet to run into an instance where an Enumerator made such a difference over a 'for' loop that I had to consider a switch.
>The reason is that if you want to do things the proper way, you may put >this little operation >into a function. Then you will be able to easily reuse that function in >other scripts. Now if those other scripts are performance sensitive, you'll >just be glad you used the fastest loop code. Also, sooner or later, you or >someone using your oringal script may come up with some design ideas, that >will make your script do a lot more things. So any performance gain >whatosever is welcomed, well, most of the time!
[Matt] I wouldn't call wrapping everything in functions the "proper" way. I think that's dipping into the territory of user preference or execution context. Using inline code doesn't make your script improper.
I would also suggest not sticking so many statements in the 'try' portion of your 'try/catch' block as well. Ideally you want to only place statements in the 'try' block that are likely to throw errors that can halt program flow as the 'try' block has to test every statement in the block for success beyond what is normally done in the rest of the script. Anything more will slow down execution unneccesarily. You may want to simplify your block like this:
-- ---- ---- ---- -- try { InspectObj( oProp, null, "", siModal ); } catch(e) { // User cancelled or error occurred - clean up. DeleteObj( oProp ); return( null ); }
// retrieve parameter value from dialog var nValue = oParam.value;
// Delete the dialog, it's no longer needed DeleteObj( oProp ); -- ---- ---- ---- ---- ----
If you don't like using 'try/catch', you can also set the fifth argument of InspectObj() to zero which tells XSI not to throw an error should InspectObj() fail or be cancelled.
Matt
-- ---- ---- ---- ---- ---- -- Matt Lind Animator / Technical Director SOFTIMAGE certified instructor: SOFTIMAGE|3D SOFTIMAGE|XSI Matt(at)Mantom.net
Re: Scripting help - PathCns - Dan's attempt in Jscript.... Date : Thu, 02 Jun 2005 19:55:29 -0400 To : XSI(at)Softimage.COM >From : Bernard Lebel <3dbernard(at)gmail.com> Subject : Re: Scripting help - PathCns - Dan's attempt in Jscript....
No takers?
Dan Yargici wrote:
[...]
Anyway, if some of the more seasoned scripting types could cast a beady eye over this I'd really appreciate it. I did some things I'm just not sure about. First off, I iterate through the collection retuned by the duplicate command instead of enumerating it. Now I remember reading here many times that enumerating is faster, but it just seemed unecessary in this instance, am I right?
The thing is that is does not cost more to enumerate, as you pointed out. So why not using it?
The reason is that if you want to do things the proper way, you may put this little operation into a function. Then you will be able to easily reuse that function in other scripts. Now if those other scripts are performance sensitive, you'll just be glad you used the fastest loop code. Also, sooner or later, you or someone using your oringal script may come up with some design ideas, that will make your script do a lot more things. So any performance gain whatosever is welcomed, well, most of the time!
Also, I'm checking for whether the user pressed OK or Cancel by just checking if a return value exists. Is this a no no? It's seems fairly fool-proof to me.
This is also what I do, and am very happy that way. I use the technique shown by Michael Isner in Experience XSI 4, that is, a try/catch statement, that I put in a function. Then if the function returned nothing, the script stops and print a message telling why ("cancelled by user").
// Call function var nValue = askUser();
// Test return value if( nValue == null ) { logmessage( "cancelled by user.", siError ); } else { logmessage( nValue ); // do something... }
function askUser() { var oProp = activesceneroot.addproperty( "CustomProperty", false, "TellMe" ); var oParam = oProp.addparameter3( "Value", siInt2, 1, 0, 4, true, false ); try { // Here's the important thing: the raise error being true, // if the user cancels, the script will go to the catch statement inspectobj( oProp, "", "TellMe", siModal, true ); nValue = oParam.value; deleteobj( oProp ); return( nValue ); } catch(a) { deleteobj( oProp ); return( null ); } }
--- Unsubscribe? Mail Majordomo@(protected) with the following text in body: unsubscribe xsi
|
|