Commit 4c6015cb authored by Breno Rilho Lemos's avatar Breno Rilho Lemos 💬
Browse files

Improve command line arguments passing \n\nInstructions from link...

Improve command line arguments passing \n\nInstructions from link https://blender.stackexchange.com/questions/6817/how-to-pass-command-line-arguments-to-a-blender-python-script
parent 1fdce0b1
# ALICE Open Data Blender animation # ALICE Open Data Blender animation
Run animation example:
`blender -noaudio --background -P animate_particles.py -- -radius=0.05 -duration=1 -camera="BarrelCamera" -datafile="esd-detail.dat"`
In the example above, argument 'radius' has value 0.05, 'duration' has value 10 and so on.
-radius:
particle radius; must be a number; type float
-duration:
animation duration; must be a number; type int
-camera:
defines animation view; must be a string; available options: OverviewCamera, BarrelCamera, ForwardCamera
-datafile:
filename for event data file; must be a string
Implement command line arguments:
https://blender.stackexchange.com/questions/6817/how-to-pass-command-line-arguments-to-a-blender-python-script
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# animate_particles.py - Animate HEP events # animate_particles.py - Animate HEP events
# #
# For console only rendering: # For console only rendering (example):
# $ blender -noaudio --background -P animate_particles.py -- r_part(FLOAT) duration(INT) renderCamera(STRING) # $ blender -noaudio --background -P animate_particles.py -- -radius 0.05 -duration 10 -camera OverviewCamera -datafile esd-detail.dat
# #
# TODO: - Implement command line arguments
# - https://blender.stackexchange.com/questions/6817/how-to-pass-command-line-arguments-to-a-blender-python-script
import os import os
import bpy import bpy
import sys # in order to pass command line arguments
# Create array to store arguments from command line import argparse
argv = sys.argv import sys
argv = argv[argv.index("--") + 1:] # get all args after "--"
# Pass on command line arguments to script:
class ArgumentParserForBlender(argparse.ArgumentParser):
def _get_argv_after_doubledash(self):
try:
idx = sys.argv.index("--")
return sys.argv[idx+1:] # the list after '--'
except ValueError as e: # '--' not in the list:
return []
# overrides superclass
def parse_args(self):
return super().parse_args(args=self._get_argv_after_doubledash())
parser = ArgumentParserForBlender()
parser.add_argument('-radius','--r_part')
parser.add_argument('-duration','--duration')
parser.add_argument('-camera','--render_camera')
parser.add_argument('-datafile','--datafile')
args = parser.parse_args()
bpy.context.user_preferences.view.show_splash = False bpy.context.user_preferences.view.show_splash = False
# Import Drivers, partiles and scence functions: # Import Drivers, partiles and scence functions:
...@@ -21,9 +38,9 @@ filename = os.path.join(os.path.basename(bpy.data.filepath), "drivers.py") ...@@ -21,9 +38,9 @@ filename = os.path.join(os.path.basename(bpy.data.filepath), "drivers.py")
exec(compile(open(filename).read(), filename, 'exec')) exec(compile(open(filename).read(), filename, 'exec'))
# Set animation parameters # Set animation parameters
r_part = float(argv[0]) # Particle radius r_part = float(args.r_part) # Particle radius
simulated_t = 0.02 # in microsseconds simulated_t = 0.02 # in microsseconds
duration = int(argv[1]) # in seconds duration = int(args.duration) # in seconds
fps = 24 fps = 24
resolution_percent = 100 resolution_percent = 100
...@@ -31,7 +48,7 @@ resolution_percent = 100 ...@@ -31,7 +48,7 @@ resolution_percent = 100
outputPath = "/tmp/blender/" outputPath = "/tmp/blender/"
fileIdentifier = "PhysicalTrajectories_" fileIdentifier = "PhysicalTrajectories_"
## RenderCameras: ["ForwardCamera", "OverviewCamera", "BarrelCamera"] ## RenderCameras: ["ForwardCamera", "OverviewCamera", "BarrelCamera"]
renderCamera= str(argv[2]) # "ForwardCamera" renderCamera= args.render_camera
renderAnimation = True # True renderAnimation = True # True
saveBlenderFile = False # False saveBlenderFile = False # False
...@@ -44,7 +61,7 @@ driver.configure(renderCamera, duration, fps, simulated_t, outputPath, fileIdent ...@@ -44,7 +61,7 @@ driver.configure(renderCamera, duration, fps, simulated_t, outputPath, fileIdent
""" """
# Create and configure animation driver # Create and configure animation driver
driver = dataDriver("AlirootFileGenerator","esd-detail.dat") # Simple dataDriver takes one parameters: filename driver = dataDriver("AlirootFileGenerator",args.datafile) # Simple dataDriver takes one parameters: filename
driver.configure(renderCamera, duration, fps, simulated_t, outputPath, fileIdentifier, resolution_percent) driver.configure(renderCamera, duration, fps, simulated_t, outputPath, fileIdentifier, resolution_percent)
### Build scene ### Build scene
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment