Notifications
Clear all

[Closed] Need help with an 'instance-delete on obj move' script

I need some help with a script that will instance and delete objects, based on another objects pos.z

ie – a 1m x1m x1m box – every time it moves in +z by 1m, an instanced box will be created underneath it and every time it moves back down (-z) the instance will be deleted.

The purpose is for a tower crane rig where the crane head moves up and down with the mast pieces underneath being either created or deleted based on the crane height.

I’ve got a basic idea to begin but need help to get it working as it should.

CurHgt = $box.pos.z

rollout test "Test"
(

    spinner ch ""  fieldwidth:55 range:[0,200,CurHgt] 
	on ch changed val do
			(
				in coordsys #world $.pos.z = val
						if val >1 then
						(
					newObj = instance $
					newObj.pos.z = $.pos.z - $.height
						)
			)
)
createdialog test 100 100
6 Replies

you will meet a lot of problems trying to complete this task your way – create and delete.

could you do the same just hiding/unhiding pieces of geometry made in advance?

Hide/unhide pre-made geo is fine. I was aiming for clone and delete to remove any unnecessary objects from the scene files but it’s not critical.

I dont think it should be too difficult (except considering my mxs knowledge) but based on everything being cloned or deleted purely on the crane head pos.z value… hopefully makes it easier?

Any ideas on the mxs would be a huge help though.

you can animate visibility (show/hide), but you can’t animate existence (create/delete) . if you don’t care about rendering you can create and delete

I made a delete mesh modifier which you can animate on and off using MCG in 30 seconds. Now you can just wire-parameter it.

‘delete mesh’ is not the same as ‘delete node’…

Thanks for the comments guys.

Denis – I’m aware of the ‘can not animate creation/deletion’ problem you raised but for this task it should be okay – to a point anyway. It shouldn’t affect things too much. I’ve progressed further with the script and have roughed in a create/delete method that works okay but still needs to be looked at.

The main problem I face now (and in my head it should be simple but I’m just not seeing the answer) is, using a mulitlistbox to select a crane_rig (multiple in the scene), the controls will only affect the crane that is selected and not the others. I tried to attach my crane_rig max file but it’s a larger file size than what is allowed on the site.

/*


try(destroyDialog Crane_Rigger)catch()


CurHgt = $Crane_Platform.transform.pos.z
Hooker = $Crane_Hook_HI.transform.pos.z - 15
CHH = $Crane_Head.transform.pos.z
MastArr = #()



rollout Crane_Rigger "CRANE_RIGGER" 
(
		
local Crane_Rigs = #()
local	Crane = #()
	
	
-----------------------------------------------------------------------------------------------------------------------------------------------------
--FUNCTIONS
-----------------------------------------------------------------------------------------------------------------------------------------------------
fn mastgen = 	
					(
						in coordsys #local $Crane.pos.z = 4.4
						newObj = instance $mast wirecolor:yellow
						newobj.name =  uniquename "mast_new" 
						append MastArr newobj
						select newobj	
						newObj.pos.z = ($crane.pos.z - ($.max.z - $.min.z))
						clearselection()
					)
					
fn mastdel = 	
					(
						in coordsys #local $Crane.pos.z = -4.4
						for i = 1 to MastArr.count do print i
						sel_length = MastArr.count
						last_1_Ar = #()
						st_nm = sel_length - 0
						for i in st_nm to MastArr.count do (	ii = MastArr[i] ;append last_1_Ar ii )
						delete last_1_Ar 
						format "MastArr %
" MastArr 
						if MastArr.count != 0 do (for i = MastArr.count to 1 by -1 where not isValidNode MastArr[i] do deleteItem MastArr i)
						if MastArr.count == 0 then messagebox "Your erection is no longer of an adequate length!!"
					)
					
					
fn crane_child =
					(
						select $crane
						for obj in selection do
						(
							if obj.children != undefined do (selectmore obj.children)
						)
					)

-----------------------------------------------------------------------------------------------------------------------------------------------------
--MERGE CRANE FILE INTO SCENE
--on Crane_Rigger open do (mergemaxfile @"S:\ExViz\Assets\Development\models\crane_ui.max")
------------------------------------------------------------------------------------------------------------------------------------------------------
				
		
		hyperlink CR "CRANE_RIGGER" height: 30 align:#center offset:[0,10] color:(color 55 150 200)  enabled:false
		multiListBox ListCrane_Rigs "" fieldWidth:140  align:#center items:#() selection:#() height:5
		button btnAddCrane_Rigs "+" pos:[45,145] width:25 height:24
		button btnRemoveCrane_Rigs "-" pos:[75,145] width:25 height:24
		button btnClearCrane_Rigs "X" pos:[105,145] width:25 height:24
		label _lb0 "CRANE_HEIGHT:" across:2 align:#left offset:[10,50] 
		label _lb00 "" offset:[15,50] 
		label _lb1 "JUMP:   " across:3 align:#right offset:[0,15]
		button _GoJump "+" width:24 height:24 align:#right offset:[15,10]
		button _NoJump "-" width:24 height:24 offset:[10,10]
		label _lb2 "ROTATION:   " across:2 align:#right offset:[0,10]
		spinner AngleVal "" fieldwidth:40 range:[-180,180,0]  type:#integer  offset:[-24,10]
		label _lb3 "PITCH:   " across:2 align:#left offset:[12,10]
		spinner PitchVal "" fieldwidth:40 range:[20,60,45] type:#integer  offset:[-25,10] 
		groupbox grp1 "CRANE" width:160  height:160 pos:[10,185]
		label _lb4 "HOOK"  align:#left offset:[0,10] across:2
		slider theSlider "" orient:#horizontal range:[0,Hooker,Hooker]   offset:[5,0] ticks:2
		label _lb5 "REACH +20"  align:#left  offset:[0,10] across:2
		slider theSlider2 "" orient:#horizontal range:[0,-20,0]   offset:[5,0] ticks:2
	
-- 		on Crane_Rigger open do -- Add Objects on UI Open
-- 			(
-- 				select $Crane
-- 				userSel = getCurrentSelection()
-- 				if userSel.count >= 1 do
-- 				(
-- 					for o in userSel do (appendIfUnique Crane_Rigs o)
-- 					ListCrane_Rigs.items = for i in Crane_Rigs collect i.name -- update list with array
-- 				)
-- 				 clearselection()
-- 			)
			
		-- Add Crane by Selection
		on btnAddCrane_Rigs pressed do
			(
				userSel = getCurrentSelection()
				if userSel.count >= 1 do
				(
					for o in userSel do (appendIfUnique Crane_Rigs o)
					ListCrane_Rigs.items = for i in Crane_Rigs collect i.name -- update list with array
				)
			)
			
		-- Removes Crane fromt list
		on btnRemoveCrane_Rigs pressed do 
			(
				local currSel = ListCrane_Rigs.selection
				for i = ListCrane_Rigs.items.count to 1 by -1 where currSel[i] do (deleteItem Crane_Rigs i)
				ListCrane_Rigs.items = for i in Crane_Rigs collect i.name -- update list with array
			)
			
		-- Clear List
		on btnClearCrane_Rigs pressed do ListCrane_Rigs.items = (Crane_Rigs=#()) 
			

		--select from list
		on ListCrane_Rigs selected nameIndex do
			(
					select (getNodeByName ListCrane_Rigs.items[nameIndex])
					(_lb00.text = CurHgt as string)
			)
	
----------------------------------------------------------------------------------------------------------------------------------	
--Need option for per crane control for multiple cranes in scene			 
----------------------------------------------------------------------------------------------------------------------------------	
--CRANE CONTROL

		--height
		on crane_height changed val do
			 (
				in coordsys #world $Crane_Head.pos.z = val
			 )

		--jump[+]
		on _GoJump pressed do
			 (
				mastgen()
			 )		 
		 
		 --jump[-]
		on _NoJump pressed do
			 (
				mastdel()
			 )		

		 --rotation
		on AngleVal changed val do 
			( 
				in coordsys #world $Crane_Head.rotation.z_rotation = val
			)
			
		--jib pitch
		on PitchVal changed val do 
			( 
				in coordsys #gimbal $Crane_Jib.rotation.y_rotation = val
			)
		
		 --hook
		on theSlider changed val do
			 (
			 in coordsys #world $Crane_Hook_LO.pos.z = val
			 )
		 
		--jib reach
		on theSlider2 changed val do
			 (
			 in coordsys #gimbal $Crane_Reach.pos.x = val
			 )
			 
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
	 --ANIMATION

		checkbutton _Rec "Rec" width:30 height:30  offset:[30,30] across:2 highlightColor:red

		on _Rec changed theState do
		(
		if theState == true then
		animButtonState = true
		else
		animButtonState = false
		)
					 
		--button _btn1 "Rec." width:30 height:30  offset:[20,30] across:2
		button _Key "Key" width:60 height:30 offset:[15,30]
		on _Key pressed do
		(
			crane_child()
			addNewKey $.rotation.controller currentTime
			addNewKey $.pos.controller currentTime
			clearselection()
		)


)
createDialog Crane_Rigger 180 500 /*parent:Project_Pro.hwnd */ style:#(#style_titlebar, #style_sysmenu, #style_toolwindow)