Importing GH components from a existing file to the current one

Hi,

I am currently trying to import the components in a GH file to another one and was wondering if someone who tried to do this or similar could help me on this as I am not very familiar with GH API.
I am trying to have multiple files associated to this component so you can bring the components in the file when necessary by selecting the right file path.
Please let me know if this would be possible.

Many thanks!!
Sun

Can you explain a bit more about what you’re actually trying to do, please?

What do you want to happen when the original imported component changes? Do you want the imported versions of it to change everywhere else as well?

It’s possible if the imported components stay as they are, or if they’re from a plug-in. It’s just fiddy and a bit of work to adjust the imported component’s position, and e.g. get your Python code into a GhPython component.

Once you have a reference to the current document (this is different to the magic variable ghdoc):

GH_doc = ghdoc.Component.Attributes.Owner.OnPingDocument()

you can search for the current component you want in:

GH_doc.Objects

You just need a reference to your “existing file” instead.
Once you’ve got one, you can make a copy of it and pass this to a call to:

GH_doc.AddObject

I’m not sure how to get a reference for a different “existing file” without searching myself, but they’re all there somewhere in the GH API.

Thank you for the reply!
I will try to dig based on what you have shared :grinning:

To add some additional info, the intention is to make GH Python Component which helps people gain access to template script files having various function.
For the updating issue, I have not thought about it in depth. The function is completed once loading the existing components from a template script file similar to Import function in Rhino. However, since the template script files will be updated once in a while, users will be able to access the most recent version of the them. If you have some other suggestion on this, please share. Thank you!!

You’re welcome. What do you mean by a template script, or what’s it a template for?

What I did was make a general GhPython component that looks up its own name, and imports a Python package . The package set MyComponent with its runscript method to a something that looks up a different function (or even a sequence of functions) depending on the component’s name. Most of the plug-in’s components are almost exactly the same GhPython component, except for the icon, name, help data, description and input and output params.