|
5 | 5 | # Usage:
|
6 | 6 | # This script is to be run from within the Blender GUI. Tested in Blender
|
7 | 7 | # 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. |
9 | 17 | #
|
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' |
13 | 23 | # >>> 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] |
17 | 27 | # >>> exec(open('/path/to/distort_mesh.py').read());
|
18 | 28 | #
|
19 | 29 |
|
@@ -140,7 +150,7 @@ def distort(file_path, object_prefix, distort_extent, method):
|
140 | 150 | distort_extent))
|
141 | 151 | return
|
142 | 152 | 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) |
144 | 154 | return
|
145 | 155 |
|
146 | 156 | # Clear scene
|
@@ -226,16 +236,28 @@ def distort(file_path, object_prefix, distort_extent, method):
|
226 | 236 | # Default values
|
227 | 237 | distort_extent = 0.1
|
228 | 238 | 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 | + |
230 | 247 | # 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.') |
233 | 250 | 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]) |
240 | 262 |
|
241 | 263 | distort(file_path, object_prefix, distort_extent, method)
|
0 commit comments