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

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

Related

Python bash script

I need help regarding bash scripting as I am new to it.
In the pycharm project I have a file .env.development
It has all the required valriables like-
A=aa ,B=bb ,Python_env=python
Now I need to read this Python_env from the .env.development file (output = python). And I need to use this 'python' in a different file.
The logic which I could think is to create a new .py file and read the .env.development (Python_env) file and output of that file I could use in my master script file.
Please guide me how to do that?
Example:
Consider 3 files-
1)Master.sh 2).env.development 3)Read_env.py
In Read.env file I need a bash script to read Python_Env from .env.development.
After reading the environment I need to use that file in master.sh.
Master.sh has code like --
(Python -m uvicorn)
So python needs to be replaced by some command which is reading the environment variable.

Python Code to Automate Abaqus Run Script

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.

How to execute Python file

I am learning Python and DJango and I am relatively nub with Linux. When I create DJango project I have manage.py file which I can execute like ./manage.py runserver. However when I create some Python program by hand it looks like that my Linux trying to execute it using Bash, not Python. So i need to write python foo.py instead ./foo.py. Attributes of both files manage.py and foo.py are the same (-rwx--x---). So my Q is: where is difference and how I can execute python program without specifying python? Links to any documentations are very appreciate. Thanks.
You missed one step, after give the corrects permissions to the file, open your foo.py then put this on the first line
#!/usr/bin/env python
Then you can use ./foo.py
I am just gonna add this for more clarity and to anyone coming across this post who might need an explanation.
Why do people write #!/usr/bin/env python on the first line of a Python script?
explains why you should it is used.
Save your python code file somewhere, using "Save" or "Save as" in your editor. Lets call it 'first.py' in some folder, like "pyscripts" that you make on your Desktop. Open a prompt (a Windows 'cmd' shell that is a text interface into the computer): start > run > "cmd".

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"

How can I load a .ply file in blender-2.68 and apply modifier to it through command line/script?

I want to apply a modifier to large number of meshes stored in different .ply files.
I wish to do this through command line so that the process can be automated. I know the basic of blender python API like how to write the modifier in python. But that required me to first import .ply file in blender using UI and then run my python script.
However, I want to automate the process of loading ply file, do the required operations and save back the result in ply format so that all the files can be processed one by one with minimum manual work.
I finally got the solution to the problem.
1. Invoke blender through command line
blender.exe --background --python yourFile.py
2. In your python file, you could use the modules provided by blender such as import_ply
(....Blender/2.68/scripts/addons/import_ply), etc. Just go through the module and you will be able to use the function written inside and manage to write code according to your need.

Categories

Resources