Script request - Vector layer/group to PNG

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

panther1svk
Posts: 2
Joined: Sat Apr 18, 2015 12:03 pm

Script request - Vector layer/group to PNG

Post by panther1svk »

Hello folks,

i dont know if someone else wrote here simmilar issue, but im daily solving this in production. Imagine huge scene, with elements, which have thousands of points. After adding more and more animation and other layers, its always starting to lag as hell. So solution can be to just make from vector PNG file.

How i imagine it?
- select layer/folder in layer menu
- click script
- script will make new file with selected layer/ group name
- will copy there item and save it (if in future customer or yourself will need to change something)
- then render PNG
- insert it inside of original file instead of vector layer/group (vector layer will be deleted because of optimalization of file)
(best would be if the script can run in background, so you wont jump into new file etc., just png appear in layer menu.. if possible :) )

If someone know about any solutions or maybe already made script will be really glad :)

Cheers
Last edited by panther1svk on Mon Apr 16, 2018 7:36 am, edited 1 time in total.
User avatar
synthsin75
Posts: 9935
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Script request - Vector layer/group to PNG

Post by synthsin75 »

That sounds completely doable, but I don't know if I'd have the time, desire, or need for it.
If any of the other scripters want to give it a go (or you want to brave learning Moho Lua), I can probably provide a few useful code snippets.

Note that images may not behave like vectors do, but I assume you've already tested it in your own workflow.
panther1svk
Posts: 2
Joined: Sat Apr 18, 2015 12:03 pm

Re: Script request - Vector layer/group to PNG

Post by panther1svk »

synthsin75 wrote:That sounds completely doable, but I don't know if I'd have the time, desire, or need for it.
If any of the other scripters want to give it a go (or you want to brave learning Moho Lua), I can probably provide a few useful code snippets.

Note that images may not behave like vectors do, but I assume you've already tested it in your own workflow.
Would be really cool if someone can :) And yes, i have tested it. Mostly its usefull to use it on objects, which are not required to be animated in scene - for example elements in background as buildings or lots of times i need to make from characters sillhoutte and copy iit 100 times to make audience in background...etc etc.. :)
chucky
Posts: 4650
Joined: Sun Jan 28, 2007 4:24 am

Re: Script request - Vector layer/group to PNG

Post by chucky »

This has bee requested before, it's a good idea.

We can either hope to see it one day, or write to SM and request it from dev.

Perhaps you could do the latter?

If we wait on a hillside for a roast duck to fly into our mouths, we will be waiting a very long time...
Is that Confucius? :wink:
User avatar
hayasidist
Posts: 3492
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Script request - Vector layer/group to PNG

Post by hayasidist »

There's a couple of questions that spring to mind.
panther1svk wrote:- script will make new file with selected layer/ group name
- will copy there item and save it ...
- then render PNG
> you've said its mostly for static objects, but might there be animation (including camera movement) in this layer / group - if so that might mean render to png sequence?
> render what - just the viewport or the extent of the whole group (the vectors might extend beyond the viewport)?

I'd be interested to hear if you've considered other approaches (e.g. layer comps; using other drawing programs for static elements etc) and your rationale for not using them - there may be valuable lessons there for others.
chucky
Posts: 4650
Joined: Sun Jan 28, 2007 4:24 am

Re: Script request - Vector layer/group to PNG

Post by chucky »

Yes, you could render a comp and reimport it.
It's a bit like the freeze function in DAWs though I guess, in th past you had to render out audio tracks and reimport them, nowadays you use freeze in almost all of them and that instantly creates a rendered version of the track, it's quick to implement , you can unfreeze make changes and freeze again, freeing up you processing. Of course that doesn't delete the original it just tempoarily hides it and displayes the rendered instead.
This is actually a lot like how the gpu works in Moho. GPU sisplay could be improved in Moho and even used to speed up rendering ( perhaps, I know nothing about this stuff really)
User avatar
hayasidist
Posts: 3492
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Script request - Vector layer/group to PNG

Post by hayasidist »

that "freeze / unfreeze" is an interesting concept - ignoring a few possible issues with the "group to png" idea for the moment - rather than a second file, it should be possible to do the selective render then mark the base vector / group as "do not render" and hide it using the layer list search feature.

That, however, just won't work if what's needed is a png of a vector that's bigger than the render area - that would probably need the second file and to set it with render dimensions big enough to accommodate the whole image; and prevent moho from scaling the vector!? not sure yet the best way to do that last step (a manual copy/paste layer of a layer that's "too big" for a 1080p project is scaled up so it's still too big for a 4K.)
User avatar
Greenlaw
Posts: 9192
Joined: Mon Jun 19, 2006 5:45 pm
Location: Los Angeles
Contact:

Re: Script request - Vector layer/group to PNG

Post by Greenlaw »

I'd like to see a baking script too.

Something to keep in mind: Month's ago, I was creating a character with an intricate headgear (lots of 'boolean-ed' areas) that needed to be animated in part using a Mesh Warp. Unfortunately, the Mesh Warp was tearing up the vectors in an unpleasant way, so I had to render it as a bitmap and import it as an image layer. This version of the art deformed with the Mesh Warp correctly, but to keep up the edge and AA quality of the deforming image, I had to render it double-size.

IMO, an automated vector baking script needs to be able to allow the user to set a scale factor (at least 2X) to be useful for production work.

Also, the script shouldn't remove the original vector art layers. Maybe just disable the visibility.
chucky
Posts: 4650
Joined: Sun Jan 28, 2007 4:24 am

Re: Script request - Vector layer/group to PNG

Post by chucky »

Greenlaw wrote:I'd like to see a baking script too.

Something to keep in mind: Month's ago, I was creating a character with an intricate headgear (lots of 'boolean-ed' areas) that needed to be animated in part using a Mesh Warp. Unfortunately, the Mesh Warp was tearing up the vectors in an unpleasant way, so I had to render it as a bitmap and import it as an image layer. This version of the art deformed with the Mesh Warp correctly, but to keep up the edge and AA quality of the deforming image, I had to render it double-size.

IMO, an automated vector baking script needs to be able to allow the user to set a scale factor (at least 2X) to be useful for production work.

Also, the script shouldn't remove the original vector art layers. Maybe just disable the visibility.
Good point Dennis
User avatar
hayasidist
Posts: 3492
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Script request - Vector layer/group to PNG

Post by hayasidist »

Greenlaw wrote:I'd like to see a baking script too.
...
IMO, an automated vector baking script needs to be able to allow the user to set a scale factor (at least 2X) to be useful for production work.
Also, the script shouldn't remove the original vector art layers. Maybe just disable the visibility.
that scaling factor may quickly take us up to the Moho limit of 9000 x 9000 px especially if working at 4K ... maybe this would be the rationale for an enhancement request for a larger canvas?

What attributes of the source vector / group would "bake to png" need to preserve? I'm guessing stuff such as the visual size / position (e.g. it was 100px square - so render to 200px but import the image at half size?) but there may be more that ought to be kept (e.g. not zeroing out layer rotation / origin / ...) -- or not??
User avatar
hayasidist
Posts: 3492
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Script request - Vector layer/group to PNG

Post by hayasidist »

I've done some thinking about this and have a VERY preliminary version that works to render a frame, import as an image layer and "hide" (mark as don't render, don't show in edit view) the original vector (all done in about an hour - and I say this for reasons that will become obvious as you read the rest of this). It's not fit for public consumption yet (no error checking; no UI; no "unfreeze" ...)

There are some fundamental questions about use cases that I'd welcome input on. If you can make your choice based on "must have" rather than "nice to have" that might just keep the time needed to implement down low enough to make it feasible to do it in the near future. [There's always the "future version" that can include bells and whistles...]

I have not yet implemented "scale up". I see three options here: user has free choice on scale via UI (maybe from among presets); fix it at 2; don't implement. [There may be issues here on "fit in frame"] Easiest: don't scale; Do fixed scale in V2; do variable scale later.

Should this script check that / try to ensure that the selected (scaled up) image fits in the render area? Easiest: No

What to render? the ONE selected layer / group? the selected layers (that might be anywhere in the doc)? a layer comp? Easiest: One selected layer / group. Multiple / arbitrary in a later version.

If the selected layer is a switch then create a switch with pngs for all the child layers rendered? With the animation copied over? Easiest: save this idea for a later version.

Is it the user's responsibility to set visibility for those (selected) layers to be rendered? Or should the script set / restore visibility for all layers? Easiest: User does this. But not too hard to do for this version

At present it runs on just the one file (i.e. does not export the selected layer to a second .moho) I can see some good reasons for making a second file; but I can also see some for not. What's the opinion of those wanting this feature? Easiest: one file.

I can see a use here for creating (say) switch mouths where the base vector gets modified a few times to create multiple pngs. So: When to render? at frame 0? at the selected frame? At keyed points in the timeline. Easiest: at the selected frame - add the frame number to the render layer name if not frame 0

If the would be target png already exists? Overwrite the file without asking? Make a second file with a unique name?
And if the would be target layer already exists? do an "update media"? Make a second layer with a unique name?
Easiest: assume it's an update and replace the image.

What about "un-bake"? How should this be implemented: select the png and invoke the script? (optionally) delete the png? "re-activate" the source layer(s) for editing. Easiest: select the one source layer and if it's "locked" - unlock it. Leave the pngs alone. Later version: select the png and then unlock / select the source layers.

any thoughts???
chucky
Posts: 4650
Joined: Sun Jan 28, 2007 4:24 am

Re: Script request - Vector layer/group to PNG

Post by chucky »

Bloody excellent mate...
You know the most often I would use this might be on backgrounds with lots of layers, effects and point colouring.

I'd still want to see how the character interacts with the BG.
So layer, group is good but comp would might cover those occasions.


This is not a request by any means , just FYI :)
User avatar
hayasidist
Posts: 3492
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Script request - Vector layer/group to PNG

Post by hayasidist »

NEW: Updated April 23rd.

this is still an "alpha" version: NEW: now at vers 1.21 - same link - a few more bells and whistles

http://www.mediafire.com/file/fx8avrl92 ... akePNG.lua


copy the .lua to your custom tools folder. (no icon yet!!!)

how to use on frame 0:

select a vector layer (or a group) or an image layer click the (defaulted to "crossed out question mark"!) tool icon.

[NEW: the tool is enabled only on a group type (bone, group, particle) layer (but not a switch) or on an image or vector layer]
... it will render what is "on screen"
if an image layer is selected and was created by this tool in the active file, the source layer will be selected
the tool renders whatever is visible and creates a png image layer. The "source" (originally selected) layer is locked and made invisible.
with the source layer selected click the tool icon again. The rendered png is made invisible and the source layer is unlocked. edit to your heart's content.
click the tool icon and the original png is replaced by the new image...

Off frame 0 the tool creates a png with the frame number as part of the name - otherwise as at frame 0.

NEW: All image layers created from the selected source layer are made invisible when the layer is opened for editing.
NEW: the saved image file name now contains the source (moho) file name e.g. mohoFileName_LayerName_frame.png but the image layer name does not

apart from the above, right now the tool leaves it to you to make sure the layers you want are visible / others are not.

I have kept everything else in this version as the "easy options" in my previous post.


feedback invited / requested ....
User avatar
hayasidist
Posts: 3492
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Script request - Vector layer/group to PNG

Post by hayasidist »

http://www.mediafire.com/file/fx8avrl92 ... akePNG.lua

I think this is a pretty robust and just about functionally complete version... but give it a thrashing and let me know!

much of the wish list is implemented - including layer comps; but not yet scale up

Installation: copy the .lua to your custom directory (sorry - still no icon...)

operation.

activate with just one unlocked (=default state) vector / group selected: the PNG is rendered; the source layer is locked
activate on a locked vector / group: the PNG is made invisible and the source layer is unlocked for editing.

activate with multiple layers selected: a layer comp is created containing the selected layers; the layercomp is SET (i.e. everything else is made invisible); a PNG is created and a NOTE layer is created as a surrogate for the layer comp. The note layer is locked; the layercomp is DEACTIVATED (i.e. only the PNG and "non-render" Note layer are visible)

activate on a locked NOTE layer - the Note layer is unlocked; the layercomp is SET; and layers are available for editing.
activate on an unlocked NOTE layer (i.e. you can put the name of a pre-existing layer comp as the note text) - this is the same as multiple selected layers - i.e.the layercomp is SET; PNG is created; note layer is locked; the layercomp is DEACTIVATED

activate on a PNG: the same as activating on whatever created it.

activate with an unlocked source on any frame - you'll get a PNG for the frame on which it's activated (e.g. run on frames 0, 6, 30 - you get 3 PNGs). All the PNGs from the source are hidden when the source is open for editing.


right now a lot of the "options" that a user might want are hardwired ... e.g. path names to save the png (defaulted to the same as the .moho -- you'll be prompted to save the .moho if necessary); png file names (moho file name .. layer name .. uuid); layer comp names (from the active selected layer)...

Let me know ....
chucky
Posts: 4650
Joined: Sun Jan 28, 2007 4:24 am

Re: Script request - Vector layer/group to PNG

Post by chucky »

Wow Paul!
This is hilariously amazing.
I mean that in the best possible way... I'm giggling while trying it out.
I love the fact that if you press it again it resets and once more just rerenders. So simple to use.

I tried different resolutions , it was brilliant.


Only one thing bugged it out... an old file using the offset on 0 , that froze the file at render... no big deal.
I didn't need to use it on that , just a bizarre cooincidence that I happenned to open that file.


Really cool, my Kentish friend, very clever.
Post Reply