73 lines
2.7 KiB
Python
73 lines
2.7 KiB
Python
# coding: utf-8
|
|
# original code https://blender.stackexchange.com/a/134596
|
|
# Copyright (C) 2023 Ilia Kurochkin <brothermechanic@yandex.com>
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
'''
|
|
DESCRIPTION.
|
|
Custom parser for Blender and FreeCAD.
|
|
This is an overlay for standard ArgumentParser.
|
|
'''
|
|
__version__ = '0.1'
|
|
|
|
import argparse
|
|
import sys
|
|
|
|
|
|
class CustomArgumentParser(argparse.ArgumentParser):
|
|
"""
|
|
This class is identical to its superclass, except for the parse_args
|
|
method (see docstring). It resolves the ambiguity generated when calling
|
|
Blender from the CLI with a python script, and both Blender and the script
|
|
have arguments. E.g., the following call will make Blender crash because
|
|
it will try to process the script's -a and -b flags:
|
|
>>> blender --python my_script.py -a 1 -b 2
|
|
|
|
To bypass this issue this class uses the fact that Blender will ignore all
|
|
arguments given after a double-dash ('--'). The approach is that all
|
|
arguments before '--' go to Blender, arguments after go to the script.
|
|
The following calls work fine:
|
|
>>> blender --python my_script.py -- -a 1 -b 2
|
|
>>> blender --python my_script.py --
|
|
"""
|
|
|
|
@staticmethod
|
|
def _get_argv_after_doubledash():
|
|
"""
|
|
Given the sys.argv as a list of strings, this method returns the
|
|
sublist right after the '--' element (if present, otherwise returns
|
|
an empty list).
|
|
"""
|
|
try:
|
|
idx = sys.argv.index("--")
|
|
return sys.argv[idx+1:] # the list after '--'
|
|
except ValueError as e: # '--' not in the list:
|
|
return None
|
|
|
|
# overrides superclass
|
|
def parse_args(self, args=None, namespace=None):
|
|
"""
|
|
This method is expected to behave identically as in the superclass,
|
|
except that the sys.argv list will be pre-processed using
|
|
_get_argv_after_doubledash before. See the docstring of the class for
|
|
usage examples and details.
|
|
"""
|
|
return super().parse_args(
|
|
args=args or self._get_argv_after_doubledash(),
|
|
namespace=namespace
|
|
)
|
|
|
|
def parse_known_args(self, args=None, namespace=None):
|
|
''' Parse only known args '''
|
|
return super().parse_known_args(
|
|
args=args or self._get_argv_after_doubledash(),
|
|
namespace=namespace
|
|
)
|