0
$\begingroup$

I am writing a python script to export a 3D model as .stl from blender. here is my code

import bpy
import time
start_time=time.time
from mathutils import *
from math import *
import random
D = bpy.data
C = bpy.context
#Clean the scene
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()
bpy.ops.object.select_by_type(type='CAMERA')
bpy.ops.object.delete()
bpy.ops.object.select_by_type(type='LAMP') 
bpy.ops.object.delete()
#Import the model .mhx in the scene
bpy.ops.import_scene.makehuman_mhx(filepath="model.mhx")
bpy.ops.object.mode_set(mode='POSE');
# modify this model and export it as .stl
#----under construction------
#select the layer 1 and after that select all object in the layer 1.
bpy.context.scene.layers[0] = True
bpy.context.scene.layers[1] = False
#I used this command to select the object in the layer but it did not work.
bpy.ops.outliner.object_operation('SELECT')
bpy.ops.export_mesh.stl("/model.stl")
$\endgroup$
  • 1
    $\begingroup$ It's not entirely clear to me what you're trying to do here... Are you trying to select an object by name, or simply every object in a specific layer? $\endgroup$ – MaVCArt Nov 27 '15 at 11:24
  • $\begingroup$ @MaVCArt, after selecting the layer1 that contains just 1 object, I need to select it (like using A). $\endgroup$ – BetterEnglish Nov 27 '15 at 15:21
2
$\begingroup$

To select objects that are currently visible in the 3dview, which means they are on any visible layers, you can set the select property of visible objects.

for obj in bpy.context.visible_objects:
    obj.select = True

If you want to check every object on layer[0] and still catch ones that may be hidden you can test the layer value of each object.

for obj in bpy.data.objects:
    if obj.layers[0]:
        obj.select = True

After deleting and deselecting objects you may also want to ensure that the active object is valid.

bpy.context.scene.objects.active = bpy.context.selected_objects[0]
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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