Created
March 30, 2018 13:57
-
-
Save n1ckfg/ba73d4644db16335ec79ad844ad35d2d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#def writeSvg(strokes=None, name="test.svg", minLineWidth=3, camera=None): | |
#def writeSvg(filepath=None, minLineWidth=3, camera=None, fps=12, start=0, end=73): | |
def writeSvg(filepath=None): | |
minLineWidth=3 | |
camera=None | |
fps=None | |
start=None | |
end=None | |
#~ | |
#if not strokes: | |
#strokes = getActiveFrame().strokes | |
if not camera: | |
camera = getActiveCamera() | |
if not fps: | |
fps = getSceneFps() | |
if (start==None or end==None): | |
start, end = getStartEnd() | |
fps = float(fps) | |
duration = float(end - start) / fps | |
gp = getActiveGp() | |
#url = getFilePath() + name | |
url = filepath | |
print(url) | |
sW = getSceneResolution()[0] | |
sH = getSceneResolution()[1] | |
svg = [] | |
#~ | |
# HEADER | |
svg.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "\r"); | |
svg.append("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">" + "\r") | |
svg.append("<svg version=\"1.1\" id=\"Layer_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"" + "\r") | |
svg.append("\t" + "width=\"" + str(sW) + "px\" height=\"" + str(sH) + "px\" viewBox=\"0 0 " + str(sW) + " " + str(sH) + "\" enable-background=\"new 0 0 " + str(sW) + " " + str(sH) +"\" xml:space=\"preserve\">" + "\r") | |
#~ | |
# BODY | |
for layer in gp.layers: | |
layerInfo = layer.info.replace(" ", "_").replace(".", "_") | |
svg.append("\t" + "<g id=\"" + layerInfo + "\">" + "\r") | |
for i, frame in enumerate(layer.frames): | |
svg.append("\t\t" + "<g id=\"" + layerInfo + "_frame" + str(i) + "\">" + "\r") | |
palette = getActivePalette() | |
for stroke in frame.strokes: | |
width = stroke.line_width | |
if (width == None or width < minLineWidth): | |
width = minLineWidth | |
#cStroke = (0,0,0,1) | |
#cFill = (1,1,1,0) | |
#try: | |
color = palette.colors[stroke.colorname] | |
print("found color: " + color.name) | |
cStroke = (color.color[0], color.color[1], color.color[2], color.alpha) | |
cFill = (color.fill_color[0], color.fill_color[1], color.fill_color[2], color.fill_alpha) | |
#except: | |
#print("color error") | |
#pass | |
svg.append("\t\t\t" + svgStroke(points=stroke.points, stroke=(cStroke[0], cStroke[1], cStroke[2]), fill=(cFill[0], cFill[1], cFill[2]), strokeWidth=minLineWidth, strokeOpacity=cStroke[3], fillOpacity=cFill[3], camera=camera) + "\r") | |
#~ | |
#idTagBase = "anim_" + layerInfo + "_frame" | |
#idTag = idTagBase + str(i) | |
#prevIdTag = "0s" | |
#if (i>0): | |
#prevIdTag = idTagBase + str(i-1) + ".end" | |
#svg.append("\t\t\t" + svgAnimate(frame=frame.frame_number, fps=fps, duration=duration, idTag=idTag, prevIdTag=prevIdTag) + "\r") | |
svg.append("\t\t\t" + svgAnimate(frame=frame.frame_number, fps=fps, duration=duration) + "\r") | |
svg.append("\t\t" + "</g>" + "\r") | |
svg.append("\t" + "</g>" + "\r") | |
#~ | |
# FOOTER | |
svg.append("</svg>" + "\r") | |
#~ | |
writeTextFile(url, svg) | |
def svgAnimate(frame=0, fps=12, duration=10, startFrame=False, endFrame=False): | |
keyIn = (float(frame) / float(fps)) / float(duration) | |
keyOut = keyIn + (1.0/float(fps)) | |
#returns = "<animate id=\"" + str(idTag) + "\" attributeName=\"display\" values=\"none;inline;none;none\" keyTimes=\"0;" + str(keyIn) + ";" + str(keyOut) + ";1\" dur=\"" + str(duration) + "s\" begin=\"0s\" repeatCount=\"indefinite\"/>" | |
returns = "<animate attributeName=\"display\" repeatCount=\"indefinite\" dur=\"" + str(duration) + "s\" keyTimes=\"0;" + str(keyIn) + ";" + str(keyOut) + ";1\" values=\"none;inline;none;none\"/>" | |
return returns | |
def svgStroke(points=None, stroke=(0,0,1), fill=(1,0,0), strokeWidth=2.0, strokeOpacity=1.0, fillOpacity=1.0, camera=None, closed=False): | |
# https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path | |
returns = "<path stroke=\""+ normRgbToHex(stroke) + "\" fill=\""+ normRgbToHex(fill) + "\" stroke-width=\"" + str(strokeWidth) + "\" stroke-opacity=\"" + str(strokeOpacity) + "\" fill-opacity=\"" + str(fillOpacity) + "\" d=\"" | |
for i, point in enumerate(points): | |
co = getWorldCoords(co=point.co, camera=camera) | |
if (i == 0): | |
returns += "M" + str(co[0]) + " " + str(co[1]) + " " | |
elif (i > 0 and i < len(points)-1): | |
returns += "L" + str(co[0]) + " " + str(co[1]) + " " | |
elif (i == len(points)-1): | |
if (closed==True): | |
returns += "L" + str(co[0]) + " " + str(co[1]) + " z" | |
else: | |
returns += "L" + str(co[0]) + " " + str(co[1]) | |
returns += "\"/>" | |
return returns | |
# ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment