You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-24 15:26:15 +00:00
Completed the support for plugins! It is not possible to add plugins.
Not all APIs are provided yet, please request whathever you are missing. Some example plugins are provided in demos/plugins. Just copy them to a folder in your project named addons/ and then enable them from the project settings. Have fun!
This commit is contained in:
81
demos/plugins/custom_import_plugin/import_plugin.gd
Normal file
81
demos/plugins/custom_import_plugin/import_plugin.gd
Normal file
@@ -0,0 +1,81 @@
|
||||
tool
|
||||
|
||||
extends EditorImportPlugin
|
||||
|
||||
|
||||
# Simple plugin that imports a text file with extension .mtxt
|
||||
# which contains 3 integers in format R,G,B (0-255)
|
||||
# (see example .mtxt in this folder)
|
||||
# Imported file is converted to a material
|
||||
|
||||
var dialog = null
|
||||
|
||||
func get_name():
|
||||
return "silly_material"
|
||||
|
||||
func get_visible_name():
|
||||
return "Silly Material"
|
||||
|
||||
func import_dialog(path):
|
||||
var md = null
|
||||
if (path!=""):
|
||||
md = ResourceLoader.load_import_metadata(path)
|
||||
dialog.configure(self,path,md)
|
||||
dialog.popup_centered()
|
||||
|
||||
func import(path,metadata):
|
||||
|
||||
assert(metadata.get_source_count() == 1)
|
||||
|
||||
var source = metadata.get_source_path(0)
|
||||
var use_red_anyway = metadata.get_option("use_red_anyway")
|
||||
|
||||
var f = File.new()
|
||||
var err = f.open(source,File.READ)
|
||||
if (err!=OK):
|
||||
return ERR_CANT_OPEN
|
||||
|
||||
var l = f.get_line()
|
||||
|
||||
f.close()
|
||||
|
||||
var channels = l.split(",")
|
||||
if (channels.size()!=3):
|
||||
return ERR_PARSE_ERROR
|
||||
|
||||
var color = Color8(int(channels[0]),int(channels[1]),int(channels[2]))
|
||||
|
||||
var material
|
||||
|
||||
if (ResourceLoader.has(path)):
|
||||
# Material is in use, update it
|
||||
material = ResourceLoader.load(path)
|
||||
else:
|
||||
# Material not in use, create
|
||||
material = FixedMaterial.new()
|
||||
|
||||
if (use_red_anyway):
|
||||
color=Color8(255,0,0)
|
||||
|
||||
material.set_parameter(FixedMaterial.PARAM_DIFFUSE,color)
|
||||
|
||||
# Make sure import metadata links to this plugin
|
||||
|
||||
metadata.set_editor("silly_material")
|
||||
|
||||
# Update the import metadata
|
||||
|
||||
material.set_import_metadata(metadata)
|
||||
|
||||
|
||||
# Save
|
||||
err = ResourceSaver.save(path,material)
|
||||
|
||||
return err
|
||||
|
||||
|
||||
func config(base_control):
|
||||
|
||||
dialog = preload("res://addons/custom_import_plugin/material_dialog.tscn").instance()
|
||||
base_control.add_child(dialog)
|
||||
|
||||
67
demos/plugins/custom_import_plugin/material_dialog.gd
Normal file
67
demos/plugins/custom_import_plugin/material_dialog.gd
Normal file
@@ -0,0 +1,67 @@
|
||||
tool
|
||||
extends ConfirmationDialog
|
||||
|
||||
var src_fs
|
||||
var dst_fs
|
||||
var import_plugin
|
||||
|
||||
func configure(p_import_plugin,path,metadata):
|
||||
import_plugin=p_import_plugin
|
||||
if (metadata):
|
||||
# metadata from previous import exists, fill in fields
|
||||
assert( metadata.get_source_count() > 0 )
|
||||
# Always expand the source paths
|
||||
var src_path = import_plugin.expand_source_path( metadata.get_source_path(0) )
|
||||
get_node("src_file").set_text(src_path)
|
||||
get_node("dst_file").set_text(path)
|
||||
# Fill in from metadata options
|
||||
get_node("use_red_anyway").set_pressed( metadata.get_option("use_red_anyway") )
|
||||
|
||||
|
||||
func _ready():
|
||||
|
||||
src_fs = FileDialog.new()
|
||||
src_fs.set_mode(FileDialog.MODE_OPEN_FILE)
|
||||
src_fs.set_access(FileDialog.ACCESS_FILESYSTEM) #access all filesystem, not only res://
|
||||
src_fs.add_filter("*.mtxt")
|
||||
src_fs.connect("file_selected",self,"_on_src_selected")
|
||||
|
||||
add_child(src_fs)
|
||||
|
||||
dst_fs = EditorFileDialog.new()
|
||||
dst_fs.set_mode(EditorFileDialog.MODE_SAVE_FILE)
|
||||
dst_fs.add_filter("*.mtl") # Use binary extension always, text can't save metadata
|
||||
dst_fs.connect("file_selected",self,"_on_dst_selected")
|
||||
|
||||
add_child(dst_fs)
|
||||
|
||||
set_hide_on_ok(true)
|
||||
get_ok().set_text("Import!")
|
||||
|
||||
|
||||
func _on_src_browse_pressed():
|
||||
src_fs.popup_centered_ratio()
|
||||
|
||||
func _on_dst_browse_pressed():
|
||||
dst_fs.popup_centered_ratio()
|
||||
|
||||
func _on_src_selected(path):
|
||||
get_node("src_file").set_text(path)
|
||||
|
||||
func _on_dst_selected(path):
|
||||
get_node("dst_file").set_text(path)
|
||||
|
||||
func _on_MaterialImport_confirmed():
|
||||
# Create an import metadata
|
||||
var imd = ResourceImportMetadata.new()
|
||||
# Add the source files, always validate the source path
|
||||
imd.add_source( import_plugin.validate_source_path( get_node("src_file").get_text() ))
|
||||
# Add the options
|
||||
imd.set_option( "use_red_anyway", get_node("use_red_anyway").is_pressed() )
|
||||
# Perform regular import
|
||||
var err = import_plugin.import( get_node("dst_file").get_text(), imd )
|
||||
# Warn if error
|
||||
if (err!=OK):
|
||||
get_node("error").set_text("Error Importing!")
|
||||
get_node("error").popup_centered_minsize()
|
||||
|
||||
111
demos/plugins/custom_import_plugin/material_dialog.tscn
Normal file
111
demos/plugins/custom_import_plugin/material_dialog.tscn
Normal file
@@ -0,0 +1,111 @@
|
||||
[gd_scene load_steps=2 format=1]
|
||||
|
||||
[ext_resource path="res://addons/custom_import_plugin/material_dialog.gd" type="Script" id=1]
|
||||
|
||||
[node name="MaterialImport" type="ConfirmationDialog"]
|
||||
|
||||
margin/right = 276.0
|
||||
margin/bottom = 154.0
|
||||
focus/ignore_mouse = false
|
||||
focus/stop_mouse = true
|
||||
size_flags/horizontal = 2
|
||||
size_flags/vertical = 2
|
||||
popup/exclusive = false
|
||||
window/title = "Silly Material Import"
|
||||
dialog/hide_on_ok = true
|
||||
script/script = ExtResource( 1 )
|
||||
__meta__ = { "__editor_plugin_screen__":"Script" }
|
||||
|
||||
[node name="src_file" type="LineEdit" parent="."]
|
||||
|
||||
margin/left = 19.0
|
||||
margin/top = 6.0
|
||||
margin/right = 190.0
|
||||
margin/bottom = 29.0
|
||||
focus/ignore_mouse = false
|
||||
focus/stop_mouse = true
|
||||
size_flags/horizontal = 2
|
||||
size_flags/vertical = 2
|
||||
text = ""
|
||||
max_length = 0
|
||||
editable = true
|
||||
secret = false
|
||||
|
||||
[node name="src_browse" type="Button" parent="."]
|
||||
|
||||
margin/left = 195.0
|
||||
margin/top = 7.0
|
||||
margin/right = 249.0
|
||||
margin/bottom = 29.0
|
||||
focus/ignore_mouse = false
|
||||
focus/stop_mouse = true
|
||||
size_flags/horizontal = 2
|
||||
size_flags/vertical = 2
|
||||
toggle_mode = false
|
||||
text = "browse"
|
||||
flat = false
|
||||
|
||||
[node name="dst_browse" type="Button" parent="."]
|
||||
|
||||
margin/left = 195.0
|
||||
margin/top = 47.0
|
||||
margin/right = 249.0
|
||||
margin/bottom = 69.0
|
||||
focus/ignore_mouse = false
|
||||
focus/stop_mouse = true
|
||||
size_flags/horizontal = 2
|
||||
size_flags/vertical = 2
|
||||
toggle_mode = false
|
||||
text = "browse"
|
||||
flat = false
|
||||
|
||||
[node name="dst_file" type="LineEdit" parent="."]
|
||||
|
||||
margin/left = 19.0
|
||||
margin/top = 46.0
|
||||
margin/right = 190.0
|
||||
margin/bottom = 69.0
|
||||
focus/ignore_mouse = false
|
||||
focus/stop_mouse = true
|
||||
size_flags/horizontal = 2
|
||||
size_flags/vertical = 2
|
||||
text = ""
|
||||
max_length = 0
|
||||
editable = true
|
||||
secret = false
|
||||
|
||||
[node name="use_red_anyway" type="CheckBox" parent="."]
|
||||
|
||||
margin/left = 20.0
|
||||
margin/top = 84.0
|
||||
margin/right = 144.0
|
||||
margin/bottom = 106.0
|
||||
focus/ignore_mouse = false
|
||||
focus/stop_mouse = true
|
||||
size_flags/horizontal = 2
|
||||
size_flags/vertical = 2
|
||||
toggle_mode = true
|
||||
text = "Use Red Anyway"
|
||||
flat = false
|
||||
align = 0
|
||||
|
||||
[node name="error" type="AcceptDialog" parent="."]
|
||||
|
||||
visibility/visible = false
|
||||
margin/right = 40.0
|
||||
margin/bottom = 40.0
|
||||
focus/ignore_mouse = false
|
||||
focus/stop_mouse = true
|
||||
size_flags/horizontal = 2
|
||||
size_flags/vertical = 2
|
||||
popup/exclusive = false
|
||||
window/title = "Alert!"
|
||||
dialog/hide_on_ok = true
|
||||
|
||||
[connection signal="confirmed" from="." to="." method="_on_MaterialImport_confirmed"]
|
||||
|
||||
[connection signal="pressed" from="src_browse" to="." method="_on_src_browse_pressed"]
|
||||
|
||||
[connection signal="pressed" from="dst_browse" to="." method="_on_dst_browse_pressed"]
|
||||
|
||||
|
||||
22
demos/plugins/custom_import_plugin/material_import.gd
Normal file
22
demos/plugins/custom_import_plugin/material_import.gd
Normal file
@@ -0,0 +1,22 @@
|
||||
tool
|
||||
extends EditorPlugin
|
||||
|
||||
var import_plugin
|
||||
|
||||
func _enter_tree():
|
||||
|
||||
import_plugin = preload("res://addons/custom_import_plugin/import_plugin.gd").new()
|
||||
|
||||
# pass the GUI base control, so the dialog has a parent node
|
||||
import_plugin.config( get_base_control() )
|
||||
|
||||
add_import_plugin( import_plugin)
|
||||
|
||||
func _exit_tree():
|
||||
|
||||
remove_import_plugin( import_plugin )
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
14
demos/plugins/custom_import_plugin/plugin.cfg
Normal file
14
demos/plugins/custom_import_plugin/plugin.cfg
Normal file
@@ -0,0 +1,14 @@
|
||||
[plugin]
|
||||
|
||||
name="Silly Material Importer"
|
||||
description="Imports a 3D Material from an external text file"
|
||||
author="Juan Linietsky"
|
||||
version="1.0"
|
||||
script="material_import.gd"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
1
demos/plugins/custom_import_plugin/test.mtxt
Normal file
1
demos/plugins/custom_import_plugin/test.mtxt
Normal file
@@ -0,0 +1 @@
|
||||
0,0,255
|
||||
Reference in New Issue
Block a user