snapping objects to 3d cursor

i want to snap an object to the 3d cursor, and the api and console autocomplete function tells me it’s:

bpy.ops.view3d.snap_selected_to_cursor() # snapping object to 3d cursor

but when running the script or pressing enter in the console, the ‘context’ is wrong…

how can this be solved?

because it is an operation that must take place in 3dview (context is then ‘VIEW_3D’), if you run it from console the context becomes ‘CONSOLE’, if you run it from text editor, then the context becomes ‘TEXT_EDITOR’

At the moment your cleanest way will be to write an operator that runs in 3dview (use the templates in the Text Editor), that way you will have the correct context. Not ideal for testing some bits of code, but it is generally an incentive to learn more about bpy and gui coding. Such code as you are trying to use will end up being used in 3dview anyway, running from one of the panels or a menu item.

right, that makes sense, do you know a quick way to change to 3d view, and at the end back to text editor?


works, but something with 3d_view or view3d doesn’t (yet…)

switching view type is too ugly :slight_smile: but use ‘VIEW_3D’ , like i said. not 3d_view, or view3d

very interesting

show us how it can work with a little script
and let say the default cube object in viewport

happy 2.5

in objectmode, straight from Text_Editor :

bpy.context.active_object.location = bpy.context.scene.cursor_location

ok , well it works so far, but python is not executing an essential part of my code, changing the empty display type to ‘image’

bpy.types.Object.empty_draw_type = ‘IMAGE’

in the console it’s not giving any errors, but it’s just not executed…

and thanks zeffii, that is alot easier then snapping the object :slight_smile:

what about the area changes
like you got bpy.context.area.type=‘GRAPH_EDITOR’
then you needed to change it to the 3D view may be

or why did you have this problem ?

happy 2.5

the problem was that i called something that’s only available in the 3d view, and the script executed from the text editor, so this gave an error.

if your active object is an Empty

mt = bpy.context.active_object
print(mt.empty_draw_type)   # will print current type

you can set it to any of the following; ‘PLAIN_AXES’, ‘ARROWS’, ‘SINGLE_ARROW’, ‘CIRCLE’, ‘CUBE’, ‘SPHERE’, ‘CONE’, ‘IMAGE’
in your case :

mt.empty_draw_type = 'IMAGE'

ok, just one step from finishing this script…

i can open an image with’’) , but how to assign it to the empty?
various append things have failed so far…

and thanks zeffii for also solving that problem, thought that object always was the active object, but i see it’s not…

again, not an obvious one, but you’ll start to see a pattern :

# assuming 
# mt = bpy.context.active_object
# mt.empty_draw_type = 'IMAGE'

# then =["ExtendX.jpg"]