selection prefix

I want to select objects based on their name.

Example:
I have a master control called "Bunnies_ctrl"

I have 3 helper objects called "Bunnies_helper"
I have 3 helpers objects called "Bunnies_temp"

First how do I collect the prefix of the selected objects name? I want to collect all the characters until i get to the underscore "_" so the result of the variable would then be "Bunnies"

Then I would use that variable to select the other helper objects. For example
select "Bunnies" + "_helper"
select "Bunnies" + "_temp"

The reason I want to collect the prefix is because it changes from "Bunnies" to "Donuts"

Comments

Comment viewing options

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

Search name up to "_"

How could I do this then.
Say for example I have all these objects in the scene

"donut_helper01"
"bunnies_pt04"
"cup_ctrl06"

I want to be able to select those objects and have a function that automatically finds the prefix and then stories it in a variable. That prefix is all the characters up till it his the underscore "_". Because the names of the objects could be anything but they always will have an underscore breaking up the prefix.

so the result would be
"donut"
"bunnies"
"cup"

rollout rlTestName "Name"
(
  local ObjName = undefined
 
  button btnFindName "Print Name"
 
  on btnFindName pressed do
  (
    ObjName = selection[1].name --the prefix only
    print ObjName
  )
)
createDialog rlTestName

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

JokerMartini's picture

I got it

I got it figured out here. Thank you very much for your help brot.

ObjName = $.name as stringStream
ObjPrefix= readDelimitedString ObjName "_"

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

br0t's picture

hey thanks for showing that

hey thanks for showing that one :P

Never get low & slow & out of ideas

br0t's picture

Hey, to find a string within

Hey,

to find a string within another string you can use the "findString" method.
It will give you an index to work with, if the string is found. You could do a search for a "_" in the name of an object and, if it is found, all characters of the objects name up to that index are your prefix. You could then use a for loop from 1 to index-1 to construct a new string that is your prefix, or use the "substring" method to extract that part of the name.

To change part of object names you could do a search and replace like this:

(
	--UI:
	local searchFor = "Bunnies"
	local replaceWith = "Donuts"
 
	--loop through object names:
	for i=1 to selection.count do
	(
		local startIndex = findString selection[i].name searchFor -- returns the starting index of the found string
 
		if startindex != undefined do
		(
			-- replace the string at "startIndex" which has a length of "searchFor.count" with "replaceWith" 
			selection[i].name = (replace selection[i].name startIndex searchFor.count replaceWith)
		)--end if
	)--end for
)

Cheers

Never get low & slow & out of ideas

Comment viewing options

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