Add Attachment Constraint


Hi everyone,

I’m 100% new to maxscript (literally started yesterday, and this forum is the best learning resource so far).

I’m currently trying to write a script that:

Having a mesh with Edit Poly, and some faces selected; it creates dummies on the center of each selected face and adds an Attachment Constraint to it (so when I later move a face, it moves the dummy with it).

I’m so far very happy with what I managed to do:

  • I get the selection
  • run thru it
  • create a dummy
  • place at the center of the face

Now, I’m struggling to add an Attachment Constraint:
The farthest I got was to add an Attachment Controller to the Position, but then the dummies get all misplaced and it indeed replaces the Position XYZ with an Attachment within the Controllers (probably not good).

-----------Make sure it has Edit Poly modifier---------------

obj = (selection as array)[1]

--only for Editable Poly:
--selFaces = GetFaceSelection obj

modifierCurrent = modpanel.getCurrentObject()
selFaces = modifierCurrent.getSelection #Face node:obj

if not selFaces.isEmpty do
	for face in selFaces do
		center = polyop.getFaceCenter obj face
		dummyBox = Dummy pos:[center.x,center.y,center.z]
		dummyBox.boxsize = [1,1,1]
		attachController = Attachment()
		attachController.node = obj
		attachController.align = true
		dummyBox.pos.controller = attachController
		keyNew = AttachCtrl.addNewKey attachController 0
		keyNew.face = face
		keyNew.coord = center


What I need is what you can get by:
-selecting dummy
-Animation -> Constraints -> Attachment Constraint

by doing so, it adds an Attachment Constraint and don’t mess with the controllers. Then one needs to manually pick the object to link to and the position of the constraint.

Can anyone please help me with this? Please keep in mind I’m an experienced programmer, but a total zero, newbie, loser in Maxscript (and I’m struggling a lot with its syntax, classes, properties, etc.)

Thank you very much!!!


Ok, I think I’m close to it.

Maybe it’s fine to do as I did, with an attachment controller (otherwise, please suggest!) I see the dummies are actually linking, but to the wrong faces.

I’ve read that the key.coord property is in barycentric coordinates. But I’m using quads. Faces can be any type of polygon, so I don’t see how barycentric makes any sense here. How can I probably obtain the proper coordinates (center of the face)? Could anyone help me with this, please? :slight_smile:


the poly face index was not same as mesh ,and almost all base interface based on mesh
before you set the key.face , you need get the mesh face and coord first
one of the methods was intersectRayEx , it will returen which mesh face index and what the coord value it is

polyobj=converttopoly (sphere())
meshobj=snapshot polyobj
attachindex = 20
facecenter = polyop.getFaceCenter polyobj attachindex
testray=ray facecenter (getViewTM()).pos
result=intersectRayEX meshobj testray
ctr=attachment ()
obj = point()
obj.transform.controller.position.controller = ctr
ctr.node = polyobj
ctr.manupdate  = true
key=AttachCtrl.addNewKey ctr 0
key.coord = [result[3][1],result[3][2]]
AttachCtrl.update ctr
delete meshobj


Hello, thank you very much for your help!

As I understand, you copy the object into an Editable Mesh because many functions only work with Editable Meshes (like intersectRayEX), right?

Unfortunately, your example doesn’t quite work for me:

It creates a helper point, but it’s not attached to the corresponding face. So when you move the object or the face, the point doesn’t move.
As I have shown in my first screenshot, I can place helpers on faces; the problem comes when attaching them (constraint).

I tried to modify your example, but it will mainly only work for spheres. If you try it with boxes or other shapes, the ray doesn’t intersect (why?):

Also, what are you doing here?

key.coord = [result[3][1],result[3][2]]

how do you know that the face is the value in “result[2]” minus 1?
how do you know that the coord is in the indices [3][1] and [3][2]? (this only work in your example, and only for face 20).

Thanks in advance!!!


you should set the attachindex to the face num you need
the result was undefined because of the dir of the ray , this method can’t set double-side , so what you need was a function to test forward direction , if undefined , test reverse
and the real copied mesh was the face you attached by detach interface , if not ,you also need the result[1] to test the pos , and set another ray to test again .
a point was clear to check result , you could change to whatever you want
the point didn’t move with the face , that’s I set the update to manual , when you done the create , set to auto again by ctr.manupdate = false


set the mesh face num the point attached to,the intersectRayEX’s result was 0 based , and all max mesh method were 1 based , so the result bigger than the real num by 1
and set the pos where the point inside the face,the result returns the pos with barycentric coordinates , this was used by attachment controller
that’s the UI key info part


Oh, I finally got it working!

Now I understand: Editable Mesh has face (triangle) and polygon. And key.face expects a triangle (that’s why key.coord is in Barycentric coords!) It makes sense now.

I had to struggle to make the ray work.
If you allow me, I’ll post here a little correction from your ray example, for anyone in the future.

instead of:

testray = ray facecenter (getViewTM()).pos
result = intersectRayEX meshobj testray

it should be:

direction = facecenter - (getViewTM()).pos
testray = ray (getViewTM()).pos direction
result = intersectRayEX meshobj testray

like this it works for me!

Thank you very much A娘 for all your help in this learning process! :slight_smile:


the ray always works may this

facenormal=polyop.getFaceNormal polyobj attachindex
testray = ray (facecenter+facenormal) (-1*facenormal)
result=intersectRayEX meshobj testray

it’s shot the ray from front of the facecenter and towards to the center , of cause detach the face to test was important , if not there may be wrong result of first test