Jump to content

Python plug-in with widgets (basic example)


Recommended Posts

About text alignments: you can set a plug-in object to respond to font and text size using 

 vs.SetObjectVariableBoolean(gPio_H, 800, True) 

but all other text properties, such as horizontal and vertical alignment, etc. must be coded and driven by parameters.

Link to comment



The plug-in needs to be event enabled: you do this in the Options tab of the Plug-in editor:





If you work on Python with external files, the plug-in needs to reload the code, so you must turn off caching during development:





Here I break down the events employed. There are bucketloads of events, you'll find them in the SDK: MiniCadHookIntf.h and it does take a lot of time to be comfortable with their usage. Some of them I have really no idea what they do, and believe me, I did spend an inordinate about of time on them. The two major files you might want to study are these, but there is far more (the SDK version is here just irrelevant):





The code for the plug-in runs a number of times (and double as much if the developer mode is on), vs.GetCustomObjectInfo is compulsory for fetching the needed basic variables:

(ok, gPio_N, gPio_H, gPioRec_H, gWall_H) = vs.GetCustomObjectInfo()


Every time the code runs, you must fetch the type of event involved and eventually do something during that event. vs.vsoGetEventInfo is compulsory for fetching event info:

(theEvent, theButton) = vs.vsoGetEventInfo()
# whereby theButton is a generic message

(theEvent, aMessage) = vs.vsoGetEventInfo()


  • Like 2
Link to comment


Init properties:


During the init event you set up the Object Info Palette behaviour and other things:

if theEvent == kObjOnInitXProperties:


If you want to use buttons and/or Custom pull-down menus you need to turn on UIoverride: 

ok = vs.SetObjPropVS( kObjXPropHasUIOverride, True )

IF, and only IF,  you also want to use custom pull-down menus loading custom values, you will also need to enable custom widget visibilities. If you do this, you cannot use vs.SetParameterVisibility or vs.EnableParameter, which use parameter names, you can only use vs.vsoWidgetSetVisible and vs.vsoWidgetSetEnable. These need parameter indexes, so you must keep track of the parameter indexes and store them somehow as file. This does add complexity (I have a sub auto-filling a list as text into the right place):

# not used in the test example:
vs.SetObjPropVS(kObjXHasCustWidgetVisibilities, True); 
# only needed for custom pull-down menus with special lists of values
# after enabling this prop you need to set visibilities during event kObjOnWidgetPrep


To have the lovely widget groups, you must


  • enable them
ok = vs.SetObjPropCharVS( kWidgetGroupMode, vs.Chr(kWidgetGroupAutomatic), True ) # add widget groups
  • Add parameters as static text



  • turn the static text into separators. Mind that the routine vs.vsoInsertWidget takes a string and places it in the Object Info Palette. You can use the localised parameter name.  Since the function (boo, locParmName) = vs.GetLocalizedPluginParameter(pioRecName, univParmName) returns two vars, it cannot be used directly, so I created one simple sub named O_GetLocParmName that does that: return only a string.
ok = vs.vsoInsertWidget( cP___div0 -1, kWidgetSeparator, cP___div0, 'parameter name', empty);

# it is advisable to use the localized name
ok = vs.vsoInsertWidget( cP___div0 -1, kWidgetSeparator, cP___div0, O_GetLocParmName(gPio_N, '__div0'), empty);

# O_GetLocParmName is a subroutine that fetches only the string, dropping the boolean, 
# so I can use it directly in vs.vsoInsertWidget 
def O_GetLocParmName(pioRecName, univParmName):
	(boo, locParmName) = vs.GetLocalizedPluginParameter(pioRecName, univParmName)
	return locParmName


  • add indenting levels. These turn the widgets into expandable widgets. Don't omit to set the 0-levels, or things are going to mess up:
titles = [cP___div0, cP___div1, cP___div2]
for i in range(vs.NumFields(gPioRec_H) +1):
	if (i in titles): 
		vs.vsoWidgetSetIndLvl(i, 0)
		vs.vsoWidgetSetIndLvl(i, 1)


Warning: you will need to close and re-open your document to see widget groups display properly EVERY time you change something in the parameter index list (add, delete parameters)

Edited by _c_
  • Like 4
Link to comment
  • 1 month later...

I'm really thankfull to this post!


I'm doing a lot of try and error, but it works pretty fine! 🙂


On 3/29/2021 at 6:56 AM, _c_ said:

Warning: you will need to close and re-open your document to see widget groups display properly EVERY time you change something in the parameter index list (add, delete parameters)

I guess, a simple open and close the Editing-Window in the PlugIn Manager is working as well..

  • Like 2
Link to comment
  • 2 months later...

Nice share @_c_! Finally got my group widgets working after months of trial and error.


My widget indent calls were in the kObjOnWidgetPrep function not the kObjOnInitXProperties. (Funny that indentation works in kObjOnWidgetPrep without the Grouping)


Again, thank you and thank you.

Edited by twk
  • Like 1
Link to comment
  • 1 month later...

@Alexander Zemtsov,

4 hours ago, Alexander Zemtsov said:
def main():
	gDX = vs.Pwidth
	gDY = vs.Pdepth
	gDZ = vs.Pheigth


Pwidth, Pdepth, and Pheight are programmer defined parameters in the Plug-in object. The are displayed in the OIP at runtime (without the leading "P") and you use them to change the shape of the Plug-in object on the drawing. You can see all of the plug-in's "P" parameters by using the Plug-In Editor. In the code, they are effectively constants that are loaded before the code runs.






  • Like 2
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...