convert: undefined to type: String PROBLEM [SOLVED]

OldString = "Arch137" as string -- Change to suit
NewString = "AM137" as string -- Ditto
theMaps = getClassInstances BitmapTexture
for i in 1 to theMaps.count do (
	fileName = theMaps[i].filename
	if matchPattern fileName pattern:"AM*" ignoreCase:false == false then (
		newFilename = trimleft fileName OldString
		new2filename = NewString + newFilename
		theMaps[i].filename = new2Filename -- renames
	)
)

The above creates an error stated in the subject. I have tested this on 20+ scenes prior without any errors. I want to achieve is to replace 'string1' for 'string2' in the filename paths whilst not affecting the actual path.

As I've said it worked on 20 odd scenes prior.

Thanks for any help.

Comments

Comment viewing options

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

Regexpression example (Greaaaat success - Borat)

OldString =@".+"
MapArray = getClassInstances BitmapTexture
for o in MapArray where doesFileExist(o.filename) == false do (
	rgx = dotnetObject "System.Text.RegularExpressions.Regex" OldString
	filename = rgx.Replace o.filename ""
	o.filename = Filename
	ATSOps.Refresh()
)

Strips filenames and paths only if missing from scene :) (much like relink bitmaps by colin senner)

3dwannab's picture

FINISHED CODE (Case sensitive)

OldString = "XXX"
NewString = "HD"
MapArray = getClassInstances BitmapTexture
-- fix for missing names in maps help by pixamoon
for o in MapArray where o.filename != undefined do (
    fileName = (filenamefrompath o.filename)
    if fileName != undefined and matchPattern fileName pattern:("*"+OldString+"*") ignoreCase:false == true then
        try(
            newFilename = replace fileName (findstring fileName OldString) OldString.count NewString
            o.filename = pathConfig.normalizePath ((getfilenamepath o.filename) + "\\" + newFilename)
			ATSOps.Refresh() 
        )catch()
)
3dwannab's picture

Code error in last code snippet NEW CODE HERE...

fileName = "HDM_04_09_testfilename"
OldString = "HDM"
NewString = "AM"
CombinedStrings = replace fileName (findstring fileName OldString) OldString.count NewString

Listener output:

"HDM_04_09_testfilename"
"HDM"
"AM"
"AM_04_09_testfilename"
pixamoon's picture

`

Hello again :) maybe something like this:

OldString = "Arch137" as string -- Change to suit
NewString = "AM137" as string -- Ditto
 
for o in getClassInstances BitmapTexture do (
	if (fileN = getfilenamefile o.filename) != undefined and matchPattern fileN pattern:"AM*" ignoreCase:false == false then (
		newFilename =  trimleft fileN OldString
		if newFilename != fileN do (
			new2filename = NewString + newFilename
			o.filename = pathConfig.normalizePath ( getfilenamepath o.filename + "\\" + new2Filename + getfilenametype o) -- renames 
		)
	)
)

or just minimum addition to your code:

OldString = "Arch137" as string -- Change to suit
NewString = "AM137" as string -- Ditto
theMaps = getClassInstances BitmapTexture
for i in 1 to theMaps.count do (
	fileName = theMaps[i].filename
	if fileName != undefined and matchPattern fileName pattern:"AM*" ignoreCase:false == false then (
		newFilename = trimleft fileName OldString
		new2filename = NewString + newFilename
		theMaps[i].filename = new2Filename -- renames
	)
)
3dwannab's picture

Hiiii, No...

First code you wrote still has that same error.

I've fixed the problem in my original code where it was replacing part of the filename with this particular scene by using 'fileName = (getfilenamefile theMaps[i].filename)' as opposed to 'fileName = theMaps[i].filename' but the error still crops up.

Full output from the listener using code:

OldString = "Arch137" as string -- Change to suit
NewString = "AM137" as string -- Ditto
theMaps = getClassInstances BitmapTexture
for i in 1 to theMaps.count do (
	--fileName = theMaps[i].filename
	fileName = (getfilenamefile theMaps[i].filename)
	if fileName != undefined and matchPattern fileName pattern:"AM*" ignoreCase:false == false then (
		newFilename = trimleft fileName OldString
		new2filename = NewString + newFilename
		fileName = new2filename -- renames
	)
)

Listener:

"Arch137"
"AM137"
#(Map #1:Bitmap, Map #1:Bitmap, Map #1:Bitmap, Map #1:Bitmap, Map #1:Bitmap, Map #1:Bitmap, Map #1:Bitmap, Map #1:Bitmap, Map #1:Bitmap, Map #1:Bitmap, Map #4411:Bitmap, Map #4412:Bitmap, Map #4413:Bitmap)
-- Error occurred in i loop; filename: ; position: 777; line: 19
--  Frame:
--   new2filename: undefined
--   i: 13
-- Unable to convert: undefined to type: FileName

BTW do you know why the listener stops outputting when you run evaluate or eval selected line/code. Have to restart max to get it working.

I can send the test scene if you're willing to help. The way I want the format of the jpgs filenames is 'AM66_*.jpg' so the pattern would need to match 'AM' 'digits' 'underscore' 'anychar' 'ext'. Maybe I'm tired learning maxscript but I can't figure it out, plus the error which is more important.

Getting this to work would be a great timesaver. I tried looking for wxample codes also where say I want:
OldString = "Arch137" as string -- Change to suit
to allow different options that support wildcards. I know this is asking a lot. :)

MANY THANKS FOR HELPING SO FAR.

pixamoon's picture

`

yes sure please send it.

But for renaming files or bitmap filenames in project you can use my Bitmap Tracking/Resizing... or cbuelter't Interactive Renamer

And yes, I know that "listener stops working" I had similar thing many times. What I found out only: it happens mostly if there is mistake in code and than it can't evaluate it or show error.
If I remember sometimes helps to delete what I changed last or try maybe to delete whole code, evaluate only with a = 1 and than undo detete ?? just an idea

also u have undefined error in other line:
new code:

OldString = "Arch137" as string -- Change to suit
NewString = "AM137" as string -- Ditto
theMaps = getClassInstances BitmapTexture
for i in 1 to theMaps.count do (
	--fileName = theMaps[i].filename
	fileName = (getfilenamefile theMaps[i].filename)
	if fileName != undefined and matchPattern fileName pattern:"AM*" ignoreCase:false == false then 
	try(
		newFilename = trimleft fileName OldString
		new2filename = NewString + newFilename
		fileName = new2filename -- renames
	)catch()
)

or

OldString = "Arch137" as string -- Change to suit
NewString = "AM137" as string -- Ditto
theMaps = getClassInstances BitmapTexture
for i in 1 to theMaps.count do (
	--fileName = theMaps[i].filename
	fileName = (getfilenamefile theMaps[i].filename)
	if fileName != undefined and matchPattern fileName pattern:"AM*" ignoreCase:false == false then (
		newFilename = trimleft fileName OldString
		new2filename = NewString + newFilename
		if new2filename != undefined do fileName = new2filename -- renames
	)
)
3dwannab's picture

FOUND A SOLUTION TO THE LISTENER NOT WORKING

Place the cursor in the listener and hit escape a couple of times. :]

3dwannab's picture

:{([

Neither of those codes worked. I'm stumped. Sending scene and thanks for the trick. I tried evaluating a line of code I know was okay but I'll try your method next time. Don't mind the state of the scene I decimated the model.

AttachmentSize
rename_prob_scene_2015.zip 2.44 MB
pixamoon's picture

`

:( I don't have 2015 to test it

But looked in the the file with Library Track/Relink. And could see your assets list.
I test names in new scene with this new code:

OldString = "Arch137"
NewString = "AM137"
theMaps = getClassInstances BitmapTexture
for o in theMaps do (
    fileName = (filenamefrompath o.filename)
    --change file name only if match oldstring pattern  + "*"
    if fileName != undefined and matchPattern fileName pattern:(OldString+"*") ignoreCase:false then
        try(
            newFilename = trimleft fileName OldString
            o.filename = pathConfig.normalizePath ((getfilenamepath o.filename) + "\\" + NewString + newFilename)
        )catch()
)

Sorry I didn't noticed this before - you had:
if matchPattern fileName pattern:"AM*" ignoreCase:false == false then
It was trying to trim and rename all files other then "AM*"
I think it should look for oldString to machtpattern. Than will try to trim only files you want to rename...
if fileName != undefined and matchPattern fileName pattern:(OldString+"*") ignoreCase:false then

Also for renaming you can use Bitmap Tracking Resizing or Library Track/Relink...
They have much more options to rename files in proj or on HDD etc.

Just a small screenshot how to replace in filename:

3dwannab's picture

Thanks, the idea behind this

Thanks, the idea behind this .ms file is to run it with Batch it max over multiple directories and therefore saving tonnes of time in the long run :)

So far I've done a script to clean the assets how I like them found here: http://www.scriptspot.com/3ds-max/scripts/asset-cleaner minus the UI of course to work with BatchItMax and rename objects based on 'Filename + node type' and others like using relink bitmaps to find missing maps. and of course filter set to none and blur to 0.0. Things I find myself doing all the time on an individual basis.

Thanks for spotting the AM. I could replace that with:
pattern:("*"+OldString+"*")
to search that string anywhere in the string replacing any instances of it. I'd love if I could get regular expressions to work seeing as I know how to do that :) but maxscript is a different league but baby steps, baby steps. I've attached the scene in '14 format.

The new code is still throwing an error at: fileName = (filenamefrompath o.filename) Bangs head!!

AttachmentSize
rename_prob_scene_2014.zip 2.46 MB

Comment viewing options

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