Can I get snap settings in a script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dwpasquinelli

I have a scene with grid snapping turned on and configured. I would like to be able to get the grid offset and grid step from a script. Basically, I want the information in this form picture the configure snap form for the scene editor snap settings

Is it possible?

:bust_in_silhouette: Reply From: mechPenSketch

I have an unrelated code that might help:

# ON MAIN MAP
extends TileMap
tool
func set_cell_size(v2):
	# CONFIGURE SNAP > GRID STEP
	#	SET TO CELL_SIZE: VECTOR 2
	CanvasItemEditor.snap_step = v2
	
	# SET CELL SIZE
	cell_size = v2
	pass

So on this script, I made it such that when I set the cell size of a TileMap, the snap is set accordingly.

For your case, you can use “CanvasItemEditor.snap_step” to get the step. Not sure about the rest, though; godot docs has not cover the CanvasItemEditor class yet.

I take back my solution-I thought I got it when I mistook the grid as the snap guide. For now, I’m still looking to get the snap settings by code. I’m currently looking at plugins.

mechPenSketch | 2020-07-10 15:48

:bust_in_silhouette: Reply From: takaturre

It’s probably exposed later: Expose CanvasItemEditor.snap_point() and CanvasItemEditor.snap_angle() to GDscript to use in EditorPlugins. · Issue #11180 · godotengine/godot · GitHub
However, you can hack it by setting it as if a user did it:

Step 1: Find some important nodes: SnapDialog and nodes of SpinBox class inside it.

  • To find them, first get get_base_control() on an EditorPlugin class and then get all nodes in it. (I have an extension just to make the EditorPlugin available: when the plugin loads up it just attaches itself to a singleton tool script: _tool.editor_plugin).
  • For more help see the attached pic or the link below.

Step 2: Modify the UI settings by script (as if user setting values) and then click the ok.

var snap_grid_boxes := []  # The SpinBox objects inside SnapDialog.
func update_snap_grid_size( new_size : Vector2 ):
	# Set the values to the UI controls.
	snap_grid_boxes[2].set_value(new_size.x)
	snap_grid_boxes[3].set_value(new_size.y)
	# Accept. (No need to popup.)
	_tool.ed_snap_grid_popup.get_ok().emit_signal("pressed")

Helper funcs

Many thanks to https://forum.godotengine.org/18051/tool-script-in-3-0

UPDATE: Oops, the question was about GETTING. Just use get_value()instead.

Why does snap_spinbox[2].get_value() and snap_spinbox[3].get_value() both return 0.01 when they, as snap step values, should return an integer like 64? Is it because I called it all the way from the wrong function (_enter_tree()), or am I still missing another step, even after snap_dialog.get_ok().emit_signal("pressed")?

[UPDATE 1] The snap step value will return the intended values after I open the “Configure Snap…” window. Anyone know how to do that via script?

[UPDATE 2] In the end, I stopped getting value from the snapbox. Instead, I created a ConfigFile that stores the snap settings, which would be applied to the snapbox upon _enter_tree().

[UPDATE 3] I’ve tried virtually opening the snap dialog, both by snap_dialog.popup_centered() and through its parent PopupMenu.

When I tried the former, the value are all defaults in the variable sense, not snap settings sense. This mean all entries are 0s, with few exceptions being 0.01 due to their range settings. After a few more testing, I’ve learnt that the snap settings only load after clicking Snapping Options (3 vertical dots icon) > “Configure Snap…”

Now, for the latter, I have to find a PopupMenu that has 7 items total, and with the last one with the text of “Configure Snap…”. This is the code I used to find it:

for c in children:
    if c is PopupMenu:
		if c.get_item_count() == 7:
			if c.get_item_text(6) == "Configure Snap...":
				c.get_parent().emit_signal("pressed")
				c.emit_signal("index_pressed", 6)
				break

However, calling its parent MenuButton to emit_signal("pressed") does not caused it to drop down the menu. Neither does calling emit_signal("index_pressed", 6) from the PopupMenu causes the SnapDialog to pop up.

mechPenSketch | 2020-08-27 09:13