ScriptSpot

Forum topic · Scripts Wanted

Replace selected objects by instances according to prefix names.

By titane357 · 2020-04-07

Description

Hi,
I know how to replace objects by instance of an object but I can't figure how to do this in one go :

I have a lot of copies of identical ( but can be scaled, rotated ) "cube" : "cube001","cube002",... of "sphere", "cylinder",....
I want to select all these objects and click, boom !
All "cubexxx" are replaced by an instance of one "cubexxx" idem for "spherexxx",...

Thanks.

Comments (3)

.

miauu · 2020-04-07

Maybe this will help you.


(
	global rol_
	try(destroyDialog rol_)catch()
	rollout rol_ "miauu"
	(
		local masterObjsArr = undefined
		button btn_getMasterObjects "Get Master Objects" 
		button btn_doReplace "Replace Selected"
		
		on btn_getMasterObjects pressed do
		(
			if selection.count != 0 do 
			(
				masterObjsArr = selection as array
			)
		)
		
		on btn_doReplace pressed do
		(
			selObjsArr = selection as array
			if masterObjsArr != undefined and selObjsArr.count != 0 do
			(
				for mObj in masterObjsArr do
				(
					mObjName = (mObj.name + "*")
					for o in selObjsArr where (matchPattern o.name pattern:mObjName) == true do
					(
						format ": %   : % \n" mObj.name o.name
						--	"put the replace code here"
					)
				)
			)
		)
			
	)
	createdialog rol_ 	
)

titane357 · 2020-04-08

Hi, Miauu !!
How is your situation in Bulgaria ? Are you fine ?
Thank you very much for this script, it is exactly what I needed !!
I saw that you want me to work a little bit :-)
So I go further and I change :
-----------------------------------------------------------------------------
for mObj in masterObjsArr do
(
mObjM = trimright mObj.name "1234567890"
mObjName = (mObjM + "*")
for o in selObjsArr where (matchPattern o.name pattern:mObjName) == true do
(
--format ": % : % \n" mObj.name o.name
instanceReplace o mObj
)
)
-----------------------------------------------------------------------------
So I don't need to select the source object. Just one of each randomly.
I try to remove UI and then make the 2 array identical.
It works but when dealing with more than 200 objects it is very slow as it loops for each object all objects....

One thing very cool should be to remove UI, select all objects, and use as masters one of each different selected objects.
But I don't know how to... :-)
Take care.

.

miauu · 2020-04-08

Hi!
We have state of emergency, but I still go at work. :)
I did not know how exactly you replace your objects so I leaved this part to you. :)
Try this:


(
	selObjsArr = selection as array
	masterObjsArr = for o in selObjsArr where (trimright o.name "1234567890") == o.name collect o
	--print masterObjsArr
	for mObj in masterObjsArr do
	(
		mObjM = trimright mObj.name "1234567890"
		mObjName = (mObjM + "*")
		for o in selObjsArr where o != mObj and (matchPattern o.name pattern:mObjName) == true do
		(
			--format ": % : % \n" mObj.name o.name
			instanceReplace o mObj
		)
	)
)

It works only when your master objects don't have any number as suffixes.