Notifications
Clear all

[Closed] Maxscript Help – External Txt file Selection Set

Hi,

I’ve been struggling to make this simple bit of code work and finally gave up and decided to come online to ask for some help. Since I’m a noob at maxscripting, please be gentle if it’s something blatantly obvious:).

Purpose:
The purpose of the script is to be able to select a predefined set of objects similar to how Selection Set works, but where the selection set can be passed onto somebody else working on the same scene and be able to select those very parts in their scene.
So the script I am trying to create can select multiple objects based off a list of parts I have in a txt file.

The problem is, whether it works or not is not consistent.
So the conditions are that each object has unique random numbers in their part names.

How it works:

  1. The script will essentially open up the txt file with the list of objects with names and unique random number

  2. It will then add each object name to an array of undefined size so it can grow as needed(somehow maxscript allows this) until EOF via a ‘while not eof’ loop.

  3. Once it reaches EOF it will select all parts that have been added to the array and close the txt file.

So the problem is that once I generate that parts list and run my script, it will give a ‘No ““eof”” function for undefined’ error.

I remember at one point I was able to make it select parts but it’s always been unpredictable.
I made a script before that created the list based on my selection in max and saved it in a txt file and this script (that I’m asking help about) used to be able to grab objects if I created the txt that way but now even that won’t work. Before I thought it was because there might have been some sort of hidden EOF marker present that my maxscript placed into the txt file and this selection script recognizes but I do not think that is the case anymore.

Use of Wildcards
I was also trying to use wildcard () to select objects so if the part was named ‘Sphere_L468569224890’ all it needs to search for would be ‘_L468569224890’ but I couldn’t get that to work either. THe reason this would be handy would be because the object name may change but the unique identifier would not. So Sphere may be changed to a more descriptive and sensible name later on such as ‘Soccer Ball_L468569224890’.

So if anybody can help me out, that would be greatly appreciated!

Thanks in advance!

  • Alex

The Code:

 
-- Script Name: External Selection Set
-- Author: Alex Ting
 
-- CHANGE YOUR PATH HERE!!!-------------------
=====================================
filePath = "C:\ATProj	est	est.max_partlist.txt"
=====================================
-- CHANGE YOUR PATH HERE!!!-------------------
 
-- run starts here -------------------------------
arrayOfParts = #() -- initializing array as a temporary storage of the list of parts being read in from file
 
try
	 file = openFile filePath
catch
(
	 close file
	 messageBox "File cannot be opened. Check the file path of your file!"
	 results = undefined
)
 
clearSelection()
 
while not eof file do
(
	 partName = readLine file
	 blah = execute ("$'" + partName + "'")
	 print blah
	 if blah == undefined do continue
	 arrayOfParts = append arrayOfParts blah
)
 
select arrayOfParts
 
close file

2 Replies

First of all, the line

filePath = "C:\ATProj	est	est.max_partlist.txt"

should be either

filePath = @"C:\ATProj	est	est.max_partlist.txt"

or

filePath = "C:\\ATProj\	est\	est.max_partlist.txt"

or

filePath = "C:/ATProj/test/test.max_partlist.txt"

This is because means “Tab control character” and you need another backslash to escape it, the @ character to define the string as “verbatim” (as is), or use forward slash instead of backslash.
Right now your file name is quite invalid and is probably triggering the error trap, thus closing the file stream prematurely.

Then you should have an IF file != undefined DO () around your while loop to ensure that file was not passed as undefined.

As for the actual code, here are some ideas:

*I prefer writing self-contained scripts for this case – for example, instead of writing a list like

Sphere01
 Box02
 Cone03

and then using a dedicated loader to parse this list, convert each line to a name and add to an array to select, you could instead output an actual MAXScript which, when run, would restore the selection. The output file could look like

try($Sphere01.isSelected = true)catch()
 try($Box02.isSelected = true)catch()
 try($Cone03.isSelected = true)catch()

or

if isValidNode (theObject = getObjectByName "Sphere01") do theObject.isSelected = true
 if isValidNode (theObject = getObjectByName "Box02") do theObject.isSelected = true
  if isValidNode (theObject = getObjectByName "Cone03") do theObject.isSelected = true

Note that (getNodeByName someName) is WAY faster than (Execute (“$’”+someName+”’”)), so if the names won’t be changing, you should be using that.

If you want to go with a dedicated loader and names, you could implement some code that, if getNodeByName returns false, tries to remove the prefix of the name, replace it with “*” and call execute on it (which is slower, but would find ANY objects with the unique number).

The relevant part of the code would look something like this:

--open the file for reading as discussed above...
 theObjectsToSelect = #() --array to store objects to select
 while not eof theFile do --loop through the file
 (
 theName = readLine theFile --read a name
 if isValidNode (theObject = getNodeByName theName) then --try to resolve quickly by name
   append theObjectsToSelect theObject  --if successful, add to array
 else --otherwise try the slow method
   (
 	 theFS = filterString theName "_" --filter by underscore
 	 theSuffix = theFS[theFS.count] --get the last part behind an underscore
 	 theObjects = execute ("$*'"+theSuffix+"' as array") --get all objects with that suffix
 	 if theObjects.count > 0 do --if any objects had that suffix,
 	   append theObjectsToSelect theObjects[1] --add the first one
   )
 )--end while
 select theObjectsToSelect --select whatever was collected
  

There are some ideas to try, let me know how it goes…

Hi BoBo,

Wow, I’ve been looking around for so long and I must’ve been looking in the wrong places (Function Reference, etc). Yes the solution you have provided is brilliant and works and the best part is I understand it b/c of your comments. Thanks for such a detailed response with explanations!

I have to get back to doing more coding and maybe learning Maxscript properly…or pickup Python again…