  | | | checking that an object exists | checking that an object exists 2005-04-07 - By Brad Friedman
Back Thanks Brad.
I'm going to get Clay on this e-mail list so you don't have to keep answering the same questions multiple times :)
-brad
Bradley R. Gabe wrote:
> You can always try to populate an XSI collection using the items > property set to a string. If the objects don't exist in the scene, the > collection count will remain 0. Otherwise, it will fill up with the > items that match the string input. This is very fast and never raises > errors: > > // Create an empty XSI collection > var findColl = new ActiveXObject('XSI.Collection'); > > // Find the object from the name > findColl.Items = 'objectName'; > > // Test the collection count > if(findColl.count>0){ > var obj = findColl(0); > LogMessage(obj); > } > // -- ---- ---- ---- ---- ---- ---- ---- ------ > > NOTE: This is the same way the Selection object gets populated when > you type a string in the text selection box, so you can always test > your strings before you put them into your code. Also, wildcard > selections will work with this method, so you could use it to quickly > grab all the nodes in a specific model space: > > // Create an empty XSI collection > var modelColl = new ActiveXObject('XSI.Collection'); > > // Get the name of the current selected model > var modelName = selection(0).name; > > // Get all the nodes in the model space > modelColl.Items = modelName + '.*'; > // -- ---- ---- ---- ---- ---- ---- ---- ------ > > You can also use this technique to fill a collection with multiple > parameters or properties. I like to use an Array because it reads > clearly, requires less string concatenation, and is easy to add or > remove items from: > > // Create an empty XSI collection > var visColl = new ActiveXObject('XSI.Collection'); > > // Get the visibility parameters of the currently selected obj > var obj = selection(0); > > visColl.Items = [ > obj + '.visibility.viewvis', > obj + '.visibility.rendvis', > obj + '.visibility.selectability' > ]; > > // Get pointers to the visibility params > var viewvis = visColl(0); > var rendvis = visColl(1); > var selectability = visColl(2); > // -- ---- ---- ---- ---- ---- ---- ---- ------ > > If you're interested in more techniques for getting object pointers > and speed comparisons, check out this link: > http://www.cg-soup.com/archives/2005/02/loopin_lizards.html > > > -Brad > CG Soup > >> Hi, >> >> I got an interesting question from someone, that I wanted to check >> out with you all. >> >> Given an object's full name, he wants to check if it exists. >> >> So there are two ways to do it. >> >> Via command: GetValue() >> >> Via object model: Dictionary.GetObject() >> >> now here's the question: >> >> Both of these solutions throw an exception if the given object does >> not exist. While you can catch the exception, I'd think this is a >> poor way to go about solving the problem. Exceptions should not be >> used for flow control. They're inherently slow (or so I've been >> taught time and time again). Is there a better fast way to check if >> an object exists from its full name that doesn't involve depending on >> exceptions? I can come up with some ways of doing it that will be >> inherently slow... but none that are fast. >> >> -brad >> --- >> Unsubscribe? Mail Majordomo@(protected) with the following text in >> body: >> unsubscribe xsi >
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html;charset=ISO-8859 (See http://ISO-8859.ora-code.com)-1" http-equiv="Content-Type"> </head> <body bgcolor="#ffffff" text="#000000"> Thanks Brad.<br> <br> I'm going to get Clay on this e-mail list so you don't have to keep answering the same questions multiple times :)<br> <br> -brad<br> <br> Bradley R. Gabe wrote: <blockquote cite="mid6.1.2.0.0.20050407115323.0386bc10@(protected)" type="cite">You can always try to populate an XSI collection using the items property set to a string. If the objects don't exist in the scene, the collection count will remain 0. Otherwise, it will fill up with the items that match the string input. This is very fast and never raises errors:<br> <br> <font color="#008000" face="Courier New, Courier">// Create an empty XSI collection<br> </font><font face="Courier New, Courier">var findColl = new ActiveXObject('XSI.Collection');<br> <br> </font><font color="#008000" face="Courier New, Courier">// Find the object from the name<br> </font><font face="Courier New, Courier">findColl.Items = 'objectName';<br> <br> </font><font color="#008000" face="Courier New, Courier">// Test the collection count<br> </font><font face="Courier New, Courier">if(findColl.count>0){<br> var obj = findColl(0);<br> LogMessage(obj);<br> }<br> </font><font color="#008000" face="Courier New, Courier">// -- ---- ---- ---- ---- ---- ---- ---- ------<br> <br> </font>NOTE: This is the same way the Selection object gets populated when you type a string in the text selection box, so you can always test your strings before you put them into your code. Also, wildcard selections will work with this method, so you could use it to quickly grab all the nodes in a specific model space:<br> <br> <font color="#008000" face="Courier New, Courier">// Create an empty XSI collection<br> </font><font face="Courier New, Courier">var modelColl = new ActiveXObject('XSI.Collection');<br> <br> </font><font color="#008000" face="Courier New, Courier">// Get the name of the current selected model<br> </font><font face="Courier New, Courier">var modelName = selection(0).name;<br> <br> </font><font color="#008000" face="Courier New, Courier">// Get all the nodes in the model space<br> </font><font face="Courier New, Courier">modelColl.Items = modelName + '.*';<br> </font><font color="#008000" face="Courier New, Courier">// -- ---- ---- ---- ---- ---- ---- ---- ------<br> <br> </font>You can also use this technique to fill a collection with multiple parameters or properties. I like to use an Array because it reads clearly, requires less string concatenation, and is easy to add or remove items from:<br> <br> <font color="#008000" face="Courier New, Courier">// Create an empty XSI collection<br> </font><font face="Courier New, Courier">var visColl = new ActiveXObject('XSI.Collection');<br> <br> </font><font color="#008000" face="Courier New, Courier">// Get the visibility parameters of the currently selected obj<br> </font><font face="Courier New, Courier">var obj = selection(0);<br> <br> </font><font face="Courier, Courier">visColl.Items = [<br> obj + '.visibility.viewvis',<br> obj + '.visibility.rendvis',<br> obj + '.visibility.selectability'<br> ];<br> <br> </font><font color="#008000" face="Courier New, Courier">// Get pointers to the visibility params<br> </font><font face="Courier New, Courier">var viewvis = visColl(0);<br> var rendvis = visColl(1);<br> var selectability = visColl(2);<br> </font><font color="#008000" face="Courier New, Courier">// -- ---- ---- ---- ---- ---- ---- ---- ------<br> <br> </font>If you're interested in more techniques for getting object pointers and speed comparisons, check out this link:<br> <a href="http://www.cg-soup.com/archives/2005/02/loopin_lizards.html" eudora="autourl">http://www.cg-soup.com/archives/2005/02/loopin_lizards.html <br> <br> <br> </a>-Brad<br> CG Soup<br> <br> <blockquote type="cite" class="cite" cite="">Hi,<br> <br> I got an interesting question from someone, that I wanted to check out with you all.<br> <br> Given an object's full name, he wants to check if it exists.<br> <br> So there are two ways to do it.<br> <br> Via command: GetValue()<br> <br> Via object model: Dictionary.GetObject()<br> <br> now here's the question:<br> <br> Both of these solutions throw an exception if the given object does not exist. While you can catch the exception, I'd think this is a poor way to go about solving the problem. Exceptions should not be used for flow control. They're inherently slow (or so I've been taught time and time again). Is there a better fast way to check if an object exists from its full name that doesn't involve depending on exceptions? I can come up with some ways of doing it that will be inherently slow... but none that are fast.<br> <br> -brad<br> ---<br> Unsubscribe? Mail <a class="moz-txt-link-abbreviated" href="mailto:Majordomo @(protected)">Majordomo@(protected)</a> with the following text in body:<br> unsubscribe xsi<br> </blockquote> </blockquote> <br> </body> </html>
|
|
 |