Notifications
Clear all

[Closed] Script Controller Code to Script Equivalent Code question

I am writing a script where a selected object ($) is moved to another objects mesh face.
This is being done by calling the intersectRay function between the $ and the mesh, then updating $.pos to equal where the ray intersects with the mesh. I want to do this in real-time, in the viewport.

 What I want to do is have the viewport update constantly, so the user can drag the $ on top of the mesh, and the $ always stays on top of the mesh.
 
 I am using this tutorial as a starting point: [ http://www.rubengarza.com/maxscript-T04-ScriptController.htm ]( http://www.rubengarza.com/maxscript-T04-ScriptController.htm) 
 
 [b]Here are my questions[/b]: 
 I do not want to use a script controller, instead I want all the code to stay inside a .ms file.  What kind of code/syntax would be required to constantly update the viewport, check for the ray intersections, and update $'s.pos?  I want to make this as 'real-time' as possible, and the above tutorial accomplishes this but uses script controllers with certain bits of code I haven't seen before, so I don't know how to convert those bits of code into code that works outside of the script controller.  These bits of code are: "dependsOn" and  "$Collision".  
 Going through the MAXscript help, I find: [i]"You do  not need 'dependsOn' in the scripted controller's expression since [b][b]3ds  Max 8[/b][/b].[/i][i]  'dependsOn' should not be used in new scripts, and should  actually be [b][b]removed[/b][/b] from old ones." 
 [/i]So I probably can't/shouldn't use dependsOn in a .ms script, but I'm not sure about $Collision.

Here is WIP code, where selectedMesh = a picked mesh object, and currentlySelectedObject = $ (user has object selected):


   function stickObjToMesh currentlySelectedObject =
   	(
   	rayToMeshCheckDownZ = ray currentlySelectedObject.pos [0,0,-1]
   	ZpositionDWN = intersectRay selectedMesh rayToMeshCheckDownZ
   	currentlySelectedObject.pos.z = ZpositionDWN.pos.z + currentlySelectedObject.radius
   	)
   
   

I stuck that code into a .ms, then added a button that calls that function, then added a pickbutton that lets the user set the selectedMesh to any mesh in the scene. Then the user clicks another object and presses the “StickTo” button, which runs the function above, sticking the selected object to the mesh’s face.
So far it only works on the Zaxis, but I don’t think it will be difficult to check ray intersections in all directions. However, I’d like to remove the “StickTo” button and instead just use a button toggle, like pressing CTRL+ALT (with a valid node selected) to call the function above.

I’m not looking for a copy + paste solution, rather an understanding of these bits of code.
Any direction / wisdom / coding know-how greatly appreciated!

2 Replies

your right dependsOn is old and should not be used

$Collision I’m guessing actually references an object of that name directly in the example scene

If you don’t want to use a script controller you can use a callback to check the position of your object while the user is dragging it. Write a function to glue the object to the surface and call it several time a second.

look at the ‘timer’ rollout UI control

Found the answer! Here’s the bit of code that I’m betting on:


       when transform $ changes do
       (
       	print "update...."
       )
       
   Here is my psuedo-code for operation so far:

       upon $ move do (shoot ray from perspective, if ray hits mesh 
   	  ($.pos = rayIntersectionPointOnMesh.pos))
       
   The other way of doing it I contemplated was using a position script controller, but I think the above method would be faster.  I'm not sure if the above method will hog system resources, so this might not be the proper way to do it.  Any input welcome! :)

*update

Strictly speaking, change handlers are supposed to be used to just set “dirty flags” in a lightweight, order-independent way, and then use Redraw Views or Time Change callbacks to actually cause the triggered processing. You should attempt to use change handlers judiciously, and not, for example, as a simple way to get scripted controllers.

Gonna have to put more thought into this one…