Maxscript: callbacks.addScript #nodeLayerChanged fn calling constructor

Maxscript: callbacks.addScript #nodeLayerChanged fn calling constructor

Anonymous
Not applicable
1,109 Views
2 Replies
Message 1 of 3

Maxscript: callbacks.addScript #nodeLayerChanged fn calling constructor

Anonymous
Not applicable

When calling sphere() in combination with callbacks.addscript #nodelayerchanged max crashes. Simple prints and manipulation of existing nodes for example move works if you put them inside the function but not constructs.

Example:

 

macroScript NewTest
Category: "TestScripts"
(

global RunThis
fn RunThis =
(
sphere()
)--end fn


fn disable_event = (callbacks.removescripts #nodeLayerChanged id:#TestID)
fn enable_event = (callbacks.addScript #nodeLayerChanged "(RunThis())" id:#TestID)

disable_event()
enable_event()

)--end script

0 Likes
Accepted solutions (1)
1,110 Views
2 Replies
Replies (2)
Message 2 of 3

michaelsonbritt
Advocate
Advocate
Accepted solution

Notice that your script triggers when creating a new object from the Create panel.  New objects need to go onto a layer, so that counts as a layer change.  So it's simple what is happening: infinite recursion.  You create a Sphere which triggers your script, which creates a Sphere ... and so on.  The simple solution is to check whether you're inside a recursion already and stop.  See below.

 

But notice this goes a bit crazy.  Creating any object leads to creating two spheres.  That's because the Create panel operates by performing a create-undo-create-undo sequence of operations (that gets pretty technical).  And your script also triggers when deleting objects.  You can try to delete all the extra spheres, but that creates more spheres.

 

Maybe if you provide more background on the goal you're trying to accomplish, the community can provide guidance on best approach?

 

Cheers,

Michaelson Britt

macroScript NewTest
Category: "TestScripts"
(

global RunThis
fn RunThis =
(
	global StopRecurse
	if StopRecurse != true do
	(
		StopRecurse = true
		sphere radius:4 pos:[random -50 50, random -50 50, 0 ]
		StopRecurse = false
	)
)--end fn


fn disable_event = (callbacks.removescripts #nodeLayerChanged id:#TestID)
fn enable_event = (callbacks.addScript #nodeLayerChanged "RunThis()" id:#TestID)

disable_event()
enable_event()

) --end script
0 Likes
Message 3 of 3

Anonymous
Not applicable

Thank you for the comprehensive answer!

My goal is to implement a workflow based on a created layerhierarchy.

When certain objects gets placed into certain layers scripts will get triggered.

Most triggered events will create geometry or import/merge max objects .

 

For example: A closed spline gets placed in layer Grass, a function triggers and imports a maxfile containing a predefined scatterobject and applies the closed spline as a scatterboundary for the scatterobject. 

 

I have it working ,only thing left to solve is the eventrigger,foolproofing and optimised layout of the scripts the workflow is using.

 

I tried NodeEventCallback  and now callbacks.addscript but they seem to complex/unstable for me.

Is there some other way to trigger events or how do i go about learning more about callbacks and events,any good tutorials out there ?

 

0 Likes