Home Technical Talk

Maxscript: Set up a permanent face selection array

polycounter lvl 4
Offline / Send Message
Benjam polycounter lvl 4
Hello Polycounters,

I am in need of some assistance. I have a script that at the moment gathers the faces you have selected and stores them in an array so that you can cycle through the face selection array and randomly select faces from it.

The issue I am having is that at first run the array is set up fine, but when you go to select a face at random the array seems to be reset to only contain the one face even though the line that gathers the faces is only called at the very beginning?

I am not sure why this is happening as I am storing the faces at the very beginning or is the array dependent on what is selected at that point in time?
fn gatherInfo = 
	(
		for i in selection do
		(
			obj = i
		)			
 		faceList = obj.selectedFaces
	)

	fn pickFace =
	(
		select faceList[random 1 faceList.count]
		
		faceVerts = polyop.getVertsUsingFace obj faceList  
		vertPos= for v in faceVerts collect(polyop.getVert obj v) 
	)

Replies

  • Swordslayer
    Options
    Offline / Send Message
    Swordslayer interpolator
    Actually, obj.selectedFaces is a live set, it changes when the selection changes. There are more issues with the code, though, first of all the gatherInfo function is equivalent to selection[selection.count].selectedFaces, pickFace function doesn't follow the single responsibility principle (and its name) and apart from selecting a random face it collects and returns positions of the face vertices - and you probably should pass the faceList to the functions and not declare it in the outer scope (hope it's not actually global scope btw. :) ).

    So to answer your question, you'd have to cast the obj.selectedFaces to array or bitarray first.
  • McGreed
    Options
    Offline / Send Message
    McGreed polycounter lvl 15
    I'm not sure if it works with that, but arrays works differently when you use =, for example Arr1 = Arr2 will not copy the values but link those two arrays together, so if one change, the other change as well, which can be very confusing. You can use Arr1 = copy Arr2 to make those arrays unique, and maybe you can do the same with your issue.
  • Benjam
    Options
    Offline / Send Message
    Benjam polycounter lvl 4
    Thanks for the idea McGreed, I ended up storing the index of the faces in a permanent array and using setfaceselection and getting all my information from the face that way :)

    Thanks for the heads up Swordslayer, this is purely the base of the tool I am working at at the moment and I was trying to get a couple of things working before moving on. The function is indeed suppose to return the positions of the verts and such :)
    fn gatherInfo = 
    (
    	for i in selection do
    	(
    		obj = i
    	)			
    	faceListBitArr = for i in obj.selectedFaces collect i.index
    	faceList = faceListBitArr as array
    )
    
    -- Selects a random face from the array
    fn pickFace listOfFaces =
    (
    			
    	polyop.setfaceselection obj (faceList[random 1 listOfFaces.count])
    	mySelectedFace = obj.selectedFaces
    			
    	faceVerts = polyop.getVertsUsingFace  obj mySelectedFace
    	vertPos = for v in faceVerts collect (polyop.getVert obj v)
    )
    
Sign In or Register to comment.