Jump to content

Python Newbie, Retrieving Parameters from OIP?


Recommended Posts

I've been scripting with vectorscript for a while but, I'm new to python.

in vectorscript I would retrieve a parameter from the OIP as follows:

LineLength:=PLINELENGTH;

How do I go about this in python?

If possible, could someone provide me with a simple example of creating a rectangular plugin object that allows me to change the Width & Height of the rectangle via the OIP?

Is python meant to phase out vectorscript completely in the future?

Thank you

Derek

Link to comment

Hi, this is done in Python by getattr(vs, 'PLineLength')

You could use DLibrary to make it easier to work with: DLibrary - Vectorworks. Inherit from the AbstractActivePlugInParameters class to access your parameters, and save them back like this:

# Your parameters contained in a class, so you can add extra checks and convertions.
class MyParameters(AbstractActivePlugInParameters):

   P_LENGTH = 'LineLength'

   @property
   def length(self) -> float:
       return self._get_parameter(self.P_LENGTH)

   @length.setter
   def length(self, value: float):
       self._set_parameter(self.P_LENGTH, value)


# Your run block, one of the places where you can use your parameters.
def run():
   # Create an instance of your parameters class for the current execution of the current active plugin.
   parameters = MyParameters()
   # Get the length parameter. (LineLength)
   length = parameters.length
   # some other stuff you need to do
   # the length parameter can even change due to a dialog setting etc...
   # Set the length parameter.
   parameters.length = length

One of the main reasons for the parameters class is that you can put all checks/conversions and other parameter value related stuff in there. Then the code that just works with the values don't have to bother.

If you want we can see to add examples to DLibrary to explain more how to work with it and do things in Python. I don't have lots of time, but I'm trying hard to get enough in it so others can build plugins faster, and I know examples are missing, but they are coming. All can contribute!

NOTE: Off course I could have chosen to let the base parameters class add the existing parameters as properties, but then the IDE will not pick them up. Also, now you can choose which parameters you want to get/set for the rest of your code. So you could have a read-only parameter for your code because you will never set it through code for example.

Edited by Dieter @ DWorks
Link to comment

I'm sorry, I'm confused by your explanation.

at the beginning you state that I use getattr(vs,'PLineLength') but, in your example you do not show that at all.

But, I have attempted using it as follows:

def Example():

    length=getattr(vs,'PLineLength')
    width=getattr(vs,'PBoxWidth')
    x = 0
    y = 0

    vs.Rect(x,length,y,width)

Example()

Good News! it draws the object and allows me to manipulate it via the OIP

Bad News... it's invisible. It has no line weight, fill, or color.

I need to find a way to apply attributes to the newly created object.

Link to comment
Setting the attributes from the attribute palette is not working. All i see are the six handles of the object. I really want to be able to set my line weight/style/color from inside the plugin.

It should work. All the things you draw in your plugin will be drawn in the class of the plugin by default, and will get the attributes of what is on the attributes palette when your plugin object is selected. If it doesn't work, then I think you have set the active class in your plugin, which you should not be doing, or the object isn't drawn for some reason. Is this a rectangle based plugin?

Can you post the whole code so we can have a look on what's going wrong?

Edited by Dieter @ DWorks
Link to comment

You can get parameter values a little more simply. Parameters are defined as constants of the vs class, so you can just say

length = vs.PLineLength

I don't think attributes is your issue -- you are seeing the extents of the PIO and not actually drawing the rectangle. Possibly the block where you define the object isn't getting executed.

-Josh

Link to comment

What I posted was the entire code:

def Example():

    length=getattr(vs,'PLineLength')
    width=getattr(vs,'PBoxWidth')
    x = 0
    y = 0

    vs.Rect(x,length,y,width)

Example()

Its just a simple rectangular plugin object that creates a rectangle.

I want to specify the line weight/pattern/color within the plugin but, the following does not seem to work:

def Example():

    length=getattr(vs,'PLineLength')
    width=getattr(vs,'PBoxWidth')
    x = 0
    y = 0

    vs.FillPat(1)
    vs.PenSize(4)
    vs.PenPat(2)
    vs.PenFore(255)
    vs.Rect(x,length,y,width)

Example()

I've also tried tried applying a handle to the new object but, that didn't seem to work either

def Example():

    length=getattr(vs,'PLineLength')
    width=getattr(vs,'PBoxWidth')
    x = 0
    y = 0

    vs.Rect(x,length,y,width)

    #LNewObj found in Document List Handling
    h=vs.LNewObj()

    #SetLW found in Object Attributes
    vs.SetLW(h, 4)
    vs.SetFPat(h,1)
    vs.SetPenFore(h,255)

Example()

which didn't seem to work either.

Edited by MaxStudio
Link to comment

length = vs.PLineLength

My code is as follows:

def Example():

    length=vs.PLineLength
    width=vs.PBoxWidth
    x = 0
    y = 0

    vs.Rect(x,length,y,width)

Example()

You are saying it's creating the plugin object container using the length and width parameters but, it's not actually drawing the rectangle?

Link to comment
You are saying it's creating the plugin object container using the length and width parameters but, it's not actually drawing the rectangle?

That would be my guess. If you don't do any special coding, the object will take on the attributes set in the Attributes palette.

The easiest way to check your geometry is to duplicate the object and select Convert to Group.

You can also add vs.Message() in your function to make sure it is running and check the value of your variables.

Python will not replace Vectorscript, and they will both have the same funciton calls. VS functions that have better alternatives in Python, like file reading and writing or date handling, will likely not get any improvements in vs however, so python will ultimately be a more capable solution.

-Josh

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.

Guest
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...