Gonda Posted June 3, 2018 Share Posted June 3, 2018 Hey All, So, does anyone know at what point an event enabled plug in pulls in the default values from parameters? I've tried altering these values during the InitXProperties event, but they seem to get overwritten. I've also tried doing it during the Reset event; however, this would then get called on a cut and paste resulting in any user-changed values being overwritten. I figured the easy way around this was to just add a test to the Reset event to see if it was the first time that it was called; however, in doing this, I've discovered that it seems that any global variables that get assigned get wiped as soon as handling of the current event is finished...is this accurate? Or am I just missing something? Cheers, -gonda Quote Link to comment
Miguel Barrera Posted June 3, 2018 Share Posted June 3, 2018 I would think that they get assigned on the first event because they are available for setting up popup menus, parameter visibility, and any buttons clicked. If I need to change any value I will do it during the reset value with SetRField. Quote Link to comment
Gonda Posted June 3, 2018 Author Share Posted June 3, 2018 So, I just tested the follow: vs.SetRField(paramHandle,'Custom Object1','LS3D','789') #Where LS3D is the parameter name as defined in the plug-in defintion and it is a Y coordinate field. When I place this line as the final statement at the end of the kObjOnInitXProperties handler, nothing seems to happen. When I place this line in the Reset Event handler, it is executed; however, on subsequent calls, this overwrites any user assinged values... I've also insterted an AlrtDialog() that displays each event number before it is handled and as far as I can see, the kObjOnInitXProps is only called once (unless you go back into the plug-in definition)....And then a reset event is generated.... Thougts? -g Quote Link to comment
JBenghiat Posted June 3, 2018 Share Posted June 3, 2018 Maybe take a step back and explain what you are trying to accomplish. Do you want to change object defaults, override defaults on insertion, or something else? Default values store in a record format that has the same name as the plug-in. Use GetObject(pluginName) to get the handle and SetRField to set defaults. The plug-in reads default values before the first event. The init event usualy only only happens once per session to tell VW how to handle plug-in. In developer mode, the init event runs with every regen, allowing you to actively refine how the plug-in behaves. Quote Link to comment
Miguel Barrera Posted June 3, 2018 Share Posted June 3, 2018 My own experience is to change values in the reset event. the typical work flow is : assign parameter values (either default or user entered) to variables with the P value or GetRField. find which parameter has changed and update any other variables as needed. redraw the plugin with the new values. assign the variables to the corresponding parameters at the end of the reset procedure with SeRField. If I see that some value is not changing when it is supposed to I will turn on the debugger and trace the value in question to find out why it is not changing. I work exclusively in vscript because the debugger is readily available. I have tried to setup the debugger in python but with no success and is the main reason I have not jumped into python. If you can get the debugger to work for you, it will help you a lot in finding any bugs in your code. Quote Link to comment
Gonda Posted June 4, 2018 Author Share Posted June 4, 2018 @JBenghiat So, what I was envisioning is that when you click on the tool's icon, a dialog is presented to ask for some initial values (this is a whole other issue of getting it to appear every time). The plug-in then uses these answers to set some initial values for parameters for when the plug-in is first inserted. Following that, these values will only be updated by the user via the OIP and will then be used in turn to insert some text onto the drawing. The kicker, is that I need to get input from the user before I can set the defaults and from what you're saying, these values are generated before any event happens. It seems, from what I can tell, that the values that appear in the OIP for each widget get loaded from the defaults sometime after the completion of the kObjOnInitXProperties event but before any other event happens. As a sidebar, how do you put VW into Developer Mode? I've looked through all the various preferences (both application and document) on both Windows and Mac and can't seem to find where that setting is... @Miguel Barrera That was my thought; however, unless I'm missing something, it seems that a plug-in does not maintain state between events (i.e. global variables). Initially, I would set a global variable firstRun that is tested. If it was the first run, assign defaults, if it wasn't carry-on but from what I can tell this behavior can't be accomplished without some way to store state... -g Quote Link to comment
Miguel Barrera Posted June 4, 2018 Share Posted June 4, 2018 developer mode is in the Vectorworks Preferences->Session->Run scripts in developer mode Quote Link to comment
JBenghiat Posted June 4, 2018 Share Posted June 4, 2018 Do you want the dialog with options to appear once per document in order to set defaults or on the insertion of every plug-in? If the former, you want to invoke SetObjPropVS(kObjXPropPreference, TRUE) in the initXProp event, which gives you an event to handle for whenever the plug-in defaults dialog gets displayed, whether on first insertion or when pressing the settings button in the mode bar. You then can craft a custom dialog to set initial values. The downside for this approach is that you will need to handle every field that you want to have user accessible default data — I don't believe you have a way to also present the standard defaults dialog. If the latter, in the reset event, check if the object is regenerating for the first time, and if so present your dialog. You can do this either with states (state 0) or IsNewCustomObject(). Note that both of these methods will only be true if manually inserting the object. If you want to also gather information for duplicating or inserting via script, you can do this with states, but because you would be presenting a dialog, I advise against it. Your dialog should write values to the object's record using SetRField. A few things to note about parameter values. If you access a value with Pparam_name, this is really just a constant that vs initializes as soon as the script loads, so it is fast and correctly typed, but read only. GetRField() will always retrieve the stored value, so if you use SetRField() earlier in the script, you will retrieve the updated value. When dealing with plug-ins with initialized or manipulated data, I use the following procedure: - At the beginning of the reset event, initialize a class that stores all parameters into variables - In the course of the plug-in code only read and write the variables - At the end of the reset event, write all the variables with SetRField. Quote Link to comment
Gonda Posted June 5, 2018 Author Share Posted June 5, 2018 So, I tried doing this with states....i.e. a variable is defined and checked to see if it had been changed from an initial value but it doesn't seem that the plugin retains the state of set variables between events...or am I missing something? Quote Link to comment
JBenghiat Posted June 5, 2018 Share Posted June 5, 2018 I probably didn’t explain my suggestion of states very well. The only way states address your task as you described it is as a check to see if the plug-in is regenerating for the first time. Are you trying to set defaults for the first plug-in in the document or set values when each plug-in is inserted? Quote Link to comment
Gonda Posted June 7, 2018 Author Share Posted June 7, 2018 I'm trying to set the values each time an object is inserted -g Quote Link to comment
JBenghiat Posted June 11, 2018 Share Posted June 11, 2018 Here is a very simplified procedure for the parameter SideLength: boo, PIName, PIHan, PIRecHan, PIWallHan = vs.GetCustomObjectInfo() # Initialize variable with parameter sideLength = vs.PSideLength # If first regen, use dialog for value if IsNewCustomObject( PIName ): outVal = vs.DistDialog('Enter a distance value:','0') if not vs.DidCancel(): sideLength = outVal # Use variable to draw object vs.MoveTo(0,0) vs.LineTo(sideLength, 0) # Write value to parameter vs.SetRField( PIHan, PIName, 'SideLength', vs.Num2StrF( sideLength ) I often have other things that can affect the parameter value, such as error checks or adjusting based on another parameter change (detected with states), so I just write values at the end of the script, but you could have this line under the if section. Quote Link to comment
Recommended Posts
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.