Image Sheet Exporter: working with modification to corona-imagesheet.lua file

Requires Texture packer ( 3.0.0b3 )

You need to edit your export Template //Program Files/TexturePacker/bin/exporters/corona-imagesheet/ corona-imagesheet.lua

You can modify this file to your own needs here is an example how i use it with two functions. “getSheet” , “getFrame”

[lua]–
– created with TexturePacker (http://www.texturepacker.com)

– {{smartUpdateKey}}
local V = {}
local sheet =
{
frames = {
{% for sprite in sprites %}
{ x={{sprite.frameRect.x}}, y={{sprite.frameRect.y}}, width={{sprite.frameRect.width}}, height={{sprite.frameRect.height}} }, – {{sprite.trimmedName}}
{% endfor %}
},

sheetContentWidth = {{texture.size.width}},
sheetContentHeight = {{texture.size.height}}
}

local frameindex =
{
{% for sprite in sprites %}
[’{{sprite.trimmedName}}’]= {{ forloop.counter }},
{% endfor %}
}
local function getSheet() return sheet end
local function getFrame() return frameindex end

V.getSheet = getSheet
V.getFrame = getFrame

return V[/lua]

Example:
[lua]local sheetInfo = require(“myExportedImageSheet”) – lua file that Texture packer published

local myImageSheet = graphics.newImageSheet( “ImageSheet.png”, sheetInfo.getSheet() ) – ImageSheet.png is the image Texture packer published

local myImage1 = display.newImage( myImageSheet , sheetInfo.getFrame()[“image_name1”] )
local myImage2 = display.newImage( myImageSheet , sheetInfo.getFrame()[“image_name2”] ) [/lua]

cheers Darkmod
lol: After looking at this it really should be “getIndex” and “getFrames” then nice thing is you can modify the export to your needs.

[import]uid: 7177 topic_id: 23739 reply_id: 323739[/import]

Nice work, thanks for sharing [import]uid: 84637 topic_id: 23739 reply_id: 95467[/import]

This looks like just what I need, but im having trouble finding where I can edit my exporters. Any idea how to do it on a Mac?
Thanks. [import]uid: 65285 topic_id: 23739 reply_id: 98688[/import]

Ah silly me, I needed to be using 3.0.0b3. All works fine now thanks Darkmod.

I sent a message to Andreas from Code’n’Web about updating Texture Packer. He will add this with the next update.
[import]uid: 65285 topic_id: 23739 reply_id: 99935[/import]

Im now using version 3.0.0b5, which does the above. Ive had some issues with sprites not aligning correctly. Could you help me out by letting me know what settings to use or not use when packing a sprite in texture packer? thx [import]uid: 118379 topic_id: 23739 reply_id: 103035[/import]

with the new build you will need too add size too sheetContent
was <<
sheetContentWidth = {{texture.width}},
sheetContentHeight = {{texture.height}}
now <<

sheetContentWidth = {{texture.size.width}},
sheetContentHeight = {{texture.size.height}}

I updated the original post as well.

[import]uid: 7177 topic_id: 23739 reply_id: 103106[/import]

Hi,

I’ve just added an official exporter to TexturePacker which is quite similar to your solution.

To use it do the following:

local sheetInfo = require("spritesheet") -- lua file that Texture packer published  
  
local myImageSheet = graphics.newImageSheet( "spritesheet.png", sheetInfo:getSheet() )  
  
local myImage1 = display.newImage( myImageSheet , sheetInfo:getFrameIndex("drink"))  
local myImage2 = display.newImage( myImageSheet , sheetInfo:getFrameIndex("hamburger"))  

I moved the functions inside the data structure and added functions to get the frame indexes.

Cheers
Andreas
[import]uid: 9611 topic_id: 23739 reply_id: 103123[/import]

Any chance on having it work with Dynamic Resolutions? I’m having problems because of the coordinates being used. See my post here:

http://developer.anscamobile.com/forum/2012/04/26/new-sprite-api-dynamic-resolution-problem-video

Any solution to this?

EDIT: I found a fix to get Dynamic Res working! In texture packer, the “Border padding” and “Shape padding” default to “2”. That’s fine for regular resolution. When you want to make @2x res, you need to change both of those to “4” instead of 2. Hope this helps someone out! [import]uid: 51654 topic_id: 23739 reply_id: 103547[/import]

@003naveen , nice one. that is what was eluding me. Makes sense. :slight_smile: [import]uid: 118379 topic_id: 23739 reply_id: 103845[/import]

I’ve modified the corona-imagesheet to use trimming that has been added to the new imagesheet API.

Just add sourceX, sourceY, sourceWidth, sourceHeight to the frames statement

frames = {  
 {% for sprite in sprites %}  
 { x={{sprite.frameRect.x}}, y={{sprite.frameRect.y}}, width={{sprite.frameRect.width}}, height={{sprite.frameRect.height}}, sourceX={{sprite.sourceRect.x}}, sourceY={{sprite.sourceRect.y}}, sourceWidth={{sprite.untrimmedSize.width}}, sourceHeight={{sprite.untrimmedSize.height}},}, -- {{sprite.trimmedName}}{% endfor %}  
 },  

You also need to turn on trimming in the exporter.xml

:slight_smile: [import]uid: 77943 topic_id: 23739 reply_id: 104645[/import]

Hi Danedwar,

where did you get information about trimming from?
I’ve searched through the whole docs and forums about this and couldn’t find anything.

Thanks,
Krystian [import]uid: 109453 topic_id: 23739 reply_id: 109015[/import]

Hi! Danedwar can you explain a bit more about enabling trimming??
I don’t have that option available in Texture Packer (currently runnning v 3.0.0b8), it appears faded out. Cropping is enabled, but i really need to trim.
Thanks! [import]uid: 105206 topic_id: 23739 reply_id: 110125[/import]

If you want to enable it in the GUI again visit the exporter.xml and set

<supportstrimming>true</supportstrimming>  

cropping can be used with every exporter - since it simply resizes the original shape but trimming requires support from the target framework.

Does anybody have information how stable this API is? [import]uid: 9611 topic_id: 23739 reply_id: 110140[/import]

Well I got trimming to work fine for the SD images, but the HD images act all wonky. The coordinate system does not scale well it seems.
Without trimming it works well. Don’t think they support dynamic scaling and trimming yet.
[import]uid: 100901 topic_id: 23739 reply_id: 110141[/import]

I am working on that. The problem is currently that trimming might leave odd sized sprites which creates an incompatible layout.

That’s not a bug - TexturePacker tries to minimize memory usage - it just does not work well with the imagesheet api right now.

For now I would suggest

  1. Disable trimming
  2. Make sure your sprites are even sized
  3. Use Padding=4 for hd
  4. Use Padding=2 for sd

That should create identical sheets.
[import]uid: 9611 topic_id: 23739 reply_id: 110143[/import]

where in my spritesheet.lua should i add the [blockcode] {% for sprite in sprites %}
{ x={{sprite.frameRect.x}}, y={{sprite.frameRect.y}}, width={{sprite.frameRect.width}}, height={{sprite.frameRect.height}} }, – {{sprite.trimmedName}}
{% endfor %} [/blockcode]
code?

i have all the frames listed… should i add the code before or after? can anyone give me an example?
[import]uid: 105206 topic_id: 23739 reply_id: 110270[/import]

Here’s mine:

local V = {}  
local sheet =  
{  
frames = {  
{% for sprite in allSprites %}  
{ x={{sprite.frameRect.x}}, y={{sprite.frameRect.y}}, width={{sprite.frameRect.width}}, height={{sprite.frameRect.height}} }, -- {{sprite.trimmedName}}  
{% endfor %}  
},  
  
sheetContentWidth = {{texture.size.width}},  
sheetContentHeight = {{texture.size.height}}  
}  
  
local frameindex =   
{  
{% for sprite in allSprites %}  
['{{sprite.fullName}}']= {{ forloop.counter }},  
{% endfor %}  
}  
  
V.spriteInfo = sheet  
V.frameInfo = frameindex  
  
return V  

works fine with aliases as well. [import]uid: 109453 topic_id: 23739 reply_id: 110274[/import]

Oh, ok thanks!! Now i understood!

EDIT: What’s the difference between that code and the one already inside texture packer?

corona-imageSheet.lua
[blockcode]


– created with TexturePacker (http://www.texturepacker.com)

– {{smartUpdateKey}}

– local sheetInfo = require(“myExportedImageSheet”) – lua file that Texture packer published

– local myImageSheet = graphics.newImageSheet( “ImageSheet.png”, sheetInfo.getSheet() ) – ImageSheet.png is the image Texture packer published

– local myImage1 = display.newImage( myImageSheet , sheetInfo.getFrameIndex(“image_name1”))
– local myImage2 = display.newImage( myImageSheet , sheetInfo.getFrameIndex(“image_name2”))

local SheetInfo = {}

SheetInfo.sheet =
{
frames = {
{% for sprite in allSprites %}
{ x={{sprite.frameRect.x}}, y={{sprite.frameRect.y}}, width={{sprite.frameRect.width}}, height={{sprite.frameRect.height}} }, – {{sprite.trimmedName}}{% endfor %}
},

sheetContentWidth = {{texture.size.width}},
sheetContentHeight = {{texture.size.height}}
}

SheetInfo.frameIndex =
{
{% for sprite in allSprites %}
["{{sprite.trimmedName}}"] = {{ forloop.counter }},{% endfor %}
}

function SheetInfo:getSheet()
return self.sheet;
end

function SheetInfo:getFrameIndex(name)
return self.frameIndex[name];
end

return SheetInfo
[/blockcode]
[import]uid: 105206 topic_id: 23739 reply_id: 110275[/import]

hmmm

well I didn’t see the updated code in texture packer to be honest :slight_smile:
First off the filename is not trimmed in my version and the data structure returned by the script suits the way I handle imagesheets better.

Apart from that I don’t think there are any differences.
[import]uid: 109453 topic_id: 23739 reply_id: 110277[/import]

Hi, Nice post,
Why don’t you use the auto SD feature?

If your images allow it making them in a vector program is really handy. I create everything at 1.0 but export it at 2.0, that way they are always dividable by two.

Great info on the image sizes. [import]uid: 100901 topic_id: 23739 reply_id: 110642[/import]