Notifications
Clear all

[Closed] Put object at a fix distance from border

Hi!

Please, if you can take a look at this screenshot:

www.ossosso.com/scambio/shoot_21.JPG

There are 3 different rectangular shape (no parametric rectangular objects).

At the beginning they have a certain distance I’ve marked as “dist01a / dist02a” in black.
I’d like through a script to move shape 2 and 3 nearer, respectively at “dist02b” and “dist01b”.

The nearer distance (“dist02b/dist01b”) has a fix value of 3.7mm

The problem for me is that I have more then one solution; in the screenshot you can see one, but in other solutions Rectangular Shapes have different dimensions, and the black “dist01a/dist02a” changes.

Theorically I think this should be the process:
-Each shape as a name, for example “shape 1=Base_001”, “shape 2=Top_001”, “shape 3=Right_001”
-In a variable I store coordinates of the top right vertex of Base_001
-In a variable I store coordinates of the bottom right vertex of Top_001
-In a variable I store coordinates of the top left vertex of Righ_001
-then with some math operations I calculate the original distance, I subtract 3,7 and I translate the result along Y for Top_001 and along X for Right_001

But I really don’t know how to transfer this instructions in maxscript

If someone could help me… give me also a quote if you think this is the case.

Thanks a lot!

2 Replies

here is a little toy to play and hopefully have a fun:

global testXY_pos = unsupplied
 try
 (
 	testXY_pos = getdialogpos testXY
 	destroydialog testXY
 )
 catch()
 rollout testXY "X&Y by denisT" width:150
 (
 	local x_constraint, y_constraint, target
 	local x_tape, y_tape
 	local target_min = [0,0,0], target_max = [0,0,0], target_center = [0,0,0]
 
 	fn randomSize = (random 4 20) 
 	fn randomPos = (random -[20,20,0] [20,20,0])
 	
 	local possible_pos
 	
 	group "Distance: "
 	(
 		spinner x_distance_sp "X: " type:#float range:[0,1e9,0] fieldwidth:50 align:#right offset:[4,0]
 		spinner y_distance_sp "Y: " type:#float range:[0,1e9,0] fieldwidth:50 align:#right offset:[4,0]
 	)
 	group "Variant: "
 	(
 		radiobuttons variant_rb labels:#("1", "2", "3", "4") columns:4 --align:#left offset:[0,0]
 		button find_best_bt "Find Best" width:132 align:#right offset:[4,0]
 	)
 	group "Create: "
 	(
 		spinner seed_sp "Seed: " type:#integer range:[0,1e9,0] fieldwidth:50 align:#right offset:[4,0]
 		button create_bt "Create Test" width:132 align:#right offset:[4,0]
 	)
 	fn updateDistance = try
 	(
 		x_distance = x_distance_sp.value
 		y_distance = y_distance_sp.value
 		
 		x1 = x_constraint.min.x - x_distance - target_max.x
 		x2 = x_constraint.max.x + x_distance - target_min.x 
 		
 		y1 = y_constraint.min.y - y_distance - target_max.y
 		y2 = y_constraint.max.y + y_distance - target_min.y
 
 		possible_pos = #([x1, y1, 0], [x1, y2, 0], [x2, y1, 0], [x2, y2, 0])
 	)
 	catch()
 	
 	fn findBestVariant =
 	(
 		min_dist = 1e9
 		best_var = 0
 		for k=1 to possible_pos.count do
 		(
 			pos = possible_pos[k] + target_center
 			dist = distance x_constraint.center pos + distance y_constraint.center pos 
 			if dist < min_dist do
 			(
 				min_dist = dist
 				best_var = k
 			)
 		)
 		variant_rb.state = best_var
 	)
 
 	fn createTest = 
 	( 
 		delete objects
 		if seed_sp.value != 0 do seed seed_sp.value
 			
 		x_constraint = rectangle name:#x_constraint width:(randomSize()) length:(randomSize()) pos:(randomPos()) wirecolor:red
 		y_constraint = rectangle name:#y_constraint width:(randomSize()) length:(randomSize()) pos:(randomPos()) wirecolor:green
 
 		target = rectangle name:#target width:(randomSize()) length:(randomSize()) pos:(randomPos()) wirecolor:yellow
 		target_min = target.min
 		target_max = target.max
 		target_center = target.center
 
 		x_tape = tape name:#x_tape target:(targetobject name:#'x_tape.target')
 		y_tape = tape name:#y_tape target:(targetobject name:#'y_tape.target')
 		
 		x_distance_sp.value = random 1.0 20.0
 		y_distance_sp.value = random 1.0 20.0 
 
 		updateDistance()
 	)
 	fn updateTest state: = undo off try
 	(
 		if state == unsupplied do state = variant_rb.state
 		with redraw off
 		(
 			target.pos = possible_pos[state] + target_center
 
 			if state < 3 then  
 			(
 				x_tape.pos = [x_constraint.min.x, target.center.y, 0]
 				x_tape.target.pos = [target.max.x, target.center.y, 0]
 			)
 			else
 			(
 				x_tape.pos = [x_constraint.max.x, target.center.y, 0]
 				x_tape.target.pos = [target.min.x, target.center.y, 0]
 			)
 			
 			if state == 1 or state == 3 then
 			(
 				y_tape.pos = [target.center.x, y_constraint.min.y, 0]
 				y_tape.target.pos = [target.center.x, target.max.y, 0]
 			)
 			else
 			(
 				y_tape.pos = [target.center.x, y_constraint.max.y, 0]
 				y_tape.target.pos = [target.center.x, target.min.y, 0]
 			)
 		)
 		completeredraw()
 	)
 	catch()
 	
 	on create_bt pressed do undo "Create Test" on
 	(
 		createTest()
 		findBestVariant()
 		updateTest()
 	)
 	on x_distance_sp changed val do
 	(
 		updateDistance()
 		updateTest()
 	)
 	on y_distance_sp changed val do
 	(
 		updateDistance()
 		updateTest()
 	)
 	on variant_rb changed state do undo off
 	(
 		updateTest()
 	)
 	on find_best_bt pressed do undo off
 	(
 		findBestVariant()
 		updateTest()
 	)
 )
 createdialog testXY pos:testXY_pos

enjoy!

PS. play in TOP view

Impressive denis… you have written it in so few time… a bit of envy…
Anyway I don’t know how to use it in my scenes: consider I have a lot of layers, and each layer has its triad of squares.

I thought I can align my squares to those created by your script, but when I click “Create Test” on your window, it deletes everything in the scene.

Thank you very much denis. I have only to learn from this.