Run blender from command line with custom startup .blend file - blender

I know I can change the default startup file with File/Defaults/Save Startup File. I don't want to change the default startup file, but rather call blender from the command line with a custom .blend startup file, something like:
blender --startup-file my_file.blend
I don't want to do blender my_file.blend because then I can accidentally save the file and overwrite my_file.blend, which is supposed to be a template and not an editable file. I want to get the prompt to select the filename if I attempt to save. How can I accomplish that from the command line?

https://docs.blender.org/api/current/bpy.ops.wm.html#bpy.ops.wm.read_homefile
blender --python-expr "import bpy; bpy.ops.wm.read_homefile(filepath=r'my_file.blend')"
Or as a script:
import bpy
import subprocess
path = r"D:\Desktop\test.blend"
script = "\n".join([
"import bpy",
f"bpy.ops.wm.read_homefile(filepath=r\"{path}\")"
])
subprocess.Popen([bpy.app.binary_path, "--python-expr", script])

run
blender "my_file.blend" --python "my_script.py"
and put this in my_script.py
import bpy
bpy.ops.wm.save_as_mainfile(filepath="my_new_file.blend", compress=False)
https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html#python-options
https://docs.blender.org/api/current/bpy.ops.wm.html#bpy.ops.wm.save_as_mainfile

Related

I can't use the module I created in my main python file

I created a module named module_1:
def learn(programming_lang):
print("I am learning " + programming_lang)
and I want to use it in my main Python file.
module_1.learn("Python")
How can I do that?
in main Python file add this line
import module_1

How to write match results from .cypher into textfile via cypher shell (Windows)?

I want to write match results based on cypher code inside a cypher file via cypher-shell into a text file (I am trying to do this on Windows). The cypher file contains: :beginmatch(n) return n;:commit
I tried to execute:
type x.cypher | cypher-shell.bat -u user -p secret > output.txt I get no error. But at the end there is just an empty text file "output.txt" inside the bin folder. Testing the cypher code directly in the cypher-shell (without piping) works. Can anyone help, please?
consider using the apoc library
that you can export to different formats, maybe it can help you.
Export to CSV
Export to JSON
Export to Cypher Script
Export to GraphML
Export to Gephi
https://neo4j.com/labs/apoc/xx/export/ --> xx your version Neo4j,example 4.0

Blender Command line importing files

I will run the script on the Blender command line. All I want to do is run the same script for several files. I have completed the steps to run a background file (.blend) and run a script in Blender, but since I have just loaded one file, I can not run the script on another file.
I looked up the Blender manual, but I could not find the command to import the file.
I proceeded to creating a .blend file and running the script.
blender -b background.blend -P pythonfile.py
In addition, if possible, I would appreciate it if you could tell me how to script the camera and track axes to track to contraint (Ctrl + T -> Track to constraint).
really thank you for reading my ask.
Blender can only have one blend file open at a time, any open scripts are cleared out when a new file is opened. What you want is a loop that starts blender for each blend file using the same script file.
On *nix systems you can use a simple shell script
#!/bin/sh
for BF in $(ls *.blend)
do
blender -b ${BF} -P pythonfile.py
done
A more cross platform solution is to use python -
from glob import glob
from subprocess import call
for blendFile in glob('*.blend'):
call([ 'blender',
'-b', blendFile,
'--python', 'pythonfile.py' ])
To add a Track-to constraint to Camera pointing it at Cube -
camera = bpy.data.objects['Camera']
c = camera.constraints.new('TRACK_TO')
c.target = bpy.data.objects['Cube']
c.track_axis = 'TRACK_NEGATIVE_Z'
c.up_axis = 'UP_Y'
This is taken from my answer here which also animates the camera going around the object.
bpy.context.view_layer.objects.active = CameraObject
bpy.ops.object.constraint_add(type='TRACK_TO')
CameraObject.constraints["Track To"].target = bpy.data.objects['ObjectToTrack']

Generate data file at install time

My python package depends on a static data file which is automatically generated from a smaller seed file using a function that is part of the package.
It makes sense to me to do this generation at the time of running setup.py install, is there a standard way in setup() to describe “run this function before installing this package's additional files” (the options in the docs are all static)? If not, where should I place the call to that function?
Best done in two steps using the cmdclass mechanism:
add a custom command to generate the data file
override build_py to call that before proceeding
from distutils.cmd import Command
from setuptools import setup
from setuptools.command.install import install
class GenerateDataFileCommand(Command):
description = 'generate data file'
user_options = []
def run(self):
pass # Do something here...
class InstallCommand(install):
def run(self):
self.run_command('generate_data_file')
return super().run()
setup(
cmdclass={
'generate_data_file': GenerateDataFileCommand,
'install': InstallCommand,
},
# ...
)
This way you can call python setup.py generate_data_file to generate the data file as a stand-alone step, but the usual setup procedure (python setup.py install) will also ensure it's called.
(However, I'd recommend including the built file in the distribution archive, so end users don't have to build it themselves – that is, override build_py (class setuptools.command.build_py.build_py) instead of install.)

Preventing overwriting when using numpy.savetxt

Is there built error handling for prevent overwriting a file when using numpy.savetxt?
If 'my_file' already exists, and I run
numpy.savetxt("my_file", my_array)
I want an error to be generated telling me the file already exists, or asking if the user is sure they want to write to the file.
You can check if the file already exists before you write your data:
import os
if not os.path.exists('my_file'): numpy.savetxt('my_file', my_array)
You can pass instead of a filename a file handle to np.savetxt(), e.g.,
import numpy as np
a = np.random.rand(10)
with open("/tmp/tst.txt", 'w') as f:
np.savetxt(f,a)
So you could write a helper for opening the file.
Not in Numpy. I suggest writing to a namedTemporaryFile and checking if the destination file exists. If not, rename the file to a concrete file on the system. Else, raise an error.
Not an error handler, but it's possible to create a new version in the form of:
file
filev2
filev2v3
filev2v3v4
so that no file ever gets overwritten.
n=2
while os.path.exists(f'{file}.txt'):
file = file + f'v{n}'
n+=1