1
$\begingroup$

I have a simple block of code here that helps me set the metalness, roughness and diffuse value of every BSDF shader on a GLB file I import to the scene:

object = bpy.ops.object
object.select_all(action='SELECT')

for mat in bpy.data.materials:
    if not mat.use_nodes:
        mat.metallic = 1
        mat.diffuse_color != (0,0,0)
        mat.roughness != 0.5
        continue
    for n in mat.node_tree.nodes:
        if n.type == 'BSDF_PRINCIPLED':
            n.inputs["Metallic"].default_value = 0
            n.inputs[0].default_value = (1,1,1,1)
            n.inputs[7].default_value = 0.5
            
object.select_all(action='SELECT')

At the end of the script, I want to add so that Blender will export the selected mesh as a glb again (all default setting) to "D:\Processed_Files". I'd like the name of the file to stay exactly the same as the original imported GLB.

Any suggestion on how to do this correctly?

UPDATE: Base on some insight, I've added parser arguments at the beginning of the script to get file info from command line:

parser = ArgumentParser()

parser.add_argument("-i", "--inputDirectory", help='Import File Path', default=' Import File Path')
parser.add_argument("-f", '--input_file', help='input file name', default='input file name')

args = parser.parse_args()
iDirectory = args.inputDirectory
file_name = args.input_file
file_path = iDirectory + '/' + file_name

bpy.ops.import_scene.gltf(filepath=file_path)

I'm hoping with this added, I can execute something like this in command line:

blender.exe --python myScript.py --inputDirectory file/path/folder --input_file process_file.glb

My expectation is that command line would open blender and import the file that I have defined in the command. But I am getting this error:

blender.exe: error: unrecognized arguments: --python myScript.py

Is there any reason for the error? For the record, I have set the cd path to where the "myScript.py" file is located.

$\endgroup$
7
  • $\begingroup$ Where did the import happen? How did it know the original name to import? $\endgroup$
    – emackey
    Commented Feb 14, 2022 at 15:02
  • $\begingroup$ I've only imported the file manually at the moment, and the imported file names are showing in the python info window. I'm very new to scripting, is it correct to assume if the file names are showing in the info channel, it's possibly stored somewhere in Blender? $\endgroup$ Commented Feb 15, 2022 at 16:04
  • $\begingroup$ I don't think so. That panel shows Python versions of commands initiated by the user on the UI, along with hard-coded parameters matching the user's selection at the time. I don't think those parameters become permanently stored in the Blender project. You probably want to automate the import, making it part of your script. $\endgroup$
    – emackey
    Commented Feb 15, 2022 at 23:19
  • $\begingroup$ Hi emackey, I've added some more info in the post above. With your suggestion, I added parser arguments to automate the import. However, I am running into a roadblock with blender not recognizing my --python or any other custom arugments. If you have a moment, please take a look and any suggestion will be greatly appreciated. Thanks. $\endgroup$ Commented Feb 17, 2022 at 21:07
  • $\begingroup$ Not sure but maybe the Python args need -- as a prefix? --python myScript.py -- --inputDirectory file/path/folder --input_file process_file.glb $\endgroup$
    – emackey
    Commented Feb 17, 2022 at 22:04

1 Answer 1

1
$\begingroup$

When you do parse_args() it parses sys.argv, which is all the arguments to Blender. The first one is --python which your ArgumentParser does not recognize, so it errors.

Blender lets you pass your own arguments after a --. You can get all the arguments after -- with

args = sys.argv[sys.argv.index("--") + 1:]

You can pass this to arg_parse, arg_parse(args) to get it to parse only your own args, and obviously you would call Blender as

blender.exe --python myScript.py -- --inputDirectory file/path/folder --input_file process_file.glb

But in fact there's no need to use argparse at all, just pass the input file name as the only argument after --

blender.exe --python myScript.py -- file/path/folder/process_file.glb

and access with

file_path = sys.argv[sys.argv.index("--") + 1]
file_name = os.path.basename(file_path)

Btw if all you want to do is change the baseColor/metallic/roughness I really recommend you don't use Blender at all and just manipulate the glTF JSON directly. It's a really simple format, and passing through Blender is very far from transparent.

$\endgroup$
1
  • $\begingroup$ Thanks scurest, this is helpful in many ways. Also thanks for the tip on editing the JSON directly, I wasn't aware that is a possibility and it definitely makes more sense than calling Blender to do this simple task. $\endgroup$ Commented Feb 18, 2022 at 15:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .