AFDesign Posted October 22, 2020 Share Posted October 22, 2020 Hi, this is the first time I try to use Python code inside Vectorscript, and I can't understand why by "importing" a variable in Python (using GetVSVar) the first character is automatically deleted. I'm trying to retrieve an MD5 hash from a string: procedure MD5test; var mystring,myMD5string:STRING; begin {importing the correct string} mystring:='test'; PythonBeginContext; PythonExecute('import vs'); PythonExecute('import hashlib'); PythonExecute('thestring=vs.GetVSVar(''mystring'')'); PythonExecute('vs.AlrtDialog("Imported in python <"+thestring+">")'); {string lose the first character!!} PythonExecute('myhash=hashlib.md5(thestring.encode())'); PythonExecute('vs.SetVSVar("myMD5string",myhash.hexdigest())'); PythonEndContext; alertinform(concat('input string: <',mystring,'> wrong result (result for "est"): ',myMD5string),'',false); {importing the string with a starting space} mystring:=' test'; PythonBeginContext; PythonExecute('import vs'); PythonExecute('import hashlib'); PythonExecute('thestring=vs.GetVSVar(''mystring'')'); PythonExecute('vs.AlrtDialog("Imported in python <"+thestring+">")'); PythonExecute('myhash=hashlib.md5(thestring.encode())'); PythonExecute('vs.SetVSVar("myMD5string",myhash.hexdigest())'); PythonEndContext; alertinform(concat('input string with starting space: <',mystring,'> correct result: ',myMD5string),'',false); {using the string directly} PythonBeginContext; PythonExecute('import vs'); PythonExecute('import hashlib'); PythonExecute('myhash=hashlib.md5(b"test")'); PythonExecute('vs.SetVSVar("myMD5string",myhash.hexdigest())'); PythonEndContext; alertinform(concat('correct result using "test" string directly in python: ',myMD5string),'',false); end; run(MD5test); I tried with Vectorworks 2020 and 2021, same problem. Maybe I'm doing something wrong? Thanks! Quote Link to comment
PatW Posted October 22, 2020 Share Posted October 22, 2020 I see nothing wrong, also the example has this issue. https://developer.vectorworks.net/index.php/VS:PythonExecute As a workaround i would add the whitespace and always check if the first letter is a whitespace (in case the problem gets fixed). If the first letter is a whitespace you have to remove it again. regards, Patrick Quote Link to comment
AFDesign Posted October 22, 2020 Author Share Posted October 22, 2020 Thank you Patrick, my fear was only me seeing this error. I will use the workaround. kind regards Andrea Quote Link to comment
Pat Stanford Posted October 22, 2020 Share Posted October 22, 2020 A better work around is to set the VS Variable to be a DynArray of Char rather than a string. Then you get the entire VS string into Python. Bug Report filed on the string version. Quote Link to comment
AFDesign Posted October 23, 2020 Author Share Posted October 23, 2020 Thank you, Pat! Quote Link to comment
AFDesign Posted October 23, 2020 Author Share Posted October 23, 2020 (edited) Using "mystring:DYNARRAY[] OF CHAR;" the hash is generated correctly, but now the result start with a white space. There is the same kind of bug to report.. Thank you procedure MD5test; var myMD5string:STRING; mystring:DYNARRAY[] OF CHAR; begin mystring:='test'; PythonBeginContext; PythonExecute('import vs'); PythonExecute('import hashlib'); PythonExecute('thestring=vs.GetVSVar(''mystring'')'); PythonExecute('vs.AlrtDialog("Imported in python <"+thestring+">")'); PythonExecute('myhash=hashlib.md5(thestring.encode())'); PythonExecute('vs.SetVSVar("myMD5string",myhash.hexdigest())'); PythonEndContext; alertinform(concat('<',myMD5string,'>'),'',false); {result start with a white space} end; run(MD5test); Edited October 23, 2020 by AFDesign forgot old comments Quote Link to comment
Pat Stanford Posted October 23, 2020 Share Posted October 23, 2020 I am not seeing an extra space here in VS2021SP2. Try this one. I have put asterisks on either side of the VS String to specifically look for white space. PROCEDURE Example; VAR strName : DynArray of Char; BEGIN strName := 'vs string'; PythonBeginContext; PythonExecute( 'import vs' ); PythonExecute( 'ss = ''hello from python: *''' ); PythonExecute( 'vs.AlrtDialog(ss + vs.GetVSVar("strName") + ''*'')' ); PythonExecute( 'vs.SetVSVar("strName", "python string")' ); PythonEndContext; AlrtDialog( Concat( 'VectorScript received: *', strName, '*' ) ); END; RUN(Example); Quote Link to comment
AFDesign Posted October 26, 2020 Author Share Posted October 26, 2020 Thank you Pat, I understood the problem: I passed the result to "var myMD5string:STRING;" variable, using STRING a white space is added. If I change to "var myMD5string:DYNARRAY OF CHAR" it works correctly. Thank you for your patience Quote Link to comment
Pat Stanford Posted October 26, 2020 Share Posted October 26, 2020 Are you saying there is a bug in SetVSVar as well as the one I already submitted on the GetVSVar? Quote Link to comment
AFDesign Posted October 27, 2020 Author Share Posted October 27, 2020 Yes, if you use SetVSVar with a STRING variable, the result has a white space at the beginning. Quote Link to comment
Pat Stanford Posted October 27, 2020 Share Posted October 27, 2020 I am not seeing a problem with SetVSVar here. What do you get when you run the following script? PROCEDURE Example; VAR strName : String; BEGIN strName := ''; PythonBeginContext; PythonExecute( 'import vs' ); PythonExecute( 'vs.SetVSVar("strName", "python")' ); PythonEndContext; AlrtDialog( Concat( 'VectorScript received: *', strName, '*' ) ); END; RUN(Example); Quote Link to comment
AFDesign Posted October 27, 2020 Author Share Posted October 27, 2020 It's true, if you simply "replace" a string there is no problem. But if you process the variable as in my example, the result has a white space, that disappear if you use "Dynarray of Char" instead of "string". Your example with hash process and String variable (white space in the result): PROCEDURE Example; VAR strName : String; BEGIN strName := ''; PythonBeginContext; PythonExecute('import vs'); PythonExecute('import hashlib'); PythonExecute('thestring=vs.GetVSVar(''strName'')'); PythonExecute('myhash=hashlib.md5(thestring.encode())'); PythonExecute('vs.SetVSVar("strName", myhash.hexdigest())'); PythonEndContext; AlrtDialog( Concat( '*', strName, '*' ) ); END; RUN(Example); Same example using Dynarray variable (correct result): PROCEDURE Example; VAR strName : Dynarray of Char; BEGIN strName := ''; PythonBeginContext; PythonExecute('import vs'); PythonExecute('import hashlib'); PythonExecute('thestring=vs.GetVSVar(''strName'')'); PythonExecute('myhash=hashlib.md5(thestring.encode())'); PythonExecute('vs.SetVSVar("strName", myhash.hexdigest())'); PythonEndContext; AlrtDialog( Concat( '*', strName, '*' ) ); END; RUN(Example); Maybe is something linked to string encoding in python. Quote Link to comment
Pat Stanford Posted October 27, 2020 Share Posted October 27, 2020 I think the extra space is being added on the GetVSVar and then being propagated through the rest of the functions. Can you come up with a version that does not use GetVSVar that shows SetVSVar including an extra space? Quote Link to comment
AFDesign Posted October 28, 2020 Author Share Posted October 28, 2020 Sure! Same code, but string is directly assigned in python: PROCEDURE Example; VAR strName : String; BEGIN PythonBeginContext; PythonExecute('import vs'); PythonExecute('import hashlib'); PythonExecute('thestring="test"'); PythonExecute('myhash=hashlib.md5(thestring.encode())'); PythonExecute('vs.SetVSVar("strName", myhash.hexdigest())'); PythonEndContext; AlrtDialog( Concat( '*', strName, '*' ) ); END; RUN(Example); SetVSVar return the white space, after encoding the hash. I think I will not use this phyton code because it works only if the plugin is not encrypted. After encryption, the plugin freeze Vectorworks. If I remove the code and encrypt the plugin, it works. Maybe plugin encryption cause some problems to python code, I will try to understand why. Quote Link to comment
AFDesign Posted October 28, 2020 Author Share Posted October 28, 2020 I discovered why my plugin freeze: after encrypting the plugin and rebooting vectorworks, SetVSVar returns an empty variable. You can see the problem by creating a new plugin with this simple code: procedure testsetvsvar; VAR result:STRING; begin PythonBeginContext; PythonExecute('import vs'); PythonExecute('vs.SetVSVar("result","PYTHON TEXT")'); PythonEndContext; alertinform(concat('text from python : ',result),'',false); end; run(testsetvsvar); Without encryption, the plugin works correctly. If you encrypt the plugin and reboot Vectorworks, text from python result is empty. Quote Link to comment
Pat Stanford Posted October 28, 2020 Share Posted October 28, 2020 I think it is Hashlib that is adding the space, not the SetVSVar. Try printing myhash from Python and see what you get. Quote Link to comment
AFDesign Posted October 28, 2020 Author Share Posted October 28, 2020 Yes, as I said previously, if you simply "replace" a string there is no problem, SetVSVar works correctly, and the white space isn't a problem if I use Dynarray of Char. The problem is that after encrypting the plugin, SetVSVar returns an empty value, as you can test in my last example (without using any other python code). 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.