# Importing VRML/X3D objects with their original file name

I'd like to batch import multiple VRML 2.0 files using a script. However, in contrast to other importers (i.e that for .obj files) the X3D importer add-on rename the imported objects as ShapeIndexedFaceSet.(increasing numbers).

I succesfully modified a script originally doing a batch import of .obj files but since the file names are not specified, this is completely unuseful for me!

Any help to give the object the name of the file from which it was imported will be greatly appreciated!

here is the script

import os
import bpy

# location to the directory where the wrml are located
# if you are using windows style backslashes, you need to cancel one with    another
path_to_obj_dir = os.path.join('C:\\', 'prova')

# get list of all files in directory
file_list = sorted(os.listdir(path_to_obj_dir))

# get a list of files ending in 'wrl'
obj_list = [item for item in file_list if item[-3:] == 'wrl']

# loop through the strings in obj_list and add the files to the scene
for item in obj_list:
path_to_file = os.path.join(path_to_obj_dir, item)
bpy.ops.import_scene.x3d(filepath = path_to_file)

• Do you want to import each wrl into a new scene? or simply rename the newly imported objects in one scene? – batFINGER Mar 18 '16 at 3:02

To start added a little script to make accessing the folder easier, we can now set the folder from the UI, Scene properties Scene panel.

import bpy
from bpy.props import StringProperty

bpy.types.Scene.wrl_dir = StringProperty(name="X3D Dir", subtype='DIR_PATH')

def x3d_path_draw(self, context):
self.layout.prop(context.scene, "wrl_dir")

bpy.types.SCENE_PT_scene.remove(x3d_path_draw)
bpy.types.SCENE_PT_scene.append(x3d_path_draw)


Ok with that done, I can use scene.wrl_dir rather than hard coding the folder. Here I simply deselect all objects (equiv of bpy.ops.object.select_all(action='DESELECT')) before calling the import op, then rename the selected objects to the file's name as the newly created objects are selected after the import.

import bpy
import os
context = bpy.context
scene = context.scene

# get list of all files in directory
file_list = sorted(os.listdir(scene.wrl_dir))

# get a list of files ending in 'wrl'
wrl_files = [f for f in file_list if f[-3:].lower() == 'wrl']

# loop through the strings in obj_list and add the files to the scene
for f in wrl_files:
print("Processing %s" % f)
# deselect all objs
for o in context.selected_objects:
o.select = False
path_to_file = os.path.join(scene.wrl_dir, f)
bpy.ops.import_scene.x3d(filepath = path_to_file)
# rename them to file name, change to basename for instance.
for o in context.selected_objects:
o.name = f


Other methods would be to add an empty for each file, name the empty after the file and parent to it. Or import each to a new scene.

• Fantastic!! Thank you very much, it works perfectly!! – Federico Luzzati Mar 18 '16 at 10:17

In Blender 2.8 / 2.81

Line 17 above o.select = False needs to be changed to: o.select_set(False)