[Closed] checking if an array exists
I want to check if an array of objects exists in a scene. The options that I’ve tried in max only seem to work on an individual object (getNodeByName and isValidObj) which means that I can get the array to pass as true for only one object at a time. For example if I want to print a message if the array is true, I can only make it print the message for every object in the array. What I want is for the array to test true as an array of objects and print the message once.
Any ideas?
:¬)
I am still not sure I understand you correctly.
If an array contains pointers to scene nodes and you load a new scene for example, all pointers become invalid and the array will contain a bunch of <Deleted Scene Objects>.
If the array contains a bunch of pathnames like $Box01 etc., you evaluate the code and there is no such object as Box01 in the scene, the array will contain undefined for that entry.
In both cases, asking isValidNode for each element of the array will give you an idea if all objects in the array are valid or not.
if (for o in theArray where not isValidNode o collect o).count > 0 do
messagebox “Array Contains Missing or Deleted Scene Nodes”
This loops through all elements of the array and collects the invalid entries. If the count of the resulting array is greater than zero, there were invalid entries in the array…
Or am I missing something?
Can you not just check for an undefined? ie:
if array01 != undefined then…
Is the array you’re looking for in a scripted rollout/dialog? If so you may need to use the rollout’s name then access the local array as a property of that rollout…
if rolloutname.arrayname != undefined do...
If the array doesn’t exist in a scripted rollout/dialog’s lexical scope then you should be able to access it like any other global variable.
No, it’s just checking if a bunch of objects exists in the scene before running the script. At the moment, I hack it by doing a try catch on the array as a selection. There must be a not so hacky way…
erilaz, if you define the array as a variable, it’s never going to be undefined because you’ve just defined it.
This stuff really mucks my head up sometimes.
8¬P
[color=white]the idea is create an array with the object in the scene call scenearray , later compare who many object in the your array are equal in the scenearray and later add this equal objects to a new array .
if the count of your array and the newarray are equal yo have all the objects in the scene.
[/color]scenearray = #()
for o in objects do (append scenearray o )
newarray = #()
temparray should be your array
for element in temparray do
(
idx = finditem scenearray element
if idx != 0 then
[size=2]append newArray element
)
if newarray.count == temparray .count do ( print “ok”)
brad did you recive my mail ,
take care man
[/size]
I got it!
Thanks for the finditem info Luis/Luigi. Changed a couple of things and came up with this (to find three teapots for example):
found_array = #()
teapot_array = #($'teapot01', $'teapot02', $'teapot03')
for i in objects do
(
if finditem teapot_array i != 0
then
(
append found_array i
)
)
if found_array.count == 3
then messagebox "Success!"
else messagebox "Failure"
The only problem with it is that it’ll still return successfully if there are two objects within the teapot_array with the same name. ie. If I have objects named teapot01, teapot02, teapot03 and teapot01, it’ll still say “Success!”, but that’s something I can live with.
Thanks for the help!
:¬)
the key problem is “same max object names” is not mean “same objects” ,just like u create a box and a sphere and then give them same name :
in this code “finditem teapot_array i”
this command compare the object with the array element , it compare objects ,not name of objects , although the name of object is same but not mean the object is equal ,max allow u use same name of objects but its really not the same one in max .
u can test with only 2 teapots (or two different objects like a box and a sphere),same name ,and use this code :
found_array = #()
teapot_array=#("teapot01", "teapot02", "teapot03", "Teapot01")
for i in objects do
(
if (finditem teapot_array (i.name))!=0 do append found_array i
)
found_array.count
compare code :
found_array = #()
teapot_array=#($'teapot01', "teapot02", "teapot03", "Teapot01")
for i in objects do
(
if (finditem teapot_array i)!=0 do append found_array i
)
found_array.count
maybe helpful to u
Top stuff Bobo, you’re right on the money. You’re solution is much more elegant than what I could muster. It’s probably also quicker as it cycles through the array rather than every object in the scene. I’m not really familiar with the whole “collect” command (I wasn’t really familiar with arrays really when I started doing this!) but I think I understand more about it now.
Thanks.
:¬)