Notifications
Clear all

[Closed] Script newbie needs help with moving object to snap to vertex

Dear all,

I am a newbie with max script (and newbie to this forum too)! Come across a project that needs some help with script…

The ‘pink’ plane is with noise modifier. Would like to duplicate the 3×3 boxes to a sea of boxes, and animate the boxes to move in the Z axis following the pink noise’ vertex.

Is it possible to generate keyframes to do that? Please help!

8 Replies

the task is pretty simple for a maxscripter… using intersectray find for every box a face projected to and its bary coordinates. after that assign an attachment controller as position controller for every box using the corresponding face and its bary as a key.
and bake animation after all

This will get you going:

surfaceToAttach = Plane width:140 length:150 lengthsegs:14 widthsegs:14 wirecolor:[140, 140, 90]
noiseMod = Noisemodifier()
noiseMod.strength.z = 90
noiseMod.animate = true
addModifier surfaceToAttach noiseMod
obj = Box width:5 length:5 height:15 wirecolor:blue

intersectPoint = intersectRayEx surfaceToAttach (ray obj.pos [0, 0, -1])

ctrl = Attachment()
obj.position.controller = ctrl
ctrl.node = surfaceToAttach
addNewKey ctrl 0f
ctrlKey = AttachCtrl.getKey ctrl 1

ctrlKey.face = intersectPoint[2]
ctrlKey.coord = intersectPoint[3]
-- Move one frame the time slider to see the position of the box to be updated

Thank you denisT and aca88! THAT is very very useful!

Some question though: I tried to use the Listener to mimic your script and make changes on the position key, things such as ‘Align To Surface’ radio button and ’ Position: Face: 0’ can’t seem to capture on Listener. How do I change the parameters in script?

You change parameters in the script using all
the available methods that MAXScript provides.
Check the Help File of Maxscript and write in
the search tab something like “attachment controller”
or “intersectRayEx”, or “noisemodifier” depending on
what you want to change. Then you can see what you
can change from the script and how.

Thanks very much for your kind support. I tried to follow some tutorial to learn .node and ctrl from basic but still can’t seem to figure out… here…

I am trying to:

  1. uncheck the radio button
  2. set position
  3. assign specific face

From the help file I could only find .align, followed that I don’t know how the syntax follows…

  1. I see. Align to surface is a boolean, so you can just add in my code snippet
    something like ctrl.align = true or false.

2.3. If you mean to set the position of the cube on a particular face index of
the underlying surface, then just move the cube where you want it
The script shoots a ray from the cube downwards to the surface. Hence the cube will
stay wherever the intersection point is found (the point where the ray and the surface
intersect).

As I see from the very first image, you want an array of boxes to lie on the surface.
So you just have to figure out how to make a loop of boxes and on each box apply the same logic that I showed you in the code snippet (which works on a single box).

Thank you aca88 for your patience From the very first image I wasn’t familiar how to do it in maxscript at all, and after reading tutorial a little bit from your sample the code generates the cube (which makes things easier too).

So I decided to hope to create a for-loop and change only the (3)face so I can create an array of cubes across the ‘noising’ plane with your code.

Thanks! Sorry I am really new to programming. ctrl.align = false works! The help stated the properties that I can modify: <MAXAKey>.face : Integer (0-based) <- is this the code I should use to set the position of the cube? What does it means by ‘MAXAKey’? Sorry for some really newbie question for script

No worries. I used to ask a lot in the beginning, as I do now.
You is always something to learn.

Try this…

surfaceToAttach = Plane width:140 length:150 lengthsegs:14 widthsegs:14 wirecolor:[140, 140, 90]
surfaceToAttach.pos = [0, 0, -30] -- Note the -40 offset in z direction
noiseMod = Noisemodifier()
noiseMod.strength.z = 90
noiseMod.animate = true
addModifier surfaceToAttach noiseMod

for i = 0 to 4 do (
	for j = 0 to 4 do (
		ctrl = Attachment()
		ctrl.node = surfaceToAttach
		obj = Box width:5 length:5 height:15 wirecolor:blue
		obj.pos = [-20 + i * 10, -20 + j * 10, 0]
		intersectPoint = intersectRayEx surfaceToAttach (ray obj.pos [0, 0, -1])
		
		-- Normally you will have to code two types of rays: one with direction UP [0, 0, 1]
		-- and one with direction DOWN [0, 0, -1] to make sure that you will get a HIT
		-- I just offseted my plane to -30 to be sure that I will get a HIT.
		-- Check out the Help Files for intersectRay intersectRayEx methods in MaxScript
		-- and try experimenting with rays and stuff...
		
		if intersectPoint != undefined do (
			obj.position.controller = ctrl
			addNewKey ctrl 0f
			ctrlKey = AttachCtrl.getKey ctrl 1
			ctrlKey.face = intersectPoint[2]
			ctrlKey.coord = intersectPoint[3]
		)
	)
)