Is it possible to create Blender file (.blend) programmatically with Python? - 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"

Related

How to create a .bat file that starts Maya and calls a python script at startup using evalDeferred?

I'm attempting to run a python script that creates a custom menu inside Maya when a .bat file is opened to start Maya. The python script is not inside the Maya project and I wanted to add the path in the batch file. I have this in the .bat file:
start D:\TOOLS\Maya2019\bin\maya.exe -command evalDeferred(python('execfile(\"D:\CustomMenu_startup.py\")'))
Several attempts already but it's returning a syntax error.
solved by using:
start D:\TOOLS\Maya2019\bin\maya.exe -command evalDeferred(python(\"execfile('D:\\CustomMenu_startup.py')\"))
I'd suggest that, as a rule, it'd be better to push the deferred evaluation into the python itself. That way you don't have to think about it in all 3 languages (BAT, MEL, and Python).
There may also be parts of the work you can execute before the main Maya event loop kicks in, which will save some startup time -- evaldeferred is the safe choice before touching the Maya UI or the scene but you might have other jobs (like downloading files or checking the user's disk) that can be done safely while Maya itself is still loading. That's another reason to do the deferred part in Python instead of in the outermost MEL call.
If you're interested in generating launchers like this you can simply distribute a mel file instead of the BAT; MEL is executable by Maya as a file argument so your commandline gets simpler and if you have the correct file associations set up it's double-clickable.
You might also want to check out these blog posts about how to create python launchers for Maya:
https://theodox.github.io/2018/pythonception#pythonception
https://theodox.github.io/2018/keystone#keystone

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.

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 do you write commands in blender from a python script run outside blender?

I am trying to find a wat to add virices in a subprocess.Popen opened blender using the script that opened it to write the bpy codes into the blender proces. If this isn't possible, is there a wat to make the blender python receive (through a script run in blender) messages from the python script run in terminal (I am using fedora)
Thanks in advance
Indeed - you have thought half the answer for yourself -
The blender modules won't be available from outside blender, but
you can have a script inside blender to receive data from outside.
The easiest way would be to use XMLRPC - use a script that
loads with Blender, and stars a Python XMLRPC server -
then you will be able to send commands into that script from
outside.
It is easier than it sounds - check the Python documentation
for XMLRPC (it will even allow you to have python 2.7 scripts
outside blender communicating with the Python 3 that
runs inside blender) -
http://docs.python.org/3.3/library/xmlrpc.server.html#module-xmlrpc.server

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