[Closed] Accessing digits in a string
I need to automate an alignment of a selection of objects (point01, point02, etc) on a defined selection of copies of one object(Line01, Line02, etc). The numbers of points and lines are the same.
How can I access the digits in the names of the objects? My try now is to trimleft the names and convert the remaining digits into variables, but I can’t get that to work.
Any ideas?
That approach should work…
You need to post your current code here, as it is hard to say what is wrong without that.
Are you creating these objects (via maxscript or otherwise), or are they imported? (If you are creating them yourself, there is a better approach to use than this.)
Yes, I create them by myself in a script.
this is the part where I create the splines
for i in 1 to numParts do
(
newShp = splineShape()
addNewSpline newShp
select newShp
newShp.name = ("ParticleSpline_" + i as string)
)
What I need to do is align the pivot to the first vertex(solved), create a plane(solved too) on the same position like the spline. The number of planes and splines should match, which isn’t a problem because I can do this in this “for i in 1 to numParts” part, but I need to apply a PathDeform(WSM) and according to the number of the plane I want to use the spline with the same number.
Ok, here are two suggested approaches…probably one or the other, but not both.
- If you absolutely must use the names to extract the number suffixes and convert them to integers, this approach will work…as long as you know exactly how many characters to ignore:
(
local MyPoint = (point name:("MyPoint"+((random 1 50) as string)))
local PtName = MyPoint.name
local NumStr = ""
for i=8 to PtName.count do (NumStr+=PtName[i])
local NamNum = (NumStr as integer)
format "%
" NamNum
)
- If you simply need to match “Point01” to “Line01” and so on, I would recommend storing the object references in a multidimensional array. This approach allows you to organize object references in an exact order and continue to access them as needed, so you’d never have to rely on object names.
(
global MyArray = #()
local NumObjs = 5
for i=NumObjs to 1 by -1 do (MyArray[i] = #())--preinitialize the array size and fill with 5 nested arrays
for i=1 to NumObjs do
(
local CurPyr = MyArray[i][1] = (Pyramid name:("MyPyramid"+(i as string)))
CurPyr.width = 10
CurPyr.depth = 10
CurPyr.height = 10
CurPyr.pos = [(-75+(i*25)),(75-(i*25)),0]
CurPyr.wireColor = blue
)
for i=1 to NumObjs do
(--every command in this loop accesses Pyramid properties via the array
local CurPyr = MyArray[i][1]--temporary variable for Pyramid in the current subarray
local CurDum = MyArray[i][2] = (Dummy name:((CurPyr.name)+"cap"))
CurDum.boxSize = [(CurPyr.width),(CurPyr.depth),1]
CurDum.pos = [(CurPyr.pos.x),(CurPyr.pos.y),(CurPyr.height)]
)
)
I’ve always found it easiest to have a delimiter in the name string like an unscore or dash and use the filterString function.
MyPoint = (point name:(“MyPoint-”+((random 1 50) as string)))
tn = filterString Mypoint.name “-”
local MyInt = ( tn[2] as integer )
or
local MyInt = ( (filterString Mypoint.name “-”)[2] as integer )
Good luck
Keith
Sorry for the late reply, was busy.
Not sure which of both I will use. Keiths solution looks easier to me.
I need this to create a quite large amount of guides for feathers, so I would like to take the one which requires less memory or works faster. Being kinda new to this scripting stuff I also don’t know what advantages using arrays would have for my purposes(if any).
If you have a large number of objects, the array approach will be much faster. Parsing each Point name string to construct the corresponding Line name string is slower by itself, but then you also have to use getNodeByName() function which loops through every object in the scene until it finds the first one with that name, and this is done for every object you try to find using that function.
I agree with the arraying, if you are generating the “points” from the beginning. If you are trying to pull from an existing scene of objects that have a standardized naming, standardize that naming with delimiters…
Just a response to the inital question, not your specific usage.
Good luck
Keith