twk Posted October 17, 2015 Share Posted October 17, 2015 Hello All, VS has the {$INCLUDE ..} syntax for externally referencing code. How do you do the same in python? Or must you used modules? Thanks in advance Tui Quote Link to comment
JBenghiat Posted October 17, 2015 Share Posted October 17, 2015 Import would be the equivalent. That does mean that any included elements work best as classes or functions. Unlike $INCLUDE, importing into Python is more intelligent, rather than just inserting the code block for the include directive. Import should look in the same root as any running plug-ins. Additionally, you can specify a path via Plug-Ins>Script Options. -Josh Quote Link to comment
twk Posted October 18, 2015 Author Share Posted October 18, 2015 Ah, I see .That specifying path tip to is better for me as most of in-house VS plugins have includes on a server. Thanks Josh. Tui Quote Link to comment
MaxStudio Posted December 10, 2015 Share Posted December 10, 2015 I have a question about importing into python. With vectorscript we used the $include method when we wanted to import an external file {$INCLUDE Macintosh HD:include:myfile.px} adding the '.px' allowed me to lock the file in vectorworks and it would no longer need the external reference file when I ran the plugin. Is this possible in python? or will i always need to have the multiple files in a directory to import? I could keep all the files I need to import on a server but, I would rather just get the plugin working, compile it, lock it and only have one file to deal with in the end. Does that make sense? and is it possible? Thanks Derek Quote Link to comment
MaxStudio Posted December 10, 2015 Share Posted December 10, 2015 I'm playing around with importing into python. All my work is in a folder on my hard drive at the following location: /Macintosh HD/projects/project01 In the project01 folder are 3 files: example.py test.py _init_.py I'm attempting to import information from text.py into example.py test.py contains the following: class myclass: def runme(): print "hello world" example.py contains the following: import sys sys.path.insert(0, '/Macintosh HD/projects/project01') import test.myclass When I run my code it tells me ImportError: No module named myclass Any idea why this is happening? Am I going about this the wrong way? Quote Link to comment
MaxStudio Posted December 10, 2015 Share Posted December 10, 2015 ok seems i was able to get it to work i made a small change in example.py as follows: import sys sys.path.append('/Macintosh HD/projects/project01') from test import myclass I thought import test.myclass was another way of saying from test import myclass but I guess it had a problem with it. I notice when I import myclass it runs the runme def. I assumed that it would import the def but not run it immediately. I guess I was wrong. Quote Link to comment
JBenghiat Posted December 10, 2015 Share Posted December 10, 2015 I notice when I import myclass it runs the runme def. I assumed that it would import the def but not run it immediately. I guess I was wrong. Assuming your paste was accurate, you have an indent issue. Try indenting the Print line. Quote Link to comment
MaxStudio Posted December 11, 2015 Share Posted December 11, 2015 Ok here's my latest issue: I added the Project folder /Macintosh HD/projects/project02 to the script options>environment paths In the project02 folder are 3 files: example.py test.py __init__.py example.py code: import vs from test import myclass def plugin(): obj=myclass() obj.runme() vs.Message("I've made it this far") plugin() test.py code: class myclass: def runme(self): vs.Message("hello world") I would expect the output to be: hello world I've made it this far but I only get: I've made it this far any ideas why it's not running the runme() from myclass()? Quote Link to comment
DomC Posted December 11, 2015 Share Posted December 11, 2015 Hm .. python should accept mix of single AND double quotes to use quote inside a string. So your example should work. Also maybe checked out by escape quote by \ ? vs.Message('I\'ve made it this far') It seems to work inside the Vectorscript Editor. Maybe if you have this in an external text file, Text encoding, text reading routines etc. do their part to destort the string. TextEdit saves (as standard) with UTF-8. Vectorworks imports with ...? Just an unqualified idea Quote Link to comment
MaxStudio Posted December 11, 2015 Share Posted December 11, 2015 Python does work with single and double quotes. I've actually gotten the import to work. I have updated the example.py as follows: import vs from test import myclass def plugin(): obj = myclass return obj.runme() vs.Message("I've made it this far") plugin() test.py is as follows: import vs class myclass: def runme(): vs.Message("hello world") so at this point my code produces hello world but for some reason it does not finish the plugin function it seems to skip the vs.Message("I've made it this far") Any ideas why? maybe if I move the vs.Message after I call plugin()... Quote Link to comment
JBenghiat Posted December 11, 2015 Share Posted December 11, 2015 Message() clears itself between calls, so I think your second message us just replacing your first. AlrtDialog() will run for each point if you want to track progress. Quote Link to comment
MaxStudio Posted December 11, 2015 Share Posted December 11, 2015 ok changed all the vs.Message() to vs.AlrtDialog() At first I tried just replacing the vs.Message() with vs.AlrtDialog() and still only got one alert dialog saying hello world example.py import vs from test import myclass def plugin(): obj = myclass return obj.runme() plugin() def plugin2(): vs.AlrtDialog('Ive made it this far') plugin2() test.py import vs class myclass: def runme(): vs.AlrtDialog("hello world") It wasn't until I added the plugin2() that it gave me: hello world in one alert box and Ive made it this far in a second alert box but it didn't only run once... it ran 3 times! huh!? :eek: Quote Link to comment
Dieter @ DWorks Posted December 12, 2015 Share Posted December 12, 2015 Hi MaxStudio. I think you are fairly new to Python and programming in general? You are doing some strange things in your code and I think it's because of the lack of knowledge about OOP (Object Oriented Programming) and lack of the Python peps, which are rules on how to do your code. I think it's best to get that right first. So here are some tips I can give you based on your latest example: - A class in Python 3 should at least inherit from object, so that it's a real OO class. Plus a class name should be PascalCase: class MyClass(object): pass - A method in a class can be of one of three types of methods: A static method, a class method or an instance method. As you are making an instance of your class, and calling the method on that instance, I presume you want an instance method. This is created like the example below. Also, method names are all lower-case with underscore between the words. def run_me(self): vs.AlrtDialog('Hello world.') You see, instance methods always have a first argument of self. Python then know this method is an instance method. - Now I see you do obj = myclass, which is actually not creating an instance of your class, but saying that obj is your class. To create an instance of the class, you do: obj = MyClass() - In your previous examples, you had a return statement and after that a vs.Message call. This last call will never be reached, as return means to finish the current definition (method) and return whatever you put behind the return keyword. So that was why you never saw that message. - Now your initial question was about importing stuff, well, a good IDE will help you with that, so that when using stuff from other packages/modules, it will ask you to import it. Using a good IDE will help you in all these things, as they will mark mistakes are violations to the Python PEPs. This will help you code better, and also learn new stuff, as some IDEs will tell you possible solutions to violations, or even suggest better alternatives to code that is correct, but can be better. I hope I could help you with this, and if there are any further questions, just ask. Quote Link to comment
MaxStudio Posted December 14, 2015 Share Posted December 14, 2015 First let me thank you for your response. Way back in high school I took pascal and early in college took C++ beyond that I'm self taught writing scripts mostly for vectorworks and a few other programs. So recently I was told by a sales rep that python would be the future for vectorworks. This has led me to this point where I'm attempting to get a grasp on python. Using the internet I'm trying to teach myself enough to get by and trying to keep my examples small so I don't attempt to take on too much at one time or overwhelm other forum users with an exorbitant amount of info. As for IDE, I use TextWrangler to write most of the scripts. I can run python in that but, I can't run anything written specifically fro Vectorworks. Vectorworks has been a pain to test the python code due to the fact that I have to restart it everytime I change a file i am importing. Unfortunately, at this stage I'm dealing with a lot of trial & error. (thus my quote in my signature) If you have any other near free solutions for IDE I would appreciate it. I really appreciate all your help. Thank You! Quote Link to comment
Dieter @ DWorks Posted December 14, 2015 Share Posted December 14, 2015 Well, I absolutely love IntelliJ IDEA to work with, but it's not only Python oriented. I use this because I also do a lot of other programming work. You could use PyCharm (https://www.jetbrains.com/pycharm/), which is from the same vendor, and they have a community edition which should include all the things you need to write plugins for Vectorworks. There's also a remote debugger included, so you'll be able to debug your scripts in Vectorworks, which is really handy! Also, you could try my library DLibrary (https://bitbucket.org/dieterdworks/vw-dlibrary), which is meant to make plugin development way easier, though it's still from finished. If there are things not in it that you need, you can always request it, or you could add it yourself, I'll even help with that. It should take away the strange things from VS and enable you to work more OO. There aren't many example right now, but I'm working on it. 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.