fastest way through script to get all parameters in a scene of certain value?

128 views
Skip to first unread message

Enoch Ihde

unread,
Feb 27, 2013, 10:54:03 PM2/27/13
to softimage
these are the three methods i've tried.
using tags is the fastest, but a little prohibitive, as you have to have anticipated needing to get a param quickly, so it must be tagged in advance.  also, even with getting by tags, performance imo is pretty poor.

results on my machine from the code below:
# INFO : object count is 3618
# INFO : 0.788785183814
# INFO : tagged param count is 3618
# INFO : 2.12563553863
# INFO : 7.84713397563


is there a better way?
<snip>

import time
import win32com.client
xsi    = win32com.client.Dispatch( "XSI.Application" ).Application
xsiPrint = xsi.LogMessage
from win32com.client     import constants as c
items = xsi.ActiveSceneRoot.FindChildren2()

##### this bit is really slow
for item in items:
    xsi.Tag(item.fullname + '.visibility.viewvis', c.siTag1)
xsiPrint('object count is ' + str(items.count))
#### and only needs to be run once

t = time.clock()
val = xsi.ActiveSceneRoot.TaggedParameters(c.siTag1, False)
for v in val:
    someStuff = v.value
xsiPrint(time.clock() - t)

xsiPrint('tagged param count is ' + str(val.count))

t = time.clock()
for obj in items:
    vis = obj.GetPropertyFromName2("Visibility")
    v = vis.viewvis.value
xsiPrint(time.clock() - t)


t = time.clock()
for obj in items:
    vis = obj.Properties('visibility').Parameters('viewvis').value   
xsiPrint(time.clock() - t)

</snip>

tia,
enoch



Enoch Ihde

unread,
Feb 28, 2013, 12:54:42 AM2/28/13
to softimage

better stated: certain parameter name, not value, apologies, running behind on sleep, words are challenging.

Matt Lind

unread,
Feb 28, 2013, 1:29:55 AM2/28/13
to soft...@listproc.autodesk.com

The slowness is coming from using the Tag() command.  Try using the object model to cut out the middle man:

 

// Jscript

main();

 

function main()

{

                var oItems = ActiveSceneRoot.FindChildren2();

               

                LogMessage( oItems.Count );

               

                for ( var i = oItems.Count - 1; i >= 0; i--) {

               

                                var oItem       = oItems(i);

                                var oParameter  = oItem.Properties( "Visibility" ).Parameters( "viewvis" );

                                oParameter.Tags = siTagNone;

                }

               

                return;

}

 

 

If the parameters you are attempting to modify have a large degree of consistency in name and location, you can use some of Softimage’s wildcards to find them faster:

 

var oParameters = XSIFactory.CreateActiveXObject( “XSI.Collection” );

oParameters.Unique = true;

 

                // Get viewvis and rendvis parameters of the visibility property on each X3DObject

oParameters.items = “*.visibility.{viewvis,rendvis}”;

 

LogMessage( oParameters.Count );

 

If there is a large degree of consistency in the parameter fullname, you can apply values to the parameters en masse very efficiently:

 

                SetValue( “*.visibility.{viewvis,rendvis}", false );

 

 

Matt

Enoch Ihde

unread,
Feb 28, 2013, 12:59:41 PM2/28/13
to softimage
sorry, i wasn't really clear.  i'm aware the tagging bit was slow, i couldn't be bothered to tag it object modelwise.

i'm referring only to the value retrieval of the parameter.  not the setup time.


again:
# INFO : 0.788785183814 seconds <--- accessing 3618 parameters's values by tag

# INFO : 2.12563553863 seconds  <--- accesssing 3618 parameters's values by GetPropertyFromName2(propertyname).Parameters(paramname)

# INFO : 7.84713397563 <---- accessing 3618 parameter's values by Property(propertyname).Parameters(paramname)

tagging is by far the fastest, but in imo, is unacceptably slow.
compare that to a method such as IsSelected(), getting a report on a large number of objects is negligible.

Alan Fregtman

unread,
Feb 28, 2013, 1:20:09 PM2/28/13
to XSI Mailing List
What if you use list comprehensions?
coll = XSIFactory.CreateObject('XSI.Collection')
coll.SetAsText('*.visibility.*')
valueDict = dict([(x.FullName,x.Value) for x in coll])
print valueDict

Steven Caron

unread,
Feb 28, 2013, 4:46:47 PM2/28/13
to soft...@listproc.autodesk.com
ya, geeze alan... what were you thinking!


On Thu, Feb 28, 2013 at 1:43 PM, Alan Fregtman <alan.f...@gmail.com> wrote:
Actually, it'll be faster if we change the [] to a () so it's a generator instead of a full loop. My bad. I need coffee.
coll = XSIFactory.CreateObject('XSI.Collection') coll.SetAsText('*.visibility.*')
valueDict = dict(((x.FullName,x.Value) for x in coll))
print valueDict

Matt Lind

unread,
Feb 28, 2013, 4:48:35 PM2/28/13
to soft...@listproc.autodesk.com

Retrieving the value is the same.  If you use the object model you’ll have direct access to the parameter whether it’s the tag or the value.  Just switch “oParameter.Tags” with “oParameter.value” in my example and you have your answer.

 

If you’re looking to retrieve a specific value, or parameters with a common value, then you have no choice but to loop through all of them and do comparisons to find what you’re looking for.  In your case using the other notation with wildcards is only useful if you’re setting a set of parameters to a common value.

Alan Fregtman

unread,
Feb 28, 2013, 4:43:18 PM2/28/13
to XSI Mailing List
Actually, it'll be faster if we change the [] to a () so it's a generator instead of a full loop. My bad. I need coffee.

coll = XSIFactory.CreateObject('XSI.Collection')
coll.SetAsText('*.visibility.*')
valueDict = dict(((x.FullName,x.Value) for x in coll))
print valueDict


On Thu, Feb 28, 2013 at 1:20 PM, Alan Fregtman <alan.f...@gmail.com> wrote:

jo benayoun

unread,
Feb 28, 2013, 5:48:56 PM2/28/13
to soft...@listproc.autodesk.com
Hey Enoch,
unless I missed something, the way you're doing is already the fastest on my machine...


for reference:

# INFO : tag operation1 (tag): 2.13486326588
# INFO : tag operation2 (tag+): 18.2174550721
# INFO : tag operation3 (OM): 18.2086833524
# INFO : get operation1 (tagparams): 0.452397979784 4204
# INFO : get operation2 (xsicoll): 7.46562998296 4204


import time
from win32com.client import constants
class Softimage:
    __getattr__ = lambda x, a: getattr(Application, a, False) or getattr(constants, a)
softimage = Softimage()
softimage.factory = XSIFactory
softimage.message = lambda x, *args: softimage.LogMessage(str(x).format(*args))
softimage.SetUserPref("SCR_CMDLOG_ENABLED", False)
items = softimage.ActiveSceneRoot.FindChildren2()


t = time.clock()
parameters = ["{0}.visibility.viewvis".format(x) for x in items]
softimage.Tag(parameters, softimage.siTag1)
softimage.message("tag operation1 (tag): {0}", time.clock() - t)

t = time.clock()
for item in items:
    softimage.Tag(item.FullName + '.visibility.viewvis', softimage.siTag1)
softimage.message("tag operation2 (tag+): {0}", time.clock() - t)

t = time.clock()
tag1 = softimage.siTag1
parameters = (x.Properties("visibility").Parameters("viewvis") for x in items)
for parameter in parameters:
    parameter.Tags |= tag1
softimage.message("tag operation3 (OM): {0}", time.clock() - t)

t = time.clock()
params = softimage.ActiveSceneRoot.TaggedParameters(softimage.siTag1)
softimage.message("get operation1 (tagparams): {0} {1}", time.clock() - t, params.Count)

t = time.clock()
parameters = softimage.factory.CreateObject("XSI.Collection")
parameters.SetAsText("*.visibility.viewvis")
parameters = [x for x in parameters if x.Tags & softimage.siTag1]
softimage.message("get operation2 (xsicoll): {0} {1}", time.clock() - t, len(parameters))

parameters = (x.Properties("visibility").Parameters("viewvis") for x in items)
for param in parameters:
    param.Tags &= ~softimage.siTag1







2013/2/28 Matt Lind <ml...@carbinestudios.com>

Matt Lind

unread,
Feb 28, 2013, 5:53:15 PM2/28/13
to soft...@listproc.autodesk.com

Interesting how much choice of language affects performance.

 

In your tests the Object model has the same runtime as calling commands. On my computer using Jscript, the object model is at least 4x faster than calling Tag() command.  

jo benayoun

unread,
Feb 28, 2013, 6:00:08 PM2/28/13
to soft...@listproc.autodesk.com
I suspect it to be the runtime dispatching and attribute access (dictionary lookups) cost...
Tho the Tag command allow us to pass a collection instead of giving sequentially items, this resulting in a great improvement.
-- jon




2013/2/28 Matt Lind <ml...@carbinestudios.com>
Reply all
Reply to author
Forward
0 new messages