3
$\begingroup$

I am getting an error when trying to center the cursor in a python script

import bpy

bpy.ops.import_scene.obj(filepath=objFilePath, axis_up='Y')

bpy.ops.object.select_by_type(type='MESH')

bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
view3d = bpy.context.screen.areas['VIEW_3D'].spaces[0];
view3d.pivot_point = 'CURSOR'
view3d.cursor_location = (0.0,0.0,0.0)  
USER = os.environ['USER'];

bpy.ops.export_scene.fbx(filepath="/Users/"+USER+"/house.fbx", path_mode='RELATIVE', object_types={'MESH'})

Error:

Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
  File "/private/var/folders/48/ptx7wqz138x4_shgs5nk6szm0000gn/T/AppTranslocation/DEB386B3-C3FC-4FAB-B7A2-0954C07B91CF/d/blender.app/Contents/MacOS/../Resources/2.78/scripts/modules/bpy/ops.py", line 189, in __call__
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.view3d.snap_cursor_to_center.poll() failed, context is incorrect

I am trying to center the origin of my model in batch mode of blender.

$\endgroup$
5
  • $\begingroup$ What is the error? $\endgroup$
    – satishgoda
    Jun 14, 2017 at 22:35
  • $\begingroup$ I added it above $\endgroup$
    – luii
    Jun 14, 2017 at 22:37
  • $\begingroup$ If you are running the script from the script editor or Python console, then this error makes sense. The bpy.ops.view3d.snap operators must be run with view3d being active. $\endgroup$
    – satishgoda
    Jun 14, 2017 at 22:40
  • $\begingroup$ Does that mean I should just add this line bpy.ops.view3d.snap_cursor_to_active()? $\endgroup$
    – luii
    Jun 14, 2017 at 22:44
  • $\begingroup$ I have added an answer. $\endgroup$
    – satishgoda
    Jun 14, 2017 at 23:03

2 Answers 2

2
$\begingroup$

Every operator requires a proper context to run in. Since you mentioned that you are running the script in batch mode and accessing a view3d operator, you must provide the exact context to the operator

import bpy
# in this case the area at index 4 happens to be view3d
view3d = bpy.context.screen.areas[4]
context_override = {'window': bpy.context.window, 
                    'screen': bpy.context.screen, 
                    'area' : view3d}
bpy.ops.view3d.snap_cursor_to_center(context_override)

By doing this, I am able to snap the cursor by running the script from the Python console. Also, you do not need the call to poll() since you are providing the explicit context.

Relevant Links

$\endgroup$
1
  • $\begingroup$ I updated above $\endgroup$
    – luii
    Jun 16, 2017 at 20:04
0
$\begingroup$

You can assign a Vector((0.0, 0.0, 0.0)) or just a float array to Cursor.location:

bpy.context.scene.cursor.location = (0.0, 0.0, 0.0) # Blender 2.8x
bpy.context.scene.cursor_location = (0.0, 0.0, 0.0) # Blender 2.7x
$\endgroup$
1
  • 3
    $\begingroup$ Please refrain from posting extremely short answers including nothing but code. Also, embed your code with the code tags so it looks like a python code snippet, you can do all this by editing your existing answer and look for the { } button. This makes the code easier to see and follow, and shows a different color for the type of value. Lastly, please add a explanation for why and how your answer would answer a question. $\endgroup$
    – FreemoX
    Aug 2, 2017 at 16:26

You must log in to answer this question.

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