0
$\begingroup$

I want to use scripting to import a .obj into an existing scene, then rotate and scale that .obj. Here's what I've been using to do that:

import bpy

#open the scene
scene_loc = '#the location of the scene'
bpy.ops.wm.open_mainfile(filepath=scene_loc)

#deselect everything
bpy.context.view_layer.objects.active = None

#open the object and assign it a variable
object_loc = '#the location of the object'
obj = bpy.ops.import_scene.obj(filepath=object_loc)

#make the selected object the active object
bpy.context.view_layer.objects.active = obj

I've tried various iterations using answers from here to make the .obj active right when it imports, but none of them have worked. It would be nice if I could assign that imported .obj to a variable and then rotate/scale it from there. Is there a way to assign a variable to an imported .obj in the Blender Python API?

$\endgroup$
4
  • $\begingroup$ ops only return whether they succeeded. It should already be the only selected and active object after import (same is true for any op that creates an object). $\endgroup$
    – scurest
    Jul 18, 2022 at 21:16
  • $\begingroup$ @scurest I thought non-modal operators returned {'CANCELLED'} when they fail and {'FINISHED'} when they work. $\endgroup$ Jul 19, 2022 at 0:42
  • $\begingroup$ @MartyFouts Wouldn't you call that "whether they succeeded"? $\endgroup$
    – scurest
    Jul 19, 2022 at 0:57
  • $\begingroup$ @scurest I misread 'whether' as 'when' in your comment. My bad. $\endgroup$ Jul 19, 2022 at 17:49

1 Answer 1

0
$\begingroup$

You can't assign a variable to an imported object

  • First deselect the active object before importing
  • Import the object and set it to the active object
  • Rotate/Scale the active object
import bpy
from math import radians


bpy.context.view_layer.objects.active = None


def import_obj(filepath):
    ''' Import OBJ file.

    filepath (str) - Filepath of OBJ file.
    '''
    bpy.ops.import_scene.obj(
        filepath = filepath,
        filter_glob = '*.obj',
        use_edges = True,
        use_smooth_groups = True,
        use_split_objects = True,
        use_split_groups = False,
        use_groups_as_vgroups = False,
        use_image_search = True,
        split_mode = 'ON',
        global_clamp_size = 0.0,
        axis_forward = '-Z',
        axis_up = 'Y'
    )


import_obj('') # filepath

bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]

obj = bpy.context.object
obj.scale = (2, 2, 2)
obj.rotation_euler = (radians(90), 0, 0)
$\endgroup$
2
  • $\begingroup$ Worked on its own, but did not work to import a .obj to the existing scene. Getting the same error I was getting before: "'Context' object has no attribute 'selected_objects'". $\endgroup$
    – Tesso
    Jul 19, 2022 at 13:35
  • $\begingroup$ Provide the screenshot of the error message $\endgroup$
    – Karan
    Jul 19, 2022 at 15:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .