Jump to content
Sign in to follow this  
MaxStudio

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

Share this post


Link to post

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

Share this post


Link to post

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.

Share this post


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

Well, the example was an example when using DLibrary, which is kind of a wrapper for vs, to make it easier to script plugins.

Share this post


Link to post
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.

If the rectangle is drawn within your pio, then you just apply attributes through the attributes palette when the object is selected.

Share this post


Link to post

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.

Edited by MaxStudio

Share this post


Link to post
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

Share this post


Link to post

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

Share this post


Link to post

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

Share this post


Link to post

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?

Share this post


Link to post
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

Share this post


Link to post
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

-Josh

True, but I hate it when my IDE tells me otherwise... (One of the reasons of my Parameters class, to not have to think about this.)

Share this post


Link to post

Just noticed, you don't have the Rect function quite right. It is Rect(x1, y1, x2, y2). You are defining a rectangle of zero width. Also, the rectangular PIO has it's origin in the center of the left side. Try instead:

vs.Rect(x,y-width/2,length,y+width/2)

Share this post


Link to post

JOSH!

That was the issue. I was so wrapped up in whether or not I was using python correctly I didn't realize I was making a simple mistake in the function.

The rectangle has been drawn.

Thank You.

Thanks also to Dieter for your help

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

 

7150 Riverwood Drive, Columbia, Maryland 21046, USA   |   Contact Us:   410-290-5114

 

© 2018 Vectorworks, Inc. All Rights Reserved. Vectorworks, Inc. is part of the Nemetschek Group.

×