0
$\begingroup$

I'm trying to snap the 2D cursor to the origin point(u=0,v=0) via python code.

Initial state: 2D Cursor at any point

Final State: 2D Cursor on origin point

Could anyone help me do that using python code?

$\endgroup$

2 Answers 2

1
$\begingroup$

If you enable "Python Tooltips" in settings under Interface > Display the python command will be shown when you hover over any menu item.

If you also enable "Developer Extras" you can right-click on menu items and the context menu will have an new entry for "Online Python Reference" which will open up the Blender Python API documentation for that specific python command.

enter image description here

Since this was an entry in a Pie menu and you can't right-click on those I used the search menu (F3) too look it up and right click on that instead.

This lead me to this documentation which should be what you need.

Addendum

Operators always work within a certain context. For this operator the active area needs to be the uv editor that blender knows internally as the "IMAGE_EDITOR".

Assuming you have the uv editor open you can cycle through the area's on screen to find it and store its reference in a variable. You can now use this variable to temporarily override the context through a with statement and call the operator within it. It would look something like this.

area = [area for area in bpy.context.screen.areas if area.type == "IMAGE_EDITOR"][0]
 
with bpy.context.temp_override(area=area):
    bpy.ops.uv.snap_cursor(target='ORIGIN')
$\endgroup$
2
  • $\begingroup$ This works, but I was only able to do it by changing context by opening a new UV-editor window by code and changing it there. How can I reference the current opened UV-editor window to do it? $\endgroup$ Dec 17, 2023 at 15:41
  • 1
    $\begingroup$ I have provided a little extra content to my answer to explain how you can temporarily override the context so you can execute an operator in the right context. $\endgroup$
    – Jan-Willem
    Dec 19, 2023 at 10:04
-1
$\begingroup$

The 2D cursor in the UV editor is only a visual aid and cannot be controlled by the API.

$\endgroup$
1
  • 1
    $\begingroup$ Hello, Jishnu. welcome to BSE. Before getting downvoted, in the light of this answer, I would recommend you reconsider. This is incorrect. The UV cursor can be snapped either via the UI, or via the API. $\endgroup$
    – Robin Betts
    Dec 17, 2023 at 6:38

You must log in to answer this question.

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