Notifications
Clear all

[Closed] Get All Dependency Objects

 MZ1

I want to get all objects which are related to specific character by passing single part as input:

(
	fn FindDependencies Obj Objs =
	(
		for o in objects where
		finditem Objs o == 0 and
		(refs.DependencyLoopTest Obj o or refs.DependencyLoopTest o Obj)
		do
		(
			appendifunique Objs o
			FindDependencies o Objs
		)
	)
	
	if isvalidnode (Sel = selection[1]) do
	(
		Objs = #()
		FindDependencies Sel Objs
		select Objs
	)
)

That Works on simple hierarchy. But When the rig become more complex with custom attributes and connections, It will crash the Max. I don’t know why this happen.

1 Reply

You have a lot of expensive operations being run in for loops.
Optimising your script will go a long way to making it more stable.
Cache any calls to struct methods:

-- Outside the loop:
depLoopTest = Refs.DependencyLoopTest
for obj in Objects do
(
    -- significantly faster:
    depLoopTest <obj1> <obj2>
    -- than this:
    Refs.DependencyLoopTest <obj1> <obj2>
)

appendIfUnique is also expensive.
Append all your objects and then at the end use makeUniqueArray to get a unique array.

You also recursively call your function, which will mean it will get exponentially slower with each object you add to the loop.

This will get all dependent objects in the scene of the given object:

Fn getDependents inObj =(
    depLoopTest = Refs.DependencyLoopTest
    local dependentObjects = for obj in Objects where (
        depLoopTest obj inObj
    ) collect obj
    dependentObjects -- return
)

Sorry for the formatting, doesn’t seem to want to work properly on my phone.