|
|
|
|
Reply From: |
neikeq |
If you are working in C++, it’s simple:
CanvasItemEditor::get_singleton()->get_viewport_control()
However, CanvasItemEditor is not directly accessible from GDScript, but you can use this trick to find it (may stop working in the future):
func find_viewport(node, recursive_level):
if node.get_type() == "CanvasItemEditor":
return node.get_child(1).get_child(0).get_child(1).get_child(1)
else:
recursive_level += 1
if recursive_level > 15:
return null
for child in node.get_children():
var result = find_viewport(child, recursive_level)
if result != null:
return result
Usage:
var editor_node = get_node("/root/EditorNode")
var viewport = find_viewport(editor_node, 0) # Returns null if not found
I took this method by Marcos Bitetti (which searches for SpatialEditorViewport) as base.
That’s a crazy workaround
Maybe an issue should be opened about exposing CanvasItemEditor to GDScript (at least for the EditorPlugin API)?
Yes, this workaround may stop working if the structure of CanvasItemEditor children changes. I agree some some of the built-in editor plugins should be exposed to GDScript.
neikeq | 2016-03-08 19:34
It worked! But I guess I formulated my question kinda wrong, haha.
Now I can draw in the canvas 2D, but what I wanted is to create the lines in the world 2D - the idea would be drawing lines ‘connecting’ two sprites, for example. Is this possible using this workaround?
Thanks anyway, I don’t think I would find a function like this without help!
henriquelalves | 2016-03-08 20:19
I guess im implementing this addon with a wrong architecture in mind, haha. After trying a couple of times, now I understand why Path and Path2d are nodes. I think if I implement the data as a node like those, I can create an interface to work on it.
henriquelalves | 2016-03-08 20:52
In Godot 3.2, this gives the error message:
Invalid call. Nonexistent function ‘get_type’ in base ‘EditorNode’
However, “get_class” works.
Aaron Franke | 2021-01-01 08:36