0
votes

I'm using Blender 2.76b, threejs exporter v1.5.0; my goal is to export each mesh in the Blender scene. I noticed that if a single mesh is selected, io_three exports that mesh, so I wrote a simple python script executable in console:

import bpy

for ob in bpy.context.scene.objects:
    bpy.ops.object.select_all(action='DESELECT')
    bpy.ops.object.select_pattern(pattern = ob.name)
    bpy.ops.export.three(
      filepath = 'path to folder' + ob.name + ".json",
      option_vertices=True,
      option_faces=True,
      option_normals=True,
      option_uv_coords=True,
      option_face_materials=True,
      option_colors=True)

It creates files with right names, but with wrong content: all the .json files contain exported content of the scene's first mesh.

How could I get the right behavior? Thanks in advance.

1

1 Answers

2
votes

The three.js exporter exports either the entire scene or the active object. While you are changing the selection, nothing in your script is changing the active object. The abspath() I used allows you to get a path relative to the blend file by starting the path with '//'

import bpy

for ob in bpy.context.scene.objects:
    bpy.ops.object.select_all(action='DESELECT')
    if ob.type == 'MESH':
        ob.select = True
        bpy.context.scene.objects.active = ob
        bpy.ops.export.three(
          filepath = bpy.path.abspath('//' + ob.name + ".json"),
          option_vertices=True,
          option_faces=True,
          option_normals=True,
          option_uv_coords=True,
          option_face_materials=True,
          option_colors=True)