Home Technical Talk

MaxScript Sub Object Rotation/Scale

Hello!
For the past 2 weeks, I have been trying to make a random tool for rotating and scaling my selection in the sub object level.

I manage do make it work for translation but I can't seem to figure out how the rotation or the scaling work for the sub objects.

To bad I can't just change the move to rotate or scale.

Ex: of the move
move $.selectedVerts [XRandom,YRandom,ZRandom]
to
rotate $.selectedVerts [XRandom,YRandom,ZRandom]

Anyone know how to do it?
I would really appreciate the help!

Replies

  • monster
    Options
    Offline / Send Message
    monster polycounter
    The rotate and scale command don't work on vertices. If you think about it, verts don't have a rotation/scale property, only a position property. So instead of rotating you need to calculate the rotation/scale and then set the position based on that.

    This code below will move/rotate/scale the verts. There is comments in the code. Let me know if you have any questions.
    (
    	fn transformSubObject obj vertlist subMove:[0,0,0] subRotate:[0,0,0] subScale:[0,0,0] =
    	(
    		--Cache poly commands
    		getVertex = polyOp.getVert
    		setVertex = polyOp.setVert
    		
    		--Create a transform matrix from the parameters
    		translateMatrix = transMatrix subMove * ((eulerAngles subRotate.x subRotate.y subRotate.z) as matrix3) * scaleMatrix subScale
    		
    		--Move the transform matrix to the object's transform
    		translateMatrix *= obj.transform
    		 
    		--Move each vertex according to the transform matrix
    		for v in vertlist do 
    		(
    			setVertex obj v ((in coordsys obj (getVertex obj v)) * translateMatrix)
    		)
    
    		--Update the viewport
    		redrawViews()
    	)
    
    
    	transformSubObject $ (polyOp.getVertSelection $) subScale:[1.1,1.1,1.1]
    )
    
  • teckcloud
    Options
    Offline / Send Message
    Sorry for the late response.
    First I want to thank you for this code. It is really helping me a lot.
    The move and rotate are working very well.
    I am still havin't issue witht the scale.

    I have look at the code you wrote and there are some lines that I don't exactly know what they do.

    Here are the line:

    translateMatrix = transMatrix subMove * ((eulerAngles subRotate.x subRotate.y subRotate.z) as matrix3) * scaleMatrix subScale

    translateMatrix *= obj.transform

    setVertex obj v ((in coordsys obj (getVertex obj v)) * translateMatrix)

    If you have the time I would really appreciate more detail explaination on those line please.

    Thanks again for the help :)
  • monster
    Options
    Offline / Send Message
    monster polycounter
    translateMatrix = transMatrix subMove * ((eulerAngles subRotate.x subRotate.y subRotate.z) as matrix3) * scaleMatrix subScale
    

    This line builds the target transformation matrix from the input parameters. I do it all in one line to minimize code, and maximize confusion. :)

    A transfrom matrix is built from these components: position * rotation * scale

    In order to multiply them together I have to build a matrix from each component. Position and scale are easy.

    transMatrix subMove
    scaleMatrix subScale

    Rotation is a little tricky because I need to pipe the parameter though an eulerAngle before converting to a matrix.

    ((eulerAngles subRotate.x subRotate.y subRotate.z) as matrix3)

    A more readable version of this code would be:
    --NEW DEFAULT MATRIX3
    translateMatrix = matrix3 1
    
    --MOVE THE MATRIX
    translateMatrix *= transMatrix subMove
    
    --SCALE THE MATRIX
    translateMatrix *= scaleMatrix subScale
    
    --ROTATE THE MATRIX, I DON'T KNOW WHY rotateMatrix DOESN'T EXIST 
    translateMatrix *= ((eulerAngles subRotate.x subRotate.y subRotate.z) as matrix3)
    

    translateMatrix *= obj.transform
    
    This code is simple. It just translates the matrix above to the objects current position/rotation/scale. If you didn't do this your sub objects would rotate/scale from the world origin. This isn't ideal because they use the object's pivot to rotate and scale from. So if your pivot is not centered or you haven't ResetXforms then you may get weird behavior. But it works well enough once you know what's going on.

    More ideal would be to get the center of the sub-object selection, and use the world axis for rotation / scale.
    setVertex obj v ((in coordsys obj (getVertex obj v)) * translateMatrix)
    

    This line of code gets a vertex position relative to the current object (not the world). Then it translates the position of the vertex by the matrix we build above.
    I am still havin't issue witht the scale.
    

    Maybe try ResetXforms as described above.
Sign In or Register to comment.