|
|
|
Tell us what you think of the site.
|
Autodesk Media & Entertainment User Community
|
Autodesk® 3ds Max®
|
|
Autodesk® Maya®
|
|
Autodesk® Softimage®
|
|
Autodesk® MotionBuilder®
|
|
Autodesk® Mudbox™
|
|
Autodesk® ImageModeler™
|
|
Autodesk® Sketchbook® Pro
|
|
Autodesk® Smoke on Mac®
|
| Adding components to selection using Object Model
|
|
|
A beginner question…
I wanted to write a quick script to convert my poly selection to edge selection (just the outline edges), something i’ve used in Modo and was not able to find in XSI.
So I wrote this in Python. I could get the current selection using the Object Model but then I add to switch back to using command to actually do the selection. I could not figure out what would be the way to add components to the selection using objects. I wonder if someone could give me some hint?
Thanks!
Claude
BTW here is the code:
[FONT=Courier New]edgeCount = {}
for poly in Application.Selection[0].SubComponent.ComponentCollection:
__for edge in poly.Edges:
____currentCount = edgeCount.get( edge.Index, 0 )
____currentCount = currentCount + 1
____edgeCount[ edge.Index ] = currentCount
itemSel = “”;
for item in edgeCount.items():
__if ( item[ 1 ] == 1 ):
____if ( len( itemSel) > 0 ):
______itemSel = itemSel + “, “
____itemSel = itemSel + repr( item[ 0 ] )
# should use Object Model rather than Command, but how?
Application.AddToSelection( Application.Selection[0].Name + “.edge[” + itemSel + “]” )
Application.SetSelFilter("Edge")
[/FONT]
|
|
|
|
there are several ways to do this, no one way is the blanket solution over another.
To add to an existing item to the selection use:
Selection.Add( oItem );
this assumes the added item is an object of the same class as the items already in the selection. That is, if the selection contains X3Dobjects, the added item is also an X3Dobject. If the selection contains edges, then the added item should be an edge. XSI is forgiving in certain situations, so this isn’t a hard rule. But it’s best to match added types with the selection types.
Another approach is to get an XSI Collection, populate it with your edges, then pass the collection to the selection object:
var oCollection = XSIFactory.CreateActiveXObject( "XSI.Collection" ); oCollection.Unique = true;
oCollection.Add( oEdge );
(or) oCollection.AddItems( oEdges );
Selection.Add( oCollection );
(or) Selection.SetAsText( oCollection );
Another solution:
oCollection.Items = "objectname.edge[12],objectname.edge[15-19]";
if ( oCollection.Count > 0 ) {
Selection.Add( oCollection ); }
the benefit to this method is oCollection will not throw an error if .Items evaluates to nothing. More useful for objects/models than components.
If working with components specifically, you might be better off using SelectGeometryComponents() command. Build your list of edges using an XSI collection and/or filtering commands, then pass the collection to the SelectGeometryComponents command. This is faster than recursively updating the selection with each new edge you encounter.
matt
|
|
|
|
Thanks Matt!
So I played with it, I can just indeed add each edge as I find it using Application.Selection.Add( edge ). Works great.
Now for the more efficient way to add them all in one go, I instantiate an XSI Collection, puts all the edges in there and add the collection to the selection if not empty. But I have an error:
coll = win32com.client.Dispatch( “XSI.Collection” )
...
Application.Selection.Add( coll )
Error:
# File “<COMObject <unknown>>”, line 3, in Add
# COM Error: Interface not supported. - [line 15]
Maybe more a Python issue than anything else…
Claude
|
|
|
|
Oops, my fault.
Selection.Add() expects a single object. Attempting to add an XSI Collection is like adding multiple objects in one shot. In this case you’ll probably need to use Selection.SetAsText(), or the SelectGeometryComponents() command.
sorry,
Matt
|
|
|
|
All right, that explains it then. Thanks for all the help!
Claude
|
|
|
|
|
|