Detect certain existing variant of Instance

Hello there!
So I’m working on simple plugin which will change our brandings depending on your choice in plugin UI.
Now I’m facing the issue if the instance doesn’t have a certain variant called “Brand” it will return the error. It can’t change something that is not there, right?

So I’m wondering if there’s any possibility to build some “if” which will basically say: “If instance has certain variant (in our case called Brand), do something. If not leave it be.”

Hope it you can help. Thanks!

There is. Think about the layers on the left panel for a bit. If you had a folded layer for the instance and wanted to figure out if there’s a variant “Brand” somewhere in there, what would do?

Well… just as you would start at the top layer for the instance, expand it,
and look at the child layers, and...

1. for each layer ("let's call it L") you will ask:
2.   is L the variant ? then
3.     yey! "you are done!"
5.   but if not...
6.     # you'll go inside that layer and check its inner layers, right?
7.     # exactly like you did with L !
8.     so you can send this layer all the way up to line 1 
9.     pretend it is now L an run the same steps on it!

That’s recursion :slight_smile: and you can do the same thing in code. Just instead of clicking to see which folders are inside an instance layer, you’ll look in node.children ask: instance.children.some(child => child == Brand variant)

NOTE: That may be very slow if you have too many instances with many, many layers inside them to check. It would be much better if you could add a boolean property to the component that says if it should be update or not and check for that.

Hope that helped! If you have any questions, after you research “recursive algorithm to find nodes in trees”, feel free to ask! :wink:

1 Like

Thank you so much, will try to dig deep into it and let you know!

1 Like