[Closed] error: "attempt to access deleted scene object"
I have a script (I’m a noob so don’t make fun of my messy script the randomly generates instances of your currently selected object up to a number you specify. Problem I am having is that in an attempt to avoid duplicate objects I have created a checkPosition function that checks the new objects position against all scene objects. This works, problem is in order for the script to continue it must delete the new object and select the previously created object and try again. Works sometime, but sometimes I get the “attempt to access deleted scene object” error as if it is trying to select an object that has been deleted. Take a look.
At this moment the script is setup for a sphere but down the road will be adjusted for any mesh.
gap = .35
objWidth = $.radius2
rollout ObjectBuilder “Object Builder” width:160 height:96
(
button btn17 “Create Form” pos:[21,15] width:115 height:33
edittext NumObjects “Number of Objects” pos:[16,56] width:120 height:16 enabled:true
on btn17 pressed do
createForm()
)
fn createForm = (
totalObjects = ObjectBuilder.NumObjects.text as integer
for i=1 to totalObjects do (
randomNumber = random 1 6
newObject = randomInstance randomNumber
)
)
fn randomInstance r = (
if r == 1 then (duplicateUp())
if r == 2 then (duplicateDown())
if r == 3 then (duplicateLeft())
if r == 4 then (duplicateRight())
if r == 5 then (duplicateVerticalUp())
if r == 6 then (duplicateVerticalDown())
)
fn checkPosition oldPos newPos currentObjects = (
for i=1 to currentObjects.count do (
if currentObjects[i].pos == newPos.pos then (
print “Is duplicate”
delete newPos
select oldPos
) else (
print “Is not a duplicate”
select newPos
))
)
fn duplicateUp = (
objectArray = $ as array
oldObj = $
newObj = instance $
newObj.pos.y = $.pos.y-objWidth+gap
checkPosition oldObj newObj objectArray
)
fn duplicateDown = (
objectArray = $* as array
oldObj = $
newObj = instance $
newObj.pos.y = $.pos.y-objWidth+gap
checkPosition oldObj newObj objectArray
)
fn duplicateLeft= (
objectArray = $* as array
oldObj = $
newObj = instance $
newObj.pos.x = $.pos.x-objWidth+gap
checkPosition oldObj newObj objectArray
)
fn duplicateRight = (
objectArray = $* as array
oldObj = $
newObj = instance $
newObj.pos.x = $.pos.x+objWidth-gap
checkPosition oldObj newObj objectArray
)
fn duplicateVerticalUp = (
objectArray = $* as array
oldObj = $
newObj = instance $
newObj.pos.z = $.pos.z+objWidth-gap
checkPosition oldObj newObj objectArray
)
fn duplicateVerticalDown = (
objectArray = $* as array
oldObj = $
newObj = instance $
newObj.pos.z = $.pos.z-objWidth+gap
checkPosition oldObj newObj objectArray
)
createDialog ObjectBuilder
You could implent the ‘isDeleted <MAXWrapper_object>[b][b]’ method to avoid the error check the maxscript reference for more info on it.
For the rest debug it try ‘print’ with the object name/the ‘isdeleted’ check etc.
[/b][/b]
Here is the stack trace on the error:
-- Error occurred in i loop; filename: C:\Documents and Settings\Rob\Desktop\; position: 839
-- Frame:
-- i: 19
-- called in checkPosition(); filename: C:\Documents and Settings\Rob\Desktop\; position: 951
-- Frame:
-- newPos: <Deleted scene node>
-- currentObjects: #($Sphere01, $Sphere02, $Sphere03, $Sphere04, $Sphere05, $Sphere06, $Sphere07, $Sphere08, $Sphere09, $Sphere10, $Sphere11, $Sphere12, $Sphere13, $Sphere14, $Sphere15, $Sphere16, $Sphere17, $Sphere18, $Sphere19)
-- oldPos: $Sphere19
-- called in duplicateVerticalDown(); filename: C:\Documents and Settings\Rob\Desktop\; position: 1928
-- Frame:
-- objectArray: #($Sphere01, $Sphere02, $Sphere03, $Sphere04, $Sphere05, $Sphere06, $Sphere07, $Sphere08, $Sphere09, $Sphere10, $Sphere11, $Sphere12, $Sphere13, $Sphere14, $Sphere15, $Sphere16, $Sphere17, $Sphere18, $Sphere19)
-- oldObj: $Sphere19
-- newObj: <Deleted scene node>
-- called in randomInstance(); filename: C:\Documents and Settings\Rob\Desktop\; position: 700
-- Frame:
-- r: 6
-- called in i loop; filename: C:\Documents and Settings\Rob\Desktop\; position: 455
-- Frame:
-- newObject: undefined
-- randomNumber: 6
-- i: 9
-- called in createForm(); filename: C:\Documents and Settings\Rob\Desktop\; position: 458
-- Frame:
-- totalObjects: 10
-- called in btn17.pressed(); filename: C:\Documents and Settings\Rob\Desktop\; position: 278
-- Frame:
>> MAXScript Rollout Handler Exception: -- Runtime error: Attempt to access deleted scene object <<
BTW, use the CODE tags to mark code, to keep spacing, please.
It tells you pretty much all you need to know, just read it closely and follow it from the top to the bottom. This isn’t really a complicated error, you just need to work through your code.
Sorry, I’ve not read the code fully, but, instead of creating the obj THEN check the position, why not check the position first before creating the object and only creating it when you have a valid position…??
You have the “master” object (I assume) to base additional size comparisions on…
Shane
whenever you’re working with nodes that have a possiblity of not existings, you should check to see if they do or not with a simple if…
If ( isValidNode currentObjects[i] ) then
(
-- do stuff
)
Don’t forget to space and use those brackets… it’s not only easier on us, but you too… pressing control-B will show you everything with in the bracket the cursor is in… great for debugging if you get in the habit of using () all the time…
Thanks for all the assistance. I got it working the way I want. Mostly my function design was the issue, though your comments helped me see my mistakes more clearly. Here’s the new code:
gap = -1
–objWidth = $.radius2
objWidth = $.width
rollout formBuilder “form Builder” width:160 height:124
(
button btn17 “Create Form” pos:[21,15] width:115 height:33
edittext NumObjects “Number of Objects” pos:[3,56] width:152 height:16 enabled:true
progressBar pb1 “ProgressBar” pos:[8,80] width:144 height:16 enabled:true value:0
on btn17 pressed do
createForm()
)
fn createForm = (
totalObjects = formBuilder.NumObjects.text as integer
for i=1 to totalObjects do (
formBuilder.pb1.value = 100i/totalObjects
randomNumber = random 1 6
newObject = randomInstance randomNumber
)
)
fn randomInstance r = (
if r == 1 then (duplicateUp())
if r == 2 then (duplicateDown())
if r == 3 then (duplicateLeft())
if r == 4 then (duplicateRight())
if r == 5 then (duplicateVerticalUp())
if r == 6 then (duplicateVerticalDown())
)
fn checkPosition oldPos newPos = (
currentObjects = $* as array
endArray = currentObjects.count-1
for i=1 to endArray do (
if ( isValidNode currentObjects[i] ) then (
if currentObjects[i].pos == newPos.pos then (
print newPos
delete newPos
select currentObjects[random 1 endArray]
exit()
) else if ( i == endArray ) then (
select newPos
exit()
)
)
)
)
fn duplicateUp = (
oldObj = $
newObj = instance oldObj
newObj.pos.y = oldObj.pos.y-objWidth+gap
checkPosition oldObj newObj
)
fn duplicateDown = (
oldObj = $
newObj = instance oldObj
newObj.pos.y = oldObj.pos.y+objWidth-gap
checkPosition oldObj newObj
)
fn duplicateLeft= (
oldObj = $
newObj = instance oldObj
newObj.pos.x = oldObj.pos.x-objWidth+gap
checkPosition oldObj newObj
)
fn duplicateRight = (
oldObj = $
newObj = instance oldObj
newObj.pos.x = oldObj.pos.x+objWidth-gap
checkPosition oldObj newObj
)
fn duplicateVerticalUp = (
oldObj = $
newObj = instance oldObj
newObj.pos.z = oldObj.pos.z+objWidth-gap
checkPosition oldObj newObj
)
fn duplicateVerticalDown = (
oldObj = $
newObj = instance oldObj
newObj.pos.z = oldObj.pos.z-objWidth+gap
checkPosition oldObj newObj
)
createDialog formBuilder
If you want to see some forms I made with it go HERE