Skip to content

Commit 83a417c

Browse files
committed
make script work on bash command line too
Signed-off-by: Mabel Zhang <[email protected]>
1 parent dcd6ee7 commit 83a417c

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

examples/scripts/blender/distort_mesh.py

+39-17
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@
55
# Usage:
66
# This script is to be run from within the Blender GUI. Tested in Blender
77
# 2.92.
8-
# distort_mesh.py <file_path> <object_prefix> [distort_extent] [method]
8+
# distort_mesh.py <mesh_path> <object_prefix> [distort_extent] [method]
9+
# where
10+
# mesh_path: Absolute path to mesh file
11+
# object_prefix: Prefix of object name in Scene Collection in Blender. The
12+
# prefix instead of the name is required, because if the script is
13+
# repeatedly run, Blender will append numerical suffixes .001, .002, etc.
14+
# to the object name.
15+
# distort_extent: A floating point number in the range of [0, 1]
16+
# method: Distortion method. A list of strings.
917
#
10-
# Example:
11-
# From the console panel, run
12-
# >>> file_path = '/path/to/file.dae'
18+
# Example from command line:
19+
# $ blender -b -P distort_mesh.py -- /path/to/mesh.obj object_prefix 0.005 "['subdiv_mod', 'vert_rand', 'edge_subdiv']"
20+
#
21+
# Example from Blender Scripting GUI Python command line:
22+
# >>> file_path = '/path/to/mesh.dae'
1323
# >>> object_prefix = 'Cube'
14-
# >>> distort_extent = 0.1 # float in range [0, 1]
15-
# >>> method = 'deform' # method of distortion
16-
# >>> sys.argv = ['distort_mesh.py', file_path, object_prefix, distort_extent, method]
24+
# >>> distort_extent = 0.005 # float in range [0, 1]
25+
# >>> method = ['subdiv_mod', 'vert_rand', 'edge_subdiv']
26+
# >>> sys.argv = [file_path, object_prefix, distort_extent, method]
1727
# >>> exec(open('/path/to/distort_mesh.py').read());
1828
#
1929

@@ -140,7 +150,7 @@ def distort(file_path, object_prefix, distort_extent, method):
140150
distort_extent))
141151
return
142152
if not isinstance(method, list):
143-
print('ERROR: method parameter must be specified as a list')
153+
print('ERROR: method parameter "%s" must be specified as a list' % method)
144154
return
145155

146156
# Clear scene
@@ -226,16 +236,28 @@ def distort(file_path, object_prefix, distort_extent, method):
226236
# Default values
227237
distort_extent = 0.1
228238
method = ['subdiv_mod', 'vert_rand', 'edge_subdiv']
229-
239+
240+
# Parse everything after `--`
241+
argStartI = 0
242+
for i in range(len(sys.argv)):
243+
if sys.argv[i] == '--':
244+
argStartI = i + 1
245+
break
246+
230247
# Parse args
231-
if len(sys.argv) < 2:
232-
print('ERROR: Mesh name not provided. No object to distort.')
248+
if len(sys.argv) - argStartI < 2:
249+
print('ERROR: Mesh name prefix not provided. No object to distort.')
233250
else:
234-
file_path = sys.argv[1]
235-
object_prefix = sys.argv[2]
236-
if len(sys.argv) > 2:
237-
distort_extent = float(sys.argv[3])
238-
if len(sys.argv) > 3:
239-
method = sys.argv[4]
251+
file_path = sys.argv[argStartI]
252+
object_prefix = sys.argv[argStartI + 1]
253+
if len(sys.argv) - argStartI > 1:
254+
distort_extent = float(sys.argv[argStartI + 2])
255+
if len(sys.argv) - argStartI > 2:
256+
# If arg is not already a list (happens when script run from
257+
# bash command line, for example, as opposed to Blender Python
258+
# prompt), convert string literal of a list, to a list.
259+
if type(sys.argv[argStartI + 3]) != list:
260+
import ast
261+
method = ast.literal_eval(sys.argv[argStartI + 3])
240262

241263
distort(file_path, object_prefix, distort_extent, method)

0 commit comments

Comments
 (0)