Trimming group name


#1

Stupid question here,

I have files where the groupnames are that of the filename. I’m trying to trim the filename and an underscore from ONLY grouphead’s names but somehow can’t get the script to work.

  basename = getFilenamefile(maxfilename)
  for o in selection where isgrouphead o do
  	(
  	o.name = trimleft (basename + "_" as string)
  	)

But trimleft removes everything after the underscore. If I use trimright, the same issue.

It’s actually doing the opposite of what I want. I have tried searching about string but that goes a little above my head.

Ive tried replace string but cant figure out the digits in the code e.g. From maxhelp

s=“1234567890”
s1=replace s 5 3 “inserted string”

So i could for example count the filename characters but that would be extra work. Any suggestions how I would be able to use replace?

Thanks


#3

Hi :slight_smile: no such thing as silly questions here

trimleft documentation:

http://help.autodesk.com/view/3DSMAX/2017/ENU/?guid=__files_GUID_09174DC2_033E_4EA5_ACBB_AA840A15D070_htm

Trimleft is not quite the thing you’re after because it’ll remove all leading instances of the characters you want to remove - for example try this:

o = "my    filename001" 

print (trimleft o " mfleyi")    
    
-- output: "name001"

What you really want is substring, assuming you know the characters you want to remove are always at the start of the group name:

(
    basename = getFilenamefile(maxfilename)
    for o in selection where isgrouphead o do
    (
        o.name = substring o.name 1 basename.count
    )
)

I hope that helps point you in the right direction


#4

Thanks for the help :slight_smile: I messed around with your snippet and it works now but im very confused:

(
basename = getFilenamefile(maxfilename)
for o in selection where isgrouphead o do
(
o.name = substring o.name (basename.count+2) 50
)
)

Your snippet did the exact same as mine so I reversed the order of the digit (1) and basename count and it removed the whole name except for 1 character. ???

e.g. Filename ‘kitchen-3000-ar1920-fold.max’

groupname is '[kitchen-3000-ar1920-fold_sink]

After running your script the group names became ‘[d]’

I changed the digit to 5 and the name turned into ‘[d_sin]’

So had to add (basecount+2) so that it would remove the name and the underscore at the end and 50 for the object name’s character number. IT works but the code is just confusing and probably incorrect. Would appreciate some explanation here lol


#5

The documentation for substring:

https://help.autodesk.com/view/3DSMAX/2016/ENU/?guid=__files_GUID_A6A60FC7_6206_4FFC_80E2_0EF8544BE2C4_htm

<string>substring <string> <from_integer> <length_integer>

in order, that means

  • it’ll return a string
  • the name of the function is “substring”
  • you pass it a string to search through
  • it’ll return the characters starting at <from_integer> and the next <length_integer> number of characters

Just thinking - now that I see your group name has square brackets and it looks like you want to retain them, you could use filterstring instead, much easier

splitstring_array = (filterstring groupname "_")
print splitstring_array
--output:
-- "[kitchen-3000-ar1920-fold"
-- "sink]"

newname = "[" + splitstring_array[2]
print newname
-- output: "[sink]"

#6

Apologies for the late reply, family emergency.

Currently the script works as intended (havent heard any errors or bugs yet) but when I have a little more free time I will fool around with your filterstring/splitstring solution, thank you so much for the feedback, it’s really appreciated. :slight_smile:


#7

Hey not a problem, I hope everything’s ok

Anymore questions just shout :slight_smile: