Jump to content
Developer Wiki and Function Reference Links ×

GetVSVar not working properly? MD5 in Vectorscript


AFDesign

Recommended Posts

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!

Link to comment

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 by AFDesign
forgot old comments
Link to comment

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);

 

Link to comment

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);

 

Link to comment

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.

Link to comment

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.

Link to comment

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.

Link to comment

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

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