Last active
September 20, 2026 20:00
-
-
Save redxdev/86e2bee40dc4a9a0102e1d5ed937184c to your computer and use it in GitHub Desktop.
godot play from camera plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Make a new plugin and throw this in the plugin.gd. | |
| @tool | |
| extends EditorPlugin | |
| var _play_from_camera_button: CheckBox | |
| var _play_from_camera := false | |
| func _get_plugin_name() -> String: | |
| return "PlayFromCamera" | |
| func _get_state() -> Dictionary: | |
| return { "play_from_camera": _play_from_camera } | |
| func _set_state(state: Dictionary) -> void: | |
| _play_from_camera = state.get("play_from_camera", _play_from_camera) | |
| if _play_from_camera_button: | |
| _play_from_camera_button.button_pressed = _play_from_camera | |
| func _enter_tree() -> void: | |
| _play_from_camera_button = CheckBox.new() | |
| _play_from_camera_button.text = tr("Play from Camera") | |
| _play_from_camera_button.button_pressed = _play_from_camera | |
| _play_from_camera_button.toggled.connect(_on_play_from_here_toggled) | |
| add_control_to_container(CONTAINER_TOOLBAR, _play_from_camera_button) | |
| func _exit_tree() -> void: | |
| remove_control_from_container(CONTAINER_TOOLBAR, _play_from_camera_button) | |
| _play_from_camera_button = null | |
| func _run_scene(scene: String, args: PackedStringArray) -> PackedStringArray: | |
| var viewport := EditorInterface.get_editor_viewport_3d() | |
| var camera := viewport.get_camera_3d() | |
| if camera and _play_from_camera: | |
| var position_str := "%f,%f,%f,%f,%f,%f" % [camera.position.x, camera.position.y, camera.position.z, camera.rotation_degrees.x, camera.rotation_degrees.y, camera.rotation_degrees.z] | |
| args.append_array(["--play-from-camera", position_str]) | |
| return args | |
| func _on_play_from_here_toggled(toggled_on: bool) -> void: | |
| _play_from_camera = toggled_on |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This ready function is just an example - throw _get_spawn_transform in whatever script handles spawning your player, or use this as-is | |
| # if your player is placed directly into a scene and you just want to move it. | |
| func _ready() -> void: | |
| global_transform = _get_spawn_transform() | |
| func _get_spawn_transform() -> Transform3D: | |
| var arg_idx := OS.get_cmdline_args().find("--play-from-camera") | |
| if arg_idx >= 0 and arg_idx + 1 < OS.get_cmdline_args().size(): | |
| var position_str := OS.get_cmdline_args()[arg_idx + 1] | |
| print("Player starting from %s" % position_str) | |
| position_str = position_str.substr(0, position_str.length() - 1) | |
| var components := position_str.split_floats(",", false) | |
| if components.size() == 6: | |
| var pos := Vector3(components[0], components[1], components[2]) | |
| var rot := Vector3(deg_to_rad(components[3]), deg_to_rad(components[4]), deg_to_rad(components[5])) | |
| return Transform3D(Basis.from_euler(rot), pos) | |
| else: | |
| push_error("Malformed --play-from-camera, ignoring") | |
| return global_transform |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment