How to set component options programmatically?

I am able to instantiate components on the canvas with python code.
What I have not been able to figure out so far is how to (un)set component options like these:

I assume it must be possible somehow. Does anyone know?

It depends on the API for each component Class. But when creating Python components in bundled ‘dev-ops’ code, I just set the attribute on each one before adding it to the canvas or creating a user object etc.

Thanks for your reply @James_Parrott

Unfortunately I’m not sure what you mean by “bundled ‘dev-ops’ code“. – If you have a code example (setting attributes, creating user objects) you might want to share, that would be greatly appreciated.


        gh_python_comp = GhPython.Component.ZuiPythonComponent()
        gh_python_comp.Code = tool_code
        gh_python_comp.IsAdvancedMode = True

        ...

        gh_python_comp.Description = description
    
        gh_python_comp.SubCategory = subcategory 
        gh_python_comp.Category = plug_in_name


        gh_python_comp.Locked = locked 

        gh_python_comp.NickName = name
        gh_python_comp.Name = name   

        GH_doc = ghdoc.Component.Attributes.Owner.OnPingDocument()
        GH_doc.AddObject(docObject = gh_python_comp, update = False)

This shows positioning them:

    GH_DOC = ghdoc.Component.Attributes.DocObject.OnPingDocument()
    
    # search Grasshopper.Kernel.GH_ComponentServer().ExternalFiles(True, True)
    # e.g. for the file_obj with a particular plug-in name in its path                
    user_obj = Grasshopper.Kernel.GH_UserObject(file_obj.FilePath)

    comp_obj = user_obj.InstantiateObject()
    
    comp_obj.Locked = False
    
    
    
    sizeF = System.Drawing.SizeF(x, y)
    
    
    comp_obj.Attributes.Pivot = System.Drawing.PointF.Add(
                                            comp_obj.Attributes.Pivot,
                                            sizeF
                                            )
    
    success = GH_DOC.AddObject(docObject = comp_obj, update = False)
    
    if not success:
        raise Exception('Could not add comp: %s to GH canvas' % comp_obj)

I figured out how to get the menu items:

import clr
clr.AddReference('Grasshopper')

import System
import Grasshopper as gh
import System.Windows.Forms as WinForms

doc = gh.Instances.ActiveCanvas.Document

compName = 'Graft Tree'
proxy = gh.Instances.ComponentServer.FindObjectByName(compName, True, True)
comp = proxy.CreateInstance()
comp.CreateAttributes()
comp.Attributes.Pivot = System.Drawing.PointF(100, 100)
doc.AddObject(comp, False)

# https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.toolstripmenuitem?view=windowsdesktop-10.0#properties
try:
	menu = WinForms.ToolStripDropDown()
	comp.AppendAdditionalMenuItems(menu)
	for item in menu.Items:
		item.Checked = False # <- has no effect
		print(item.Text, item.Checked)
	doc.NewSolution(True)
except:
	print('failed')

But unchecking them seems to have no effect, unfortunately.

So I’m still looking for a way to access the underlying pieces of data directly in order to flip the flags on or off.

@DavidRutten How would one do that?

What does dir on the menu items show? Is there a setter or a toggle method that’s not associated with assignment in IronPython?

Aha! – calling .PerformClick() on the menu item seems to do the trick.

Still curious about better approaches that don’t involve simulating clicks in the UI.