1
$\begingroup$

This question already has an answer here:

I am using Python to import a specific mesh in Blender, mesh that I want to modify after import using some sliders I have defined. So I have the defined the following function for the import:

import bpy, math, bmesh

def addGlasses(context, r):
    # import glasses object
    bpy.ops.import_scene.obj(filepath="myMesh.obj", axis_forward='-Z', axis_up='Y', filter_glob="*.obj;*.mtl")

    # select the imported object so it has a location
    ob = bpy.context.selected_objects[0]

    return ob

and then I have defined the user interface for the imported object:

#
#    User interface
#

from bpy.props import *

class MESH_OT_glasses_add(bpy.types.Operator):
   bl_idname = "mesh.glasses_add"
   bl_label = "Add glasses"
   bl_options = {'REGISTER', 'UNDO'}

   radius = FloatProperty(name="First parameter", default=1.0, min=0.01, max=100.0)

   location = FloatVectorProperty(name="Location")
   rotation = FloatVectorProperty(name="Rotation")
   # Note: rotation in radians!

   def execute(self, context):
      ob = addGlasses(context, self.radius)
      ob.location = self.location
      ob.rotation_euler = self.rotation

    return {'FINISHED'}

So I can move and rotate the object as I wish with the sliders "Location" and "Rotation" but I want also to modify some specific parts of my mesh. Practically, radius is a slider for my mesh that appears when the mesh is imported and I would like to use it's value to stretch some specific vertices for the mesh (I know which ones they are). Is there a way to do that? Because I can't even select a single vertex after import.

$\endgroup$

marked as duplicate by p2or, Ray Mairlot, Denis, iKlsR Apr 13 '17 at 18:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2
$\begingroup$

Set imported object active:

ob = bpy.context.selected_objects[0]
bpy.context.scene.objects.active = ob
$\endgroup$
0
$\begingroup$

Just for the records, I modified my functions like this and now it works:

def addGlasses(context, r, nseg, vstep, nplanes, twist):

   # import glasses object
   bpy.ops.import_scene.obj(filepath="myMesh.obj", axis_forward='-Z', axis_up='Y', filter_glob="*.obj;*.mtl")

   # select the imported object so it has a location
   ob = bpy.context.selected_objects[0]

   # select the imported object active
   bpy.context.scene.objects.active = ob

   # deselect every part of my mesh
   bpy.ops.object.mode_set(mode='EDIT', toggle=False)
   bpy.ops.mesh.select_all(action='DESELECT')

   # change mode
   sel_mode = bpy.context.tool_settings.mesh_select_mode
   bpy.context.tool_settings.mesh_select_mode = [True, False, False]
   bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

   # select the vertices I need
   mesh = ob.data
   mesh.vertices[1].select = True

   bpy.ops.object.mode_set(mode='EDIT', toggle=False)
   bpy.context.tool_settings.mesh_select_mode = sel_mode

   return ob
$\endgroup$

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