Replace objects in file with merged object

Hi, I was handed some code over on cgtalk to read in lines from a text file and replace objects in the max scene with the correct merged object. However im getting alot of errors and after a weekend of googling and dissecting other scripts i cannot figure out what is wrong.

fn replaceModels str = 
(
	data = filterstring str "~"
	name = data[2]
	file = data[3]
	models = getnodebyname name all:on
	if models.count > 0 and doesfileexist file do
	(
		names = getMAXFileObjectNames file quiet:on
		if finditem names name > 0 do
		(
			count = objects.count
			mergeMAXFile file  #noRedraw #mergeDups #useMergedMtlDups #neverReparent 
			if count < objects.count do 
			(
				target = objects[objects.count]
				for model in models do
				(
					mat = model.mat
					instancereplace model target
					model.mat = mat
				)
				delete target
			)
		)
	)
	models
)
 
replaceModels "~DESCRIPTION_GOES_HERE~Box001~C:\Users\Jack\Desktop\cylinder_coloured.max"

It throws the error Unable to convert: ValidSelection() to type: String but ive had many more errors in the past without changing anything.

i have put together and attached a zip file with a test scene, the object to be replaced and the script file and id really appreciate it if someone would just take a quick look for themselves and see if they can address the problem im having.
 
 
In the script there is a line
 
replaceModels "~DESCRIPTION_GOES_HERE~Box001~C:\Users\Jack\Desktop\cylinder_coloured.max"
 
Just replace the path to cylinder_coloured.max with the path to the downloaded cylinder file and run.
 

 this script would eventually be used by myself to make my job a little easier. we currently have a locked down and encryped script that deals with the cad blocks we get sent from the autocad planners . The script however cant be updated to use new data so im trying to figure out if we can store the block names and the path to the correct model in a text file, then just read those in and merge them in the place of the cad blocks. Its what our current system does but this would be much more customisable in terms of being able to add our own new data and functionality into the system. 

 
Thanks to anyone who takes the time to have a look. 

 
Jack

AttachmentSize
test_scene.zip52.66 KB

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jpjapers's picture

I rectified that and i still

I rectified that and i still get the same error unfortunately.
Thanks

pixamoon's picture

`

try first:
instead of:
"C:\Users\Jack\Desktop\cylinder_coloured.max"
use:
"C:\\Users\\Jack\\Desktop\\cylinder_coloured.max"

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.