I’m trying to find a good way to “explode” custom class objects in grasshopper. Sometimes you want to see all public properties on a custom class instance.
I have tried this one: ExplodeAnything | Food4Rhino and it is not bad, but there are some issues with it. For example, if the object property is a list of objects, it just outputs it as a list-object instead of a list of objects, and this makes it really hard to work with when there are multiple properties and some are lists sometimes and sometimes not.
Learn to use a (.Net) Debugger or Decompiler and no need for this “crap” .
But ExplodeAnything does something very simple:
using System;
using System.Reflection;
public class Program
{
public static void Main()
{
FancyObject obj = new FancyObject();
// This returns any member of FancyObject
MemberInfo[] member = typeof(FancyObject).GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
foreach (var m in member)
Console.WriteLine(m.Name);
}
// -> Example object
public class FancyObject
{
private string _password = "Cheese";
public Guid ID {get;}
public FancyObject()
{
ID = Guid.NewGuid();
}
private void DoSomethingSecret()
{
Console.WriteLine("Password is " + _password);
}
}
}
Write to the developer in food4Rhino or by email and ask for this, although I can anticipate the problem of what happens when the list contains several different types.
Thank you for this! Yes, the ExplodeAnything component is probably not that complicated but I still don’t really know how it works because it at least needs to set the outputs from the component. I would ideally like to not use this at al but rather just a python or C# snippet. Do you have any clue about how to set the outputs dynamically and to handle lists as outputs?
Thank you for your reply! That’s not a bad idea! However, I’d ideally like to not use any 3rd party libraries at all (the code will be run on a server so vanilla gh would be the neatest because it’s easy to maintain). Do you have any idea about how this can be done in in .NET or python with dynamic outputs?
Haha don’t worry, it’s not that bad. The only reason I mentioned that we are running it on a server is that we’d ideally want to avoid using 3rd party modules as far as possible. What I meant with “dynamic outputs” is that I’d like to have a component that does the following:
Takes a generic class instance as the input.
Checks all public properties on that object and
Sets one output for each of these with the same name as the property.
I know that I’m asking for a lot but I was just thinking that someone might have a snippet ready to go that does just this since it is a fairly generic issue.
Thank you for your reply! Yes, it is something like this that I’m looking for, but it needs to be a bit more generic and set the outputs dynamically based on the properties of the class. Basically this:
Takes a generic class instance as the input.
Checks all public properties on that object and
Sets one output for each of these with the same name as the property.
If one of the outputs is a grasshopper object or a list of grasshopper objects, I’d also like it to cast it to this type ideally. I’m aware that I’m asking for a lot haha.
I remember reading some time ago about changing output descriptions and values. That seems to be possible. The thread should float around here somewhere.
What I don’t know is, if it is even possible to dynamically create (or delete) component outputs in GHPython. @piac or somebody else from the dev team might be wiliing to tell you?
If you create your own plugin component all you need to do is to create the parameter list automatically instead of hardcoding them as being done in the manual/tutorials.
This will never work properly from Script component due to its special nature of the piping mechanism.
I mean I could invest some time to provide you an example, I made it work some years ago for a F# script component. However I really question the purpose of this. What is the point of this?
Seems to be a typical workaround to workaround…
If you want this you use an API Documentation, Autocompletion or better a Debugger or an Decompiler.
Thank you for taking the time to help out! I think this is a really great solution if I can get it to work. I’m not sure if I’ve been clear enough about the case though or if I simply don’t understand how to use this properly. The classes that I’m working with are defined in a toolkit that I have as a plugin in C#. When I try to get the dict property, python does not know the class of the object since it is only passed in as an generic object and I therefore cannot run the vars(object) function or object.__dict__ on it. In the image below for example I have an instance of a class called MultiFamily (which is supposed to be a multi-family house) and it has public properties such as
Perimeter of type Rhino.Geometry.Curve
Apartments of type List<MyToolkit.Apartments>
EntrancePosition of type Rhino.Geometry.Point3d
and so on. Could I still use this somehow to get these properties to a dict and access them from there. I could of course in C# or python just create a component that takes an instance of the class and outputs its values for all properties, but I have probably around 10-20 different classes in the toolkit so far so I thought it would be neat to just have one dynamic component that does it for all of them.
Wow! Thank you so much for taking the time to provide this, I really appreciate it and the code that you provided. I don’t understand all of it but most of it.
If you create your own plugin component all you need to do is to create the parameter list automatically instead of hardcoding them as being done in the manual/tutorials.
Yes, this is exactly it, should probably have been more clear about that in the beginning. I’ll have a look at how this can be done.
This is want the output looks like for your script component:
I’d be happy to share a definition with the MultiFamily instance with you, but it is not possible to internalize. However I created a custom class that I added to the script that should do pretty much the same thing if you’d like to have a look.
However I really question the purpose of this. What is the point of this?
Just to clarify this, the reason why I want this is that I have a toolkit with a bunch of different classes that have public properties, and sometimes I need to know explicitly what these are in my script. I could ofc create a component for each class that just returns these, but I thought it would be really neat and maybe useful for others as well to have a dynamic component that can do this for all classes with public properties.