Notifications
Clear all

[Closed] Selecting every other object

I realise the following code doesn’t work but was hoping someone could suggest a way to:

Select every other dummy and rotate them by 22.5 degrees in y then:

Select the one that weren’t selected the first time and rotate them in the opposite direction by 22.5 degrees

dumArr = ($chassis_* as array)
for i = 2 to dumArr.count by 2 do rotate i (angleaxis spnDeg.value [0,1,0])
for i = 1 to dumArr.count by 2 do rotate i (angleaxis -spnDeg.value [0,1,0])
11 Replies

You should change: rotate i (angleaxis spnDeg.value [0,1,0]) –> rotate dumArr[i] (angleaxis spnDeg.value [0,1,0])


dumArr = ($chassis_* as array)
for i = 2 to dumArr.count by 2 do rotate dumArr[i] (angleaxis spnDeg.value [0,1,0])
for i = 1 to dumArr.count by 2 do rotate dumArr[i] (angleaxis -spnDeg.value [0,1,0])

2 Replies
(@lucpet)
Joined: 11 months ago

Posts: 0

Thanks a lot batigolnguyen this works great.

How would I make it so the result on each item wasn’t accumulative ie the first, third, fifth etc item is rotate forward by a set amount. Say 22.5 degrees and the 2nd, 4th, 6th etc are then rotated back by 22.5 degrees.

Any ideas appreciated

(@solitude)
Joined: 11 months ago

Posts: 0

Isn’t that what you’re doing already? Every even one rotates forward, every odd rotates back…

Arr yes Ian it’s just not working out that way primarily because I’ve got it in with a bunch of other code and the result seems to be multiplying each rotation and not just going forward one amount then back that same amount on the next.

When I copy the objects then move them apart a set distance they are also not ordered in a sequential way ie They end up in this order 1, 6, 5 , 4, 3, 2 so maybe this has a effect.

I’m then trying to offset their firing order as a rotation in degrees, then trying to set them in a v formation.

Here is the full code

 /* 	Script: Piston.ms
* 	Author:  Luke Pettit
* 	Blog  http://lukepettit-3d.blogspot.com/ 
* 	e-mail:  petluke@gmail.com
* 	Date:    11/12/2010  v 0.8
* 	Based on the tutorial by Dario Strano  https://www.youtube.com/watch?v=-02mLcuNutQ 
*	With plenty of help from Pixel Monkey aka Eric Craft, Anubis, Kilad, Marc Lorenz, Jason Labbé, Johan Boekhoven
*/
try(destroydialog pistonRollout)catch()

rollout pistonRollout "Streincorp Pistons"
(
	spinner fCount "Number of Pistons: " type:#integer range:[1,12,1] 
	spinner spnDis "Distance appart: " type:#float range:[1.0,500.0,20.0]
	spinner spnAnsp "Animation speed: " type:#integer range:[1,100,10]
	spinner spnCra "Crankshaft radius: " type:#float range:[1.0,500.0,20.0]
	spinner spnRod "Conrod length: " type:#float range:[1.0,500.0,50.0]
	spinner spnDeg "Rotational offset: " type:#float range:[0.0,10000.0,0.0]
	spinner spnsVe "Set V in Degrees: " enabled:true type:#float range:[0.0,10000.0,45.0]
	radiobuttons rdoOri "Orientation" labels:#("x", "z") default:2
	CheckButton btnHlp " Help > " toolTip:"Press this button to get help."  across:2 width:75 height:21
	button btncPistons "Create Pistons"
	
	label lbl1 "-- Sets the number of pistons Default: 1" pos:[225,5] width:350 height:20
	label lbl2 "-- Distance the pistons appart Default: 20" pos:[225,26] width:350 height:20
	label lbl3 "-- Animation speed, lower = faster Def: 10" pos:[225,48] width:350 height:20
	label lbl4 "-- Crank shaft radius Default: 20.0" pos:[225,68] width:350 height:20
	label lbl5 "-- Con rod length Default: 50.0" pos:[225,89] width:350 height:20
	label lbl6 "-- Rotation Offset in Degrees Default: 0.0" pos:[225,110] width:350 height:20
	label lbl7 "-- Set V in Degrees Default: 45.0" pos:[225,130] width:350 height:20
	label lbl8 "-- Aligned along y, in x or z axis Default: z" pos:[225,160] width:350 height:20
	HyperLink hl1 "Streincorp: Dario Strano" address:"https://www.youtube.com/watch?v=-02mLcuNutQ" color:(color 78 78 177) pos:[266,180]
	HyperLink hl2 "Luke Pettit-3d Blog" address:"http://lukepettit-3d.blogspot.com/" color:(color 78 78 177) pos:[280,195]
	
	 
	fn piston = 
	(
		ch = dummy()
		ch.name = uniqueName "Chassis_"
		poO = point size:15
		poO.name = uniqueName "pointO_"
		poA = point size:15 pos:[0.0, 0.0, 20.0]
		poA.name = uniqueName "pointA_"
		poB = point size:15 pos:[45.0, 0.0, 0.0]
		poB.name = uniqueName "pointB_"
		cr = cylinder radius:2.5 height:spnCra.value 
		cr.name = uniqueName "Crank_"
		cr.renderable = off
		rd = cylinder radius:2.5 height:spnRod.value pos:[0.0, 0.0, spnCra.value] renderable = off
		rd.name = uniqueName "rod_"
		rd.renderable = off
		rotate rd (angleaxis 113.578 [0,1,0])
		ps = cylinder radius:9.0 height: 20.0 pos:[37.5, 0.0, 0.0] renderable = off
		ps.name = uniqueName "Piston_"
		ps.renderable = off
		rotate ps (angleaxis 90.0 [0,1,0])
		----------------------------------------------------
		poO.parent = ch
		cr.parent = poO
		poA.parent = cr
		-----------------------------------------------------------
		--Make sure Crank has its height set as a bezier_float()
		cr.height.controller = bezier_float()
		--Set the controller for the Z position of "point A"
		poA.position.z_position.controller = float_expression()
		--Set the Crank.height controller as the scalar target "r"
		poA.position.z_position.controller.AddScalarTarget "r" cr.height.controller
		--Set the expression for the Expression controller to "r"
		poA.position.z_position.controller.setExpression "r"
		--------------------------------------------------------
		rd.rotation.controller = LookAt_Constraint ()
		rd.rotation.controller.appendTarget poB 100
		rd.rotation.controller.target_axis = 2
		rd.rotation.controller.upnode_axis = 1
		rd.rotation.controller.StoUP_axis = 1
		rd.rotation.controller.viewline_length_abs = off
		---------------------------------------------------------------
		poB.parent = ch
		ps.parent = poB
		rd.parent = poA
		------------------------------------------------------------
		set animate on
		sliderTime = spnAnsp.value
		rotate poO (angleaxis 360 [0,1,0])
		set animate off
		-- Set the before and after Out-of-range Type to linear
		setBeforeORT poO #linear
		setAfterORT poO #linear
		------------------------------------------------------------
		rd.height.controller = bezier_float()
		--Set the controller for the x position of "point B"
		poB.pos.controller.X_Position.controller = Float_Expression ()
		-- Set the Crank.height controller as the scalar target "r"
		poB.pos.controller.X_position.controller.AddScalarTarget "r" cr.height.controller
		-- Set the expression for the Expression controller to "r"
		poB.pos.controller.X_position.controller.setExpression "r"
		poB.pos.controller.X_position.controller.AddScalarTarget "L" rd.height.controller
		-- Set the expression for the Expression controller to "L"
		poB.pos.controller.X_position.controller.setExpression "L"
		-- Set the in and out tangents for the y rotation keys to linear
		for val in poO.rotation.controller.y_rotation.controller.keys do (val.inTangentType = val.outTangentType = #linear)
		--Set the controller for the y rotation of "point O"
		poB.pos.controller.X_position.controller.AddScalarTarget "Yrot" poO.rotation.controller.y_rotation.controller
		-- Set the expression for the Expression controller to "Yrot"
		poB.pos.controller.X_position.controller.setExpression "r*sin(radToDeg(Yrot))+sqrt(L^2-(r^2*cos(radToDeg(Yrot))^2))"
		
		case rdoOri.state of
		(
			1: (rotate ch (angleaxis 0 [1,0,0]))
			2: (rotate ch (angleaxis -90 [0,1,0]))
		)
 	)
		
		on btncPistons pressed do
		(
			for i=1 to fCount.value do 
			(
				piston()
				dumArr = ($chassis_* as array)
 				for i = 2 to dumArr.count do move dumArr[i] [0.0, spnDis.value, 0.0]
				for i in $pointO_* do rotate i (angleaxis spnDeg.value [0,1,0])
				for i = 1 to dumArr.count by 2 do rotate dumArr[i] (angleaxis -(spnsVe.value/2) [0,1,0])
				for i = 2 to dumArr.count by 2 do rotate dumArr[i] (angleaxis (spnsVe.value/2) [0,1,0])
				sliderTime = 0f
			)
		)
					
			on btnHlp changed op do -- help button
			(
				if op then
				(
					pistonRollout.width = 440
					btnHlp.caption = "Help <"
				)else
					(
						pistonRollout.width = 220
						btnHlp.caption = "Help >"
					)
			)
)
createdialog pistonRollout width:220 height:220
addRollout pistonRollout

I think you should sort your dumArr before rotating.
You can sort by name, position or any properties.

1 Reply
(@lucpet)
Joined: 11 months ago

Posts: 0

Thanks I’ll look at sort

Mmmm sort just gives me an error

No "">"" function for $Dummy:Dummy02 @ [-3.631441,-20.056252,-0.000000]

Despite having 8 dummies selected via an array

sort <array>
Sorts the elements of the array into ascending order. All the elements must be comparable.??? comparable to what?

try qsort, it is the basic of maxscript, I think should read maxscript reference first


fn compareFN v1 v2 =
(
  local d = v2.pos.x-v1.pos.x
  case of
  (
     (d < 0.): -1
     (d > 0.): 1
     default: 0
  )
)
qsort dumArr compareFN

Thanks again batigolnguyen

I’ve finally discovered that angleaxis was giving me spurious results so I changed to eulerangles which was confusing the matter with another angle setting.
Not sure what the problem was but I’m rewriting much of that section.

angleaxis and eulerangles got same problem with order of rotation x,y,z
Try the quaternion, you will get better

Oh I fixed the ordering problem by changing how I created the objects yay!
But the code and angleaxis was making each angle incremental for some reason.
Now I’ve re written that bit of code as well (It works now) but also have other features now to add to the script so it’s no problem to have to rewrite.
But you’re right about learning and getting better I’ve learnt a lot already but more I learn the less I know! sigh :banghead: