I am having trouble figuring out the correct context for bpy.ops.transform.translate. I am currently running a script that calls a create_bounding_box addon, which I figured out how to override the context for, but after it runs I can't perform a simple object translation.

Here is the relevant parts of the code:

  for window in bpy.context.window_manager.windows:
    screen = window.screen
    for area in screen.areas:
      if area.type == 'VIEW_3D':
          override = bpy.context.copy() 

boundbox_add is an addon, and the code is here https://github.com/sambler/addonsByMe/blob/master/create_bound_box.py

boundbox_add breaks if I don't either do the above (which I thought was overriding the context) or set bpy.context.scene.objects.active = bpy.context.scene.objects[0].

After running boundbox_add, I get poll() error for transform stating that the context is wrong.


Could do it without using the transform.translate operator

import bpy
from mathutils import Vector
# open another file.

scene = bpy.context.scene
objects = [o for o in scene.objects]
override = bpy.context.copy()
override["selected_objects"] = objects
override["object"] = objects[0]
bbox = scene.objects.active

for o in objects:
    o.location -= bbox.location # origin after translate above

bbox.location -= bbox.location

or run for each scene in the file for scene in bpy.data.scenes

Not a fan of opening new blender file in script as above. My suggestion would be to use a handler or run a batch command.

  • $\begingroup$ Sorry, I am still slightly confused. I am reading in a .blend file. The bpy.context does not have an active_object when I read it in, and that seems to be breaking bpy.ops.mesh.boundbox_add(). So I fixed that by just setting context.scene.objects.active = context.scene.objects[0]. Then the boundbox_add() script runs ok. When it finishes, the bounding box is the active_object. But translate.poll() still says incorrect context. My goal is to actually select all of the objects in the scene and move translate them by the location of bounding box (centering them at origin). $\endgroup$
    – Wes
    Jul 6 '16 at 16:54
  • $\begingroup$ This is still not working for me. I am not sure what you mean when you have "bbox = context.active_obect" .. context.active_object doesn't exist at that point and errors. And Translate still gives a polling error. I did manage to make it work using data.objects and their locations but that is a bad work around for me because other ops do not have a 1to1 correspondence with properties of the data.objects. $\endgroup$
    – Wes
    Jul 6 '16 at 22:31
  • $\begingroup$ Oops, didn't see the open_mainfile. Edited answer accordingly. $\endgroup$
    – batFINGER
    Jul 8 '16 at 13:30

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.