Why does this give a runtime error on the 2nd run after you undo?

Why does this give a runtime error on the 2nd run after you undo?

KasperFilipsen
Enthusiast Enthusiast
1,201 Views
11 Replies
Message 1 of 12

Why does this give a runtime error on the 2nd run after you undo?

KasperFilipsen
Enthusiast
Enthusiast

I'm working on a script where I select the nodes in layers and process them.

I noticed that if I run this script, it works fine, but if I then Undo and try to run it again. I now get a Runtime error: Attempt to access deleted scene object

 

(
	(LayerManager.current).nodes &theNodes
	maxOps.cloneNodes theNodes cloneType:#instance actualNodeList:&currentObjs newNodes:&newObjs
	select newObjs
)

 

 

Does the LayerManager not update after you undo?
Can you force it to update somehow?

 

My current workaround is to basically make a new array where I check for isValidNode on each node.

Accepted solutions (1)
1,202 Views
11 Replies
Replies (11)
Message 2 of 12

klvnk
Collaborator
Collaborator

don't get any issue with....

theNodes = #();
newObjs = #();
currentObjs = #();
(LayerManager.current).nodes &theNodes
maxOps.cloneNodes theNodes cloneType:#instance actualNodeList:&currentObjs newNodes:&newObjs
select newObjs

 

0 Likes
Message 3 of 12

KasperFilipsen
Enthusiast
Enthusiast
Yeah, it seems to work fine if you run it in global scope, but as soon as you wrap it inside its own scope, it fails.
0 Likes
Message 4 of 12

klvnk
Collaborator
Collaborator

nice not only do we have to solve the issue we have to second guess your code.  😕

0 Likes
Message 5 of 12

KasperFilipsen
Enthusiast
Enthusiast
Yeah I apologize, I'm so used to running everything inside it's own scope I didn't think to wrap the example here. Edited.
0 Likes
Message 6 of 12

klvnk
Collaborator
Collaborator

no probs grumpy monday morning 😉 adding a gc() before initializing the arrays seems to work btw,

 

though will cause issues if you do it twice in a row 😕

 

I think going with 

 

local temp = for n in theNodes where isValidNode n collect n;

 

after grabbing the layer nodes is still your best bet :?

 

0 Likes
Message 7 of 12

klvnk
Collaborator
Collaborator
Accepted solution

the reason it works like this is, layers do not maintain a list of nodes they contain as such, instead each node in the scene maintains an reference to an *ILayer (the layer it's in). so 

 

 

 

 

 

 

(LayerManager.current).nodes

 

 

 

 

 

 

 

 

is initialized by iterating over the whole scene and collecting nodes in the current layer even deleted ones.

 

the mxs interface to layers has the following in its header description

 

 

 

 

This interface is a facade over disparate property sets on layers:

 

 

 

 

😕

 

you can do the same in mxs (but with out the deleted objects) with 

 

 

 

for i in objects where (refs.dependsOn i)[3] == LayerManager.current.layerAsRefTarg collect i;

 

 

 

though I wouldn't trust the hard [3] (it's actually 6 in the sdk so should be 7 😂 ) for all objects 🙂 

0 Likes
Message 8 of 12

denisT.MaxDoctor
Advisor
Advisor

agree with Klvnk... 

for n in theNodes where isvalidnode n collect n is the solution

0 Likes
Message 9 of 12

denisT.MaxDoctor
Advisor
Advisor

Great find by the way. I tweaked some of my methods because of this problem.

👍

0 Likes
Message 10 of 12

denisT.MaxDoctor
Advisor
Advisor

@klvnk wrote:

 

for i in objects where (refs.dependsOn i)[3] == LayerManager.current.layerAsRefTarg collect i;

 

 

 


Probably faster and more reliable:

 

layer_ref = layermanager.current.layerasreftarg
for obj in objects where refs.DependencyLoopTest obj layer_ref collect obj

 

or

layer = layermanager.current
for obj in objects where obj.layer == layer collect obj
0 Likes
Message 11 of 12

klvnk
Collaborator
Collaborator

it was more illustrative than serious 🙂  it's odd the .layer property isn't listed in the mxs help I use 😕

 

edit found it under INodeLayerProperties, weird that they list all the other properties of the INodeLayerProperties interface in the General Node Properties, displayByLayer, motionBylayer etc but not .layer !

0 Likes
Message 12 of 12

denisT.MaxDoctor
Advisor
Advisor

After adding "nested" layers, things became more complicated and non-obvious. So I added my own MXS methods to get layer nodes with the optional "nested" argument.

0 Likes