Python Code to Automate Abaqus Run Script - python

Error with Subprocess addition
This is the error I get regarding the abaqus module
I'm quite new to python and so please don't mind if this question might seem silly.
So I have python file that does the function of opening an abaqus viewer and I have another python file that describes the functions I want to do in the abaqus viewer.
I need a piece of code that can automate the second script without me having to manually go into file>run script.
Script to Open Abaqus:
import os
import subprocess
os.startfile('Q:/win_apps/scripts/simulia/Abaqus/6.14-3/Use_these_if_not_working/abq6143_viewer.bat')
And then I have a python script that has the code regarding my output requests from abaqus viewer.
What line can I add to the above file to automatically take the second python script and run it?

When running Abaqus with the typical start up scripts you can pass Abaqus/Viewer a script to run from the command line:
abq6143 viewer noGUI=script.py
where you replace script.py with the name of your Python script. This will start up Abaqus/Viewer with no user interface, run the script, and then quit.
If you want the user interface to come up and automatically run your script you can use the script= command instead of noGUI:
abq6143 viewer script=script.py
I see that you're using a custom batch file to start up Abaqus/Viewer. Without seeing those contents I couldn't say exactly how you would integrate the above, but you will probably need to adjust the relevant line in the batch file with the noGUI or script command.

Related

Using Python script to run a full independent powershell script

I am a noob, self-motivated programmer, and had been researching methods to use my Python script to run a Powershell file that will copy and image and place the image into Excel.
I've used the subprocess, call, and Popen commands in an attempt to call and run the Powershell program from the Python script, but none has worked.
Some of the examples I found only called different functions of a Powershell script, but when I tried those settings it didn't work for my program. All of the setup for my Powershell has been established so that it can run with my PC, and also runs well when launched independently from Python.
What I would like to ask is if I had, for example, a My_program.py file and a Moving_image.ps1 file. I want to use my .py file to run/execute my .ps1 file, while both programs are located in the same path (C:\Users\Scripts).
What line of code(s), imports, and other program setup's would I need in my Python file to simply run the independent .ps1 file from my Python script?
I don't need the Powershell script to return anything to the Python script. I would like for it to simply run the copy and paste the command I sent it.
Thank you. Any type of guidance that will lead to this program actually functioning properly will be most appreciated!
Here's what worked for me (testing on linux):
python script test.py
from subprocess import call
call(["/opt/microsoft/powershell/6.0.4/pwsh", "./test.ps1"])
powershell script test.ps1
Import-Module '/home/veefu/pwshmodules/testMod'
Write-Output "Hello from test.ps1"
Get-HelloWorld
testMod.psm1 module, stored at /home/veefu/pwshmodules/testMod
function Get-HelloWorld {
Write-Output "Hello World (from Module)"
}
result when running the python script:
Hello from test.ps1
Hello World (from Module)
On windows you'll probably have to provide the complete path, C:\Windows\system32\MicrosoftPowerShell\1.0\powershell.exe and you may have to pass /file .\yourOtherScript.ps1 in the second argument to call

How to control FEA software like MSC NASTRAN using Python code?

I would like to run MSC NASTRAN using python. I have seen a similiar function in MATLAB using
system('nastran.exe file_name.bdf') #where file_name.bdf is the input file to Run using nastran.
Hence i tried below using python code, but it did not work,
import os
os.system('nastran.exe file_name.bdf')
Could you tell me where i going wrong?
Also, how to give the command line in NASTRAN thru python? Like for example memory allocation for the run, number of cores need to be used for run etc.
some NASTRAN command lines include,
1. scr=yes delete=f04,log,xdb pause=yes
2. mem=10gb bpool=3gb memorymaximum=14gb sscr=500gb sdball=500gb mode=i8
...etc.
I can't speak directly for MSC Nastran, its been a while since I've used it. But most modern FEA programs have an API (application program interface) to allow you to call commands from a external program like python or matlab.
Without an API, you may be limited to using python to start the program from the command line, which is what your code is trying to do. As for how to launch a program from within python, check out this question/answer:
How to run application with parameters in Python?
Easy way to run a MSC NASTRAN file is to create a .bat file and run it from python.
The format for .bat file is:
<nastran.exe location> <Python script file location> <Nastran command line>
An example can be :
C:\MSC.Software\MSC_Nastran\20141\bin\nastran.exe C:\py_nastran_run\example.bdf scr=yes old=no delete=f04,log,xdb
Then include the below line in python script,
status=subprocess.call("runBatch.bat")

Blender - Open and parse a .blend file from python script

I would like to open a .blend file from a python script and parse it (get objects, animations and materials).
The documentation I have read so far about how to do this from blender API (running the script as a blender add-on), but I would like to run this script from the command line without opening blender.
I appreciate all the help you can give me.
I realized I don't need to open the binary blender file and parse it in order to use the objects.
Blender has its own python installation, so I put a python script inside the folder path_to_blender/version/scripts/addons , I can execute it in the command line as follows:
blender.exe --background --python ./version/scripts/addons/superScript.py
Next, if you have a .blend file you want to read from your script, put it after the background parameter as follows:
blender.exe --background myFile.blend --python ./version/scripts/addons/superScript.py
And inside your python script do the following:
import bpy
import os
for ob in bpy.context.scene.objects:
print("object name: ", ob.data.name)
In this example I'm printing all the objects inside the scene in the .blend file
As you answered, the easiest way is to use blender, there is also a script that is included with a blender install called blend_render_info.py that extracts the start and end frame settings without using the blender binary, I'm not sure how easy it would be to expand on that but it does work with python 2.x and 3.x
To get more detailed info there is an old project called blender-aid that was created to read and alter blend file data, someone recently had luck using it to extract blend file data using python 2.7 as documented in this question

How to attach to PyCharm debugger when executing python script from bash?

I know how to set-up run configurations to pass parameters to a specific python script. There are several entry points, I don't want a run configuration for each one do I? What I want to do instead is launch a python script from a command line shell script and be able to attach the PyCharm debugger to the python script that is executed and have it stop at break points. I've tried to use a pre-launch condition of a utility python script that will sleep for 10 seconds so I can attempt to "attach to process" of the python script. That didn't work. I tried to import pdb and settrace to see if that would stop it for attaching to the process, but that looks to be command line debugging specific only. Any clues would be appreciated.
Thanks!
You can attach the debugger to a python process launched from terminal:
Use Menu Tools --> Attach to process then select python process to debug.
If you want to debug a file installed in site-packages you may need to open the file from its original location.
You can to pause the program manually from debugger and inspect the suspended Thread to find your source file.

Is it possible to create Blender file (.blend) programmatically with Python?

I know Python is the standard scripting language for use inside Blender, but I didn't find a way to create a .blend file with python.
What I want to do is not to use python inside blender, but rather "use blender (libs?) inside python".
My planned workflow would be the following:
Define some parameters for my model;
Define a "generative recipe" to create appropriate Blender objects that will be saved to file;
Create a python script to store the parameters and procedures. When the script runs, some .blend file is created in the same folder;
Use Blender to visualize the model. If model needs to be changed, make changes to the script, run it again, and open it again.
There is an experimental option that allows you to build your own copy of blender as a python module to use like a standard python module. This page is about all the help you will get for it.
I would recommend you write your python script as a blender script to be run inside blender. You can automate saving the file using bpy.ops.wm.save_as_mainfile(filepath="path/to/myfilename")
You can run a script inside blender that generates a blender text block that you can then run as a python script, select all, delete and re-run. You can also easily reload an external file as a text block in blender if you prefer an external text editor for it's features.
You can also use your plan to generate the script yourself which you can then run using blender with blender -b -P myscript.py the -b option tells blender to run in the background without a GUI.
You can start a new Blender process from any application (a C++, Python app or even command line) and tell the new process to run a script file (written in Python). This script will generate your geometry and then can save the new scene to a blend file.
To start a new Blender process and force it to execute a script use:
blender.exe --background --python "c:\path to\script.py"

Categories

Resources