1
$\begingroup$

I'm working up to building a small script which will allow me to import and render 2D views of several OBJ models, but I'm struggling at the moment with the very basic first step, which is importing a model from a folder on my computer. I attempted to follow the code in the script in a somewhat related question, but even if I directly copy-paste the code and just change the directory path to where my model is located, when I press Run Script, nothing happens. I don't see any errors, I don't see anything happen, just nothing. I'm fairly new to both Blender and Blender scripting, so I'm not very sure what's going on and any help would be appreciated!

This is my code at the moment (copied and pasted from the link above, but modified to have a Linux file path):

import bpy
import os
import glob

# Model directory
model_dir = '/home/yuerno/Downloads/test'

# Specify OBJ files
model_files = glob.glob(model_dir + "*.obj")

for f in model_files:
    head, tail = os.path.split(f)
    collection_name = tail.replace('.obj', '')
    bpy.ops.import_scene.obj(filepath=f)
    myCol = bpy.data.collections.new(collection_name)
    bpy.context.scene.collection.children.link(myCol)
    for ob in bpy.context.selected_objects:
        myCol.objects.link(ob)
$\endgroup$
0

1 Answer 1

2
$\begingroup$

Try adding the trailing / to your directory path. You're searching for /home/yuerno/Downloads/test*.obj instead of /home/yuerno/Downloads/test/*.obj

Or better yet, use os.path.join to put the parts together:

# Specify OBJ files
model_files = glob.glob(os.path.join(model_dir, "*.obj"))
$\endgroup$
2
  • $\begingroup$ Can't believe I missed a simple trailing slash! Thanks for pointing it out $\endgroup$
    – Yuerno
    Aug 2, 2020 at 1:08
  • 1
    $\begingroup$ Additionally - you may want to remove / unlink the the newly created objects from the "Scene", as they will now be in the new "Scene Collections". You can scene unlink after your collection link. i.e.: myCol.objects.link(ob) bpy.context.scene.collection.objects.unlink(ob) $\endgroup$
    – SimplSam
    Aug 2, 2020 at 1:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.