Skip to content

Commit

Permalink
Fixes for component export.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlannan committed Aug 26, 2024
1 parent a7afbc3 commit bef07f9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
Binary file modified blender/addons/defender.zip
Binary file not shown.
7 changes: 4 additions & 3 deletions blender/addons/defender/defoldsync/defoldCmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ def processDefoldProperties(obj, thisobj):
defold_item["material_texture_defold"] = item.material_texture_defold

if item.command == "Set Key/Value":
defold_item["keyval"] = { "key": item.store_key, "value": item.store_value }
val = (item.store_value).replace('"', '\\"')
defold_item["keyval"] = { "key": item.store_key, "value": val, "is_table": item.store_is_table }

if item.command == "Init Script":
defold_item["scipt_init"] = item.command_init
defold_item["scipt_init"] = item.command_init.replace('"', '\\"')

if item.command == "Update Script":
defold_item["scipt_update"] = item.command_update
defold_item["scipt_update"] = item.command_update.replace('"', '\\"')

defold_props.append(defold_item)

Expand Down
9 changes: 9 additions & 0 deletions blender/addons/defender/defoldsync/defoldObjectProps.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ class ListItem(PropertyGroup):
default="value"
)

store_is_table: BoolProperty(
name="Is Table",
description="Is the value a table construction",
default=False
)


command_init: StringProperty(
name="Script Init",
description="A single lua script line to run on init",
Expand Down Expand Up @@ -466,6 +473,8 @@ def draw(self, context):
row.prop(item, "store_key")
row = box.row()
row.prop(item, "store_value")
row = box.row()
row.prop(item, "store_is_table")

if item.command == "Init Script":
row = box.row()
Expand Down
60 changes: 49 additions & 11 deletions blender/addons/defender/defoldsync/generator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ local gofiledata = [[
components {
id: "MESH_GO_NAME"
component: "MESH_FILE_PATH"
GO_DATA_FILE_COMPONENTS
position {
x: 0.0
y: 0.0
Expand All @@ -209,6 +208,7 @@ components {
w: 1.0
}
}
GO_FILE_COMPONENTS
GO_COLLIDER_COMPONENT
GO_FILE_SCRIPT
]]
Expand All @@ -220,13 +220,21 @@ local gomodelmaterialtexture = [[
" texture: \"GO_MODEL_TEXTURE\""
]]

local gomodelcomponentdata = [[
local gomodelcomponentembedded = [[
"components {\n"
" id: \"GO_COMPONENT_ID\"\n"
" component: \"GO_COMPONENT_PATH\"\n"
"}\n"
"}\n"
]]

local gomodelcomponentfile = [[
components {
id: "GO_COMPONENT_ID"
component: "GO_COMPONENT_PATH"
}
]]


local gomodelfiledata = {
-- Initial version. Will provide a way to set this on the exporter if needed.
[[
Expand All @@ -240,7 +248,7 @@ embedded_components {
"animations: \"MODEL_ANIM_FILE\"\n"
"default_animation: \"MODEL_ANIM_NAME\"\n"
"name: \"unnamed\"\n"
GO_FILE_COMPONENTS
GO_DATA_FILE_COMPONENTS
""
position {
x: 0.0
Expand All @@ -266,7 +274,7 @@ GO_MESH_TEXTURE_FILES
MODEL_SKELETON_FILE
MODEL_ANIM_FILE
MODEL_ANIM_NAME
GO_FILE_COMPONENTS
GO_DATA_FILE_COMPONENTS
"}\n"
position {
Expand Down Expand Up @@ -404,6 +412,8 @@ local gopscript = [[
return gop_tables
end
M.tables = gop_tables
return M
]]

Expand Down Expand Up @@ -878,6 +888,8 @@ local function makescriptfile( name, filepath, objs )
local propcount = 0

scriptdata = scriptdata..'\nlocal gop = require("'..locallua..'")\n'
local initscript = ""
local updatescript = ""

for k,v in pairs(objs) do
if(v.props) then
Expand All @@ -886,15 +898,39 @@ local function makescriptfile( name, filepath, objs )
scriptdata = scriptdata..'gop.set("'..pkey..'", "'..pvalue..'")\n'
end
end
if(v.defold_props) then
for k, v in ipairs(v.defold_props) do
if(v.command == "Set Key/Value") then
propcount = propcount + 1
if(v.keyval.is_table == true) then
scriptdata = scriptdata..'gop.set("'..tostring(v.keyval.key)..'", {} )\n'
else
scriptdata = scriptdata..'gop.set("'..tostring(v.keyval.key)..'", "'..tostring(v.keyval.value)..'")\n'
end
end
if(v.command == "Init Script") then
propcount = propcount + 1
initscript = initscript.." "..tostring(v.scipt_init)..'\n'
end
if(v.command == "Update Script") then
propcount = propcount + 1
updatescript = updatescript.." "..tostring(v.scipt_init)..'\n'
end
end
end
end

scriptdata = scriptdata..'\nfunction init(self)\n'
scriptdata = scriptdata..'\tself.props = gop.getall()\n'
scriptdata = scriptdata..'\t-- Run initial setup on properties here.\n'
scriptdata = scriptdata..'\t-- pprint(self.props) -- Debug props\n'

scriptdata = scriptdata..initscript

scriptdata = scriptdata..'end\n'

scriptdata = scriptdata..'\nfunction update(self)\n'
scriptdata = scriptdata..updatescript
scriptdata = scriptdata..'end\n'

if(propcount == 0) then return "" end
Expand All @@ -920,25 +956,27 @@ end

------------------------------------------------------------------------------------------------------------
-- Helper to insert go components from blender into go file
local function getcomponents( go, godata, fileobj, nodata )
local function getcomponents( go, godata, embedded, nodata )

local compprops = getdefoldprops(go, "Add FileComponent")
local compstr = "\"\""
local compstr = ""
local prestr = ""
if(fileobj) then
if(embedded) then
prestr = "data: "
compstr = "\"\""
end

if compprops then
local compdata = gomodelcomponentdata
local compdata = gomodelcomponentfile
if(embedded) then compdata = gomodelcomponentembedded end
for k,v in ipairs(compprops) do
compdata = string.gsub(compdata, "GO_COMPONENT_ID", v.filecomponent_id)
compdata = string.gsub(compdata, "GO_COMPONENT_PATH", v.filecomponent_path)
end
compstr = compstr..compdata
end

if(fileobj) then
if(embedded) then
if(nodata) then
if(compstr == "\"\"") then compstr = "" end
godata = string.gsub(godata, "GO_DATA_FILE_COMPONENTS", compstr)
Expand Down Expand Up @@ -1038,7 +1076,7 @@ local function makegofile( name, filepath, go )
end

godata = string.gsub(godata, "GO_FILE_SCRIPT", "")
godata = getcomponents( go, godata, true, true )
godata = getcomponents( go, godata )

-- Apply animation specific changes to model data
if(animname and animfile) then
Expand Down

0 comments on commit bef07f9

Please sign in to comment.