Getting started with scripting Synfig

Hello, I somewhat of a beginner when it comes to programming, but I’m very happy using the command line, and have some basic understanding of python and bash scripting.

I would like to learn to write python scripts to edit/replace elements within a .sif file, for example to change all instances of a colour to something different, replace multiple strings of text with others, and various other things.

Is there a tutorial or some documentation available to help me get started?
I couldn’t find anything in the manual. I have been looking into XML parsing with Python (beginner tutorials etc) but the structure of synfig files seems to make it a little more complicated.

Any suggestions?

1 Like

Hi!
You can do a lot of exciting stuff with scripting, but it requires some knowledge of Python 3, and of python xml parsing like ET and PyQt4 for GUI integration AND a lot of patience going thru the sif files and learn their layout. My advice is to start small. Make a sif scene with one shape, like a circle and try to change its color or size, etc…
Greetz!

First of all, it’s usually best to not resort to scripting when the task can be accomplished using value nodes. E.g. instead of replacing all colours, better idea is to “export” colour and connect all instances to that node. Then changing colour in one place will result in changing everywhere.

Synfig files format is changing quite often and is not consistent.
The plugin system is “offline” and is only working by tricks :wink:

Hi!
I sometimes find juggling with value nodes more time consuming and more difficult then scripting, although I use my own character maker template that is based on juggling with value nodes, but of course the color example was just a simple example and my way of saying that you have to understand the layout of sif file first before you start messing with it and you can do that by analyzing simple and small files first. I usually use self made scripts to import stuff that synfig normally does not allow me to import such as fully rigged characters with skeletons without the need that the original file has to stay open and multiple particle layers based on Genete’s particles template, just to name two. That does not necessarily mean I use an intern python script. It also works with for example with an extern perl script.
Greetz!

Thanks for all your responses!

To give you an idea of my use case, I’m adapting a synfig animation for many different websites and languages and I eventually want people to be able to make basic changes (text, colour etc) without needing my intervention, and without having to know anything about animation or synfig, and without having to install software on their own computer. That is, they can do all these things, but they don’t have to.

I would like to get into python as it seems like it will be more useful and flexible for me in the long term than bash alone, as its also useful for blender and krita, and if there’s already some kind of setup for python scripting in synfig, then great!

The current state of experimentation:
So far, I have been doing very simple experiments with bash scripts - see this folder. I am new to this so don’t judge my clumsy scripting too harshly :wink:

The script ‘honkforreform-Client’ is run by the user, and sends their translation of the words ‘HONK FOR REFORM’ to the server where hupen.sif sits, with synfig and ffmpeg installed. On the server, the ‘honkforreform-Server’ script creates a copy of hupen.sif, changes the words, renders a PNG sequence, wraps the PNGs into a video file, and deletes the PNGS. (I didn’t use the synfig ffmpeg export option because I want the possibility of having an alpha channel sometimes, and this was the only way that I could get a video+alpha out of synfig).

honkforreform-Client
then secure copies the completed video file from the server to the user’s machine.

I don’t really know what I’m doing, so any constructive suggestions would be great!

Exported Value Nodes
caryoscelus, just for clarification on your suggestion:
To adapt the video to work with (for example) an 8-colour palette, I would create 8 exported value nodes, one for each colour, and then link every object in the animation to the most appropriate of those nodes for that object.
Then for a different version of the animation, I would simply need to change the colour of Colour A, Colour B, etc, and the entire animation would be updated. Right?
That sounds like it will work a lot better and more flexibly than my current method, which is to use sed to replace existing RGB values with new values.

Colour (somewhat related)
Two things I can’t work out about colour in synfig are
a) why synfig’s hex codes are completely different to browsers, inkscape, and everything else,
and b) how should I best take a hex code and convert it to the RGB values seen within a .sif file?

Any ideas?

Yeah, that’s correct.

Hmm, you mean those on “rgb” tab? I never noticed them cause i use “hsv”. That’s weird really. Probably a bug. But then the whole colour dialogue should be rewritten. For now you can use hexcodes from hsv tab.

Needs inspecting source, i guess. I’d think it should be as simple as dividing by 0xff, but apparently not.

check this…xml parsing with python