PDA

View Full Version : Get seleted face, as integer?


Gibbz
11-02-2005, 12:03 AM
I need to get the selected face as just a number.

Basically i have

mySelection = $.getselection #Face
which gives something similar to '#{876}' basically i need to get the number, not the #{} bits.

Or alternativley i need to get materialID of selected face as an integer
myMatIDCopy = polyop.getFaceMatID x x
Ive been using that, but no luck with the above hence this doesnt work...

Thanks

stuh505
11-02-2005, 02:12 AM
this should work

(($.getSelection #face) as array)[1]

Gibbz
11-02-2005, 09:43 AM
ok swwet, thanks!

j-man
11-02-2005, 10:44 AM
Hi Gibbz and Stuh505,

This is effective on both Epoly and EMesh (I know you're using a poly) so you might find it a little more versatile. Notice also that I have replaced $ with 'obj=selection[1]' since $ can be a bit risky to use in a script.


obj=selection[1]
obj.selectedfaces[1].index

And this to make an array of selected faces:

obj=selection[1]
f=for i in obj.selectedfaces collect i.index


Hope this is sueful!

Josh.

Gibbz
11-02-2005, 11:04 AM
why is $ risky? ive noticed somtimes it doesnt pickup the selected faces, is that what you mean by risky?

Also your script works but im not under standing whats happening, what do the square brakets mean?

Also im using alot of $ in my script
for example
" $.EditablePoly.SetSelection #Face mySelection --select original selection "

is there a way to replace the $ on stuff like that?

Thanks

j-man
11-02-2005, 11:55 AM
why is $ risky? ive noticed somtimes it doesnt pickup the selected faces, is that what you mean by risky?

From the maxhelp:


You can use a shorthand form for specifying the currently selected object or objects with the pathname syntax. A lone "$" character refers to either the currently selected object if a single object is selected, the current selection set as an ObjectSet if more than one object is selected, or undefined if no objects are selected. This is a useful shorthand for use in the Listener when working on selections.

...

Inside a script, it is better to use the selection ObjectSet rather than $. selection always contains an ObjectSet regardless of the number of selected objects. Where hide $ will generate an error if no objects are selected (because $ contains the value undefined), hide selection will not generate an error. Due to the way that 3ds max internally processes notification signals, the $ form of accessing selected objects is not recommended in a select change handler. To access the selected objects, you should use the selection ObjectSet. This is because $ relies on information that has not yet been set in the selection processing whereas selection uses a different method of accessing selections and the information it uses has been set up.



This is without mentioning that the objects within $ might change at any moment.

Also your script works but im not under standing whats happening, what do the square brakets mean?

'obj.selectedfaces' refers to the selection of the faces within the mesh. This is a dynamic virtual array and the number in square brackets tell max that I want the first face in that array. Read in the maxhelp under FaceSelection Values.

Also im using alot of $ in my script
for example
" $.EditablePoly.SetSelection #Face mySelection --select original selection "

is there a way to replace the $ on stuff like that?

Thanks

You would want one of the first few lines in your code to be something like:

obj=selection[1]

which will be the first object in the selection set. Now no matter what happens to the selectionset, we will always be operating on this object. If we wanted to process every object in the selection set one after the other we would use this:

obj=selection as array
for o in obj do
(
print o.name -- and my other code here
)


To change what you've written already, simply do a search and replace CTRL+H, search for $. and replace it with obj.

Cheers,

Josh.


)

Gibbz
11-03-2005, 10:29 AM
EDIT


ok does this code look safer to use for a selection?

myObj = selection[1] --get currently selected polys
myTempSelection = for i in myObj.selectedfaces collect i.index
mySelection = myTempselection as bitarray

j-man
11-03-2005, 11:13 AM
ok still having issues...

ive added in

myObj = selection[1] --get currently selected polys
mySelection = for i in myObj.selectedfaces collect i.index

but when i need to use "mySelection" i cannot use it as it has a difrent format to $.

eg
myObj = selection[1] --get currently selected polys
mySelection = for i in myObj.selectedfaces collect i.index
gives
#(10, 11)

mySelection = $.getselection #Face
gives
#{10..11}

So how do i use the first and get into the other format to use it?

ok.

There are a couple of things you're missing.

#(1, 2, 3, 5) -- this is an array of numbers
#{1..3, 5} -- this is a bitarray of the same numbers.

#(1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 50) -- this is an array of numbers
#{1..3, 5..13, 50} -- this is a bitarray of the same numbers.

Bitarrays are smaller and faster than arrays, but a lilttle harder to handle. .getselection returns a bitarray. The code I gave you builds an array of face numbers.


y=#{1..3} as array
#(1, 2, 3)
y[1]
1

x=#(1, 2, 3) as bitarray
#{1..3}
x[1]
true



Now I don't know what it is that you're trying to do, but it seems you want to get and set face material ID's.

consider this:


myObj = selection[1] --get currently selected object
fce=for i in myObj.selectedfaces collect i.index -- get the objects selected faces and build an array
for f in fce do print (polyop.getFaceMatID myObj f) -- print each of the selected faces mat ID
fcebit=fce as bitarray
for f in fcebit do print (polyop.getFaceMatID myObj f) -- print each of the selected faces mat


polyop.setfacematid will accept either a bitarray or an array.
polyop.getfacematid needs an implicit face number

I would suggest reading about arrays and bitarrays in the MXShelp.

Does this help?

Josh.

Gibbz
11-03-2005, 11:20 AM
yyeah i jus tfound the array/bit array thing :)

Thanks

CGTalk Moderation
11-03-2005, 11:20 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.