PDA

View Full Version : Quick Weight Maps question


ExP
05-16-2008, 02:29 AM
Just getting a first taste of XSI. I'm playing around with dynamic Weight Maps etc..
Quick question: Is there a way to select components, like polys for example, from a Weight Map? Let's say I have an object with a weigh map, how do I select the equivalent polys the weigh map touches? I looked around a lot, but coun't find an answer.

Thanks.

Strang
05-16-2008, 04:01 AM
i believe you need to script this, select a mesh with a weightmap, and run this in the script editor. it should only use the first weightmap it finds. the threshold argument allows you to decide the smallest weight value to be selected.

#python
#imports
import win32com.client

#globals
xsi = win32com.client.Dispatch( "XSI.Application" ).Application
xsiPrint = xsi.LogMessage

def SelectCompsFromWeightMap(mesh,threshold = 0.9):
if mesh.type == "polymsh":
clusters = mesh.activeprimitive.geometry.clusters
weightmap = None
for cls in clusters:
for prop in cls.properties:
if prop.type == "wtmap":
weightmap = prop
break

elements = weightmap.elements
comps = []
for i in range(elements.count):
if elements[i][0] > threshold:
comps.append(i)

xsi.selectgeometrycomponents(mesh.name + ".pnt" + str(comps))
else:
xsiPrint("select a mesh!")

sel = xsi.selection(0)

SelectCompsFromWeightMap(sel,0.9)

ExP
05-16-2008, 05:07 AM
Strang, thanks a lot. I tried this, but it's giving me an error. Could it be because I'm running XSI64 using python64? I've had similar problems with python scripts before.


# ERROR : Traceback (most recent call last):
# File "<Script Block >", line 31, in ?
# SelectCompsFromWeightMap(sel,0.9)
# File "<Script Block >", line 21, in SelectCompsFromWeightMap
# for i in range(elements.count):
# File "C:\Python24\Lib\site-packages\win32com\client\__init__.py", line 454, in __getattr__
# raise AttributeError, "'%s' object has no attribute '%s'" % (repr(self), attr)
# AttributeError: '<win32com.gen_py.Softimage|XSI Object Model Library v1.5.ClusterElementCollection instance at 0x206121864>' object has no attribute 'count'
# - [line 21]


If that's the case do you happen to have the same script compiled in another language?

Cheers

Strang
05-20-2008, 10:30 PM
i think you need to use this python hack...

http://www.xsi-blog.com/archives/24

let me know how it goes

ExP
05-20-2008, 11:30 PM
Works great!! (I wonder why XSI needs that hack though)

Thanks man! This is truly a time saver for a few things I was trying to achieve.

All the best.

ThE_JacO
05-20-2008, 11:33 PM
Not a huge fan of the hack to be honest, it can have other unpredictable effects.

Since I'm bored, here's the same script with a couple tweaks.
Shouldn't require any hacks, but it's untested and still far from ideal (edited in the glorious post text field :D, sorry but am on FC8 and don't feel like rebooting just to tweak a script)
It should work bar some potential tard out on syntax.


#python
#imports
import win32com
from win32com.client import dynamic as D
DD = D.Dispatch

#globals
xsi = DD(Application)
xsiPrint = xsi.LogMessage

def SelectCompsFromWeightMap(oMesh,threshold):
if oMesh.type == "polymsh":
weightmap = None
for eachCls in oMesh.ActivePrimitive.Geometry.Clusters:
for eachProp in eachCls.properties:
if eachProp.Type == "wtmap":
weightmap = eachProp

aElements = weightmap.Elements.Array
aComponents = []
i = 0
for eachValue in aElements[0]:
if eachValue > threshold:
aComponents.append(i)
i += 1

if len(aComponents):
xsi.selectgeometrycomponents(oMesh.Name + ".pnt" + str(aComponents))
else:
xsiPrint("select a mesh!")

sel = xsi.selection(0)
SelectCompsFromWeightMap(sel,0.0)

ThE_JacO
05-20-2008, 11:40 PM
Works great!! (I wonder why XSI needs that hack though)

Thanks man! This is truly a time saver for a few things I was trying to achieve.

All the best.

The hack is related to how some calls are dispatched.
Some of XSI's python wrapping can erroneously deal with some calls and might occasionally need to be forcefully made to dispatch a different way.

The hack enforces it across the board, but it's not needed, only a handful of things need to be dealt with that way, and those few can be forcefully dispatched through the pywin32 client.

I still have to find a single tool I wrote or revised that couldn't be made to run without the hack by simply adopting a clearer and more structured design that takes care of potential dispatching issues far upstream, it's only disadvantageous for minuscule scripts where the added verbosity might be annoying.

ExP
05-21-2008, 01:27 AM
JacO,

Thanks for the info. Is there any downside to the hack? Meaning, once done is there any reason why you would want to "reactivate" the WrapDispatch function?

ThE_JacO
05-21-2008, 01:49 AM
In the past it happened that we had some problems coming from that hack, especially under Linux, and more recently I had some of the weirdest shit happening at a windows based studio that only had one difference from my test setup at home, which was the hack (they had it, I didn't), which leads me to think it's not always and entirely harmless.
We had no time to revise their codebase to remove it though, so I went the long way around in some regards and made sure things were working in both environments.

It targets libraries that, most likely, you will only be running for xsi as not many people or apps use pyWin in this industry as activeX isn't that prominent in it, but if by chance you needed pywin for something else it might also interfer with that. That's a pretty remote chance though outside of the network/IT/office automation business.

The above is largely conjecture and based on a too small sample base to make it relevant statistics, but I've hardly ever seen the hacked platforms behaving EXACTLY as expected all the time, so I'm diffident of forcing off a particular type of wrapping that might be needed somewhere.

ExP
05-21-2008, 02:31 AM
Nice. Definitely makes sense. I'll try and set the init.py file back to its original state and test your new script... see what happens. Especially considering I'm on an XP x64 machine running XSI 64bit
Thanks again for all the insights.

CGTalk Moderation
05-21-2008, 02:31 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.