Multiple instances of a custom preview gh component


I am scripting in C# a component that has a lot of custom previews implemented within a display pipeline (please refer to this thread). I noticed that when I instantiate this component multiple times, either by inputting multiple objects (from outside the component) or by simply copy-pasting it, the display pipeline is completely messed up. It seems impossible to preview more than one instance of the same script/gh component.

Any hint on how I can fix this?


Are you using static fields? Without seeing the code there’s precious little I can say.

Here is the part where I define all the previews.
All the lists/variables are coming from public static fields, I am not using any private methods.
I do clear those lists/variables in RunScript, so the previews don’t accumulate once I change a variable outside the component. This surely explains why I can’t create multiple instances of the all thing, it will remember only the last one since it clears everything each time it runs.
However I still do not get why the same problem appears when I just copy-paste the component and change one of the inputs. :confused:

Why not disable and enable the depth testing once, at the start and end of the code?
And why are you using ++i in your loops instead of i++?

At any rate, I still can’t do anything with this code, because it’s pixels and partial.

That’s to be sure that each object/list is appearing in the order i typed them. I decided to do that following this discussion. I guess it’s hacky and is maybe not the way the order of appearance should be managed, but it works. I followed your advice but it did not solve anything (and messed up the order of appearance).

That was a mistake. Changed it, but did nothing…

Sorry, I did not find a way to post formatted code so I thought a screenshot would be more insightful. I will send you a message with the whole thing.

See this post for information on that:

1 Like

Here is a simple example that relate to the same problem.
Why is the mesh not displayed twice? It only does it for one instance.
You can find the code attached. (8.5 KB)

you are only storing one “mesh” in your global “inputMesh” variable - remember that when item access is supplied, and you provide a list, the “RunScript” method executes multiple times, effectively overwriting the variable after each run. You can either provide list access and store a list, or simply populate a list instead of a single mesh reference. The native CustomPreview component does something like the following:
In SolveInstance (akin to runscript in a compiled GHA):

  • check if it’s the first time “solveInstance” is executing in a given run - if it is, clear the list holding all geometry
  • populate a List where PreviewObject is a struct containing a piece of geometry and a color

In DrawViewportWires / DrawViewportMeshes:

  • iterate over the list of PreviewObjects and draw each one