MAXScript學習筆記(4) 功能:實用工具

一.替換模型

功能:將場景中的一個或者多個模型替換爲另一個模型

應用場合:Revit中無法實現的內容,用3dmax模型替換來實現。

具體場景:監獄的金屬柵欄,包括水平着的頭頂上的金屬柵欄。當前Revit技術能力有限,無法在Revit中是實現,但是3dmax中就有很多這種的鐵絲網。

核心替換代碼是:

	group "替換"
	(
		pickbutton pbSource "Source" pos:[10,120,0] width:110
		on pbSource picked obj do 
		(	
			rootObj=Common.GetRootNode obj
			centerpivot rootObj --有時候需要
			source_obj = rootObj --存取 Utility 局部變量 
			pbSource.text = rootObj.name --存取 Utility 控件 
			-- select rootObj --場景大的情況下會小卡
		)--事件處理程序結束

		checkbutton pbTarget "Target" pos:[120,120,0] width:110 --checkbutton結合pickObject()實現多選功能
		on pbTarget changed state do
		(
			if state do
			(
				pbTarget.enabled = off
				target_obj = #()
				while isvalidnode (obj = pickObject()) do (

					rootObj=Common.GetRootNode obj
					appendifunique target_obj rootObj
					if target_obj.count == 1 then 
					(
						pbTarget.text = rootObj.name
					)
					else
					(
						pbTarget.text = (target_obj.count as string) 
					)
				)
				print ("select:"+(target_obj.count as string))

				pbTarget.enabled = on
				pbTarget.checked = off
			)
		)

		button btnClearSelection "Clear" pos:[230,120,0] width:40
		on btnClearSelection pressed do 
		(
			clearSelection()
			source_obj=undefined
			target_obj=#()
			pbTarget.text="Target"
			pbSource.text="Source"
		)

		checkbox cbIsClone "Clone" pos:[270,125,0] width:50 checked:true
		checkbox cbIsX "X" pos:[315,125,0] width:20 checked:true
		checkbox cbIsY "Y" pos:[340,125,0] width:20 checked:true
		checkbox cbIsZ "Z" pos:[365,125,0] width:20 checked:false
		button btnReplace "Replace" pos:[390,120,0] width:60
		on btnReplace pressed do 
		(
			-- targets=$ as Array --有delete時需要轉換成Array
			if target_obj ==undefined or target_obj.count == 0 do return undefined
			targets=target_obj
			
			print ("replace1:"+(source_obj as string)+"->"+(targets as string ))
			newObj=undefined
			for target in targets do
			(
				if target == source_obj do continue
				print ("replace2:"+(source_obj as string)+"->"+(target as string ))
				-- replaceInstances  
				-- format "%,%" source_obj t
				newObj=source_obj
				if cbIsClone.checked do (newObj=copy source_obj)
				if cbIsX.checked do (newObj.pos.x=target.pos.x)
				if cbIsY.checked do (newObj.pos.y=target.pos.y)
				if cbIsZ.checked do (newObj.pos.z=target.pos.z)
				-- newObj.pos=t.pos
				-- newObj.name=t.name
				newObj.parent=target.parent
				delete target
			)
		)
	)

界面:

期間比較困擾的問題是如何多選,pickbutton點擊一次只能選擇一個物體,最後找到一個資料,https://forums.cgsociety.org/t/max-script-help-multi-object-selection-to-pickbutton/1690288/3,用checkbutton結合pickobject()實現的多選物體的效果。

效果:

替換前:

替換後

還不會渲染.....

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章