Run a extern batch file Python

Anonymous

Run a extern batch file Python

Anonymous
Not applicable

Hi there,

i have a external bat file in a external folder for example c:\test\test.bat 

2. now i have a custom Button in Fusion now my question how can i start the bat file in the Folfer???

 

thanks

0 Likes
Reply
Accepted solutions (1)
2,469 Views
8 Replies
Replies (8)

jesse
Enthusiast
Enthusiast
Accepted solution

You can use the Python os module, particularly the startfile function (see https://docs.python.org/3.3/library/os.html#os.startfile).  Note that this is Windows only; if you also wanted to support scripts on Mac, it looks like you can use the system function (docs available on the same page as startfile).  Or, you can use the subprocess module (I've used this and know it works for opening files in their default program).

 

 

2 Likes

brad.bylls
Collaborator
Collaborator

I am trying to do the same thing.

What is the subprocess module you you mentioned here?

I tried this which was suggested by another contributor:

    subprocess.run([sys.executable, 'SHCS'])

but nothing happened.

Thanks for any help.

Brad Bylls
0 Likes

Anonymous
Not applicable
Hi,

i don't longer use Fusion 360 I was changed to Alibre because of the
conditions of terms of Autodesk changes to much and the Data from my clients
is better local storage nothing in the cloud.

I hope you find a solution.


0 Likes

jesse
Enthusiast
Enthusiast

Hey Brad,

 

Here are the docs for the subprocess module: https://docs.python.org/3/library/subprocess.html

 

I think you're on the right track with that, but it probably needs to be tweaked to meet your specific situation.

 

The first argument to run is a list (e.g. [...]) which is the command line you want to run.  So for example, if, in a Windows terminal, you would type "SHCS" and hit enter and it would run, you might be able to use:

 

subprocess.run(['SHCS'])

 

If this doesn't work, you may need to add the full directory path, e.g.

 

subprocess.run(['C:\path\to\file\SHCS'])

(where the dummy path here is replaced with the actual directory path to the file)

 

If it is a batch file called SHCS, and that still isnt working, try:

 

subprocess.run(['cmd', '/c', 'C:\path\to\file\SHCS'])

(same as above re the dummy path)

 

Hope this helps!

0 Likes

brad.bylls
Collaborator
Collaborator

Jesse,

I started with this:  supprocess.run(['SHCS.py'])

Error messages in debug terminal
At line:1 char:17
+ supprocess.run(['SHCS.py'])
+ ~
Missing type name after '['.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingTypename

================================================================

Then I tried:  filepath = os.path.dirname(os.path.realpath(__file__))

supprocess.run(['cmd', filepath + '\\SHCS.py'])

Error in debug terminal:
At line:1 char:17
+ supprocess.run(['cmd', filepath + '\\SHCS.py'])
+ ~
Missing type name after '['.
At line:1 char:22
+ supprocess.run(['cmd', filepath + '\\SHCS.py'])
+ ~
Missing argument in parameter list.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingTypename

=====================================================================

filepath = os.path.dirname(os.path.realpath(__file__))

When I use:  supprocess.run([filepath + '\\SHCS.py'])

Error in debug terminal:
At line:1 char:25
+ supprocess.run([filepath + '\\SHCS.py'])
+ ~
Missing ] at end of attribute or type literal.
At line:1 char:39
+ supprocess.run([filepath + '\\SHCS.py'])
+ ~
Unexpected token ']' in expression or statement.
At line:1 char:39
+ supprocess.run([filepath + '\\SHCS.py'])
+ ~
Missing closing ')' in expression.
At line:1 char:40
+ supprocess.run([filepath + '\\SHCS.py'])
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : EndSquareBracketExpectedAtEndOfAttribute

===================================================================

And when I run:  supprocess.run(['cmd', '/c', filepath + '\\SHCS.py'])

Error messages in debug terminal:
At line:1 char:17
+ supprocess.run(['cmd', '/c', filepath + '\\SHCS.py'])
+ ~
Missing type name after '['.
At line:1 char:22
+ supprocess.run(['cmd', '/c', filepath + '\\SHCS.py'])
+ ~
Missing argument in parameter list.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingTypename

==============================================================

Brad Bylls
0 Likes

jesse
Enthusiast
Enthusiast

Hey Brad,

 

It looks like you maybe be running these commands from a PowerShell command line, which is where the errors about missing type name and so forth are coming from.  The subprocess module must be used inside of a Python process, either as part of a Fusion script or add-in, or by running Python from your PowerShell command line first.

 

What I might do if I were you is this: go to Fusion, and click on File (at the top left, to the right of the A360 data panel), then hover over View, and look for Show Text Commands.  If it's there, click it to show a text command window at the bottom of Fusion; if it says Hide Text Commands, then the window is already opened somewhere.  On that window, there's a radio selector in the bottom right: Text, Py, and Js.  When Py is selected, you can type Python commands in there to be executed using Fusion's Python interpreter.  From there, you can import subprocess and then try subprocess.run(...) with one of the argument sets I mentioned above.  Since you are using a python script SHCS.py, sys.executable might be what you want instead of cmd.exe after all; you can test that in here.

 

The nice thing about this is once you have your python commands figured out here, they can go right into a Fusion 360 script (Tools -> Addins -> Scripts & Addins, then press Create when Scripts is selected) that can be run repeatedly from within Fusion.

 

Hope that helps!

 

0 Likes

brad.bylls
Collaborator
Collaborator

sys.executable might be it, but the sysntax is just to complicated for this newbie.

Thanks though.

Brad Bylls
0 Likes

brad.bylls
Collaborator
Collaborator

Hey Jesse,

Sorry, none of your suggestions worked.

After some more research on line and in the forums, I found out what did work.

I set the external file up as a function and then in the main program and call the function.

During debugging I see that it starts running the external script and I can follow it along.

The problem I'm having now is that the external file doesn't work at one point, even though it did when it was just a plain script file.

I have to keep plugging along.

Thanks for your help.

Brad Bylls
0 Likes