align object min to max of source object

Hello Scriptspot community,

I would like to have a script to align an object, by just picking an source object and it would align my selection min, to max positon of the source Object.
just for one Button, to fasten the Workflow.

Regards.

AttachmentSize
align_min_max.jpg91.5 KB

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
titane357's picture

Hi MiauuI modify your code

Hi Miauu
I modify your code to make use more faster (no ui)
Is there a way to make a rubberband from each selected object ? Now just first one have a rubberband...

obj1 = selection
obj = pickObject rubberBand:obj1[1].pos
			if obj != undefined and selection.count != 0 do
			(
				for o in selection do
				(					
					o.pos.z = obj.max.z + (o.pos.z - o.min.z)
				)
			)
miauu's picture

.

Hi!

Rubberband accept only one point3 value. Instead of it you can use custon rubberband:

(
	global DrawCustomRubberband
	unRegisterRedrawViewsCallback DrawCustomRubberband
 
	local selObjsArr = selection as array
	local selObjsPosArr = for o in selObjsArr collect o.pos
 
	function DrawCustomRubberband = 
	(
		if selObjsArr.count != 0 do
		(
			local pointsArr = for p in selObjsPosArr collect (gw.transPoint p)
			local p3ViewCameraWorldPos = (inverse(getViewTM())).row4
			local p3ViewCameraWorldDir = -(inverse(getViewTM())).row3
			local fViewAngle = acos(dot (normalize(selObjsPosArr[1] - p3ViewCameraWorldPos)) p3ViewCameraWorldDir)
			local fViewDepth = (distance selObjsPosArr[1] p3ViewCameraWorldPos) * (cos fViewAngle)
			local p2MouseScreenPos = mouse.pos
			local p3MouseViewPos = mapScreenToView p2MouseScreenPos (-fViewDepth)
			local p3MouseWorldPos = p3MouseViewPos * inverse(getViewTM())
			gw.setTransform(Matrix3 1)
			local firstPointEnd = (gw.TransPoint p3MouseWorldPos)
			try(firstPointEnd.z = 0)catch()
			gw.setcolor #line (color 56 156 256)
			for k = 1 to pointsArr.count do gw.wPolyline #(firstPointEnd, pointsArr[k]) off			
			gw.enlargeUpdateRect #whole
			gw.updateScreen()
		)
	)
	registerRedrawViewsCallback DrawCustomRubberband
 
	obj = pickObject() 	
	if obj != undefined and selection.count != 0 then
	(
		for o in selection do
		(					
			o.pos.z = obj.max.z + (o.pos.z - o.min.z)
		)
		unRegisterRedrawViewsCallback DrawCustomRubberband
	)
	else
	(
		unRegisterRedrawViewsCallback DrawCustomRubberband
	)
)
titane357's picture

Hi, thanks !!! and we can

Hi, thanks !!! and we can change color of lines !! :-)
but in 2019, I need to remove first "unRegisterRedrawViewsCallback DrawCustomRubberband"
or an error pop up...

miauu's picture

.

In max 2019 is... funny. :)
Just put the first "unRegisterRedrawViewsCallback DrawCustomRubberband" in try()catch() statement. It is not the best option but it works. Or you will face what jahman wrote.

jahman's picture

.

Then you may end up registering redraw callbacks multiple times without any possibility to unregister it in future.

Dragan_ilic's picture

Wow thank you very much

Wow thank you very much

miauu's picture

.

Select the objcets that you want to align. Run the script, press the [Pick source object] button and pick the object to which you want to align.

(
	global rol_alignTo
	try(destroyDialog rol_alignTo)catch()
	rollout rol_alignTo "miauu"
	(
		pickbutton pbtn_getObj "Pick source object" width:140
 
		on pbtn_getObj picked obj do
		(
			if obj != undefined and selection.count != 0 do
			(
				for o in selection do
				(					
					o.pos.z = obj.max.z + (o.pos.z - o.min.z)
				)
			)
		)
	)
	createdialog rol_alignTo
)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.