File size: 6,061 Bytes
2c95e43
 
 
 
 
 
ea3fcd6
 
e6081dc
ea3fcd6
e6081dc
ea3fcd6
e6081dc
ea3fcd6
e6081dc
 
ea3fcd6
e6081dc
ea3fcd6
 
e6081dc
ea3fcd6
 
e6081dc
ea3fcd6
 
e6081dc
ea3fcd6
 
e6081dc
ea3fcd6
e6081dc
ea3fcd6
e6081dc
ea3fcd6
e6081dc
ea3fcd6
 
e6081dc
ea3fcd6
 
 
 
 
2c95e43
 
842c883
 
 
 
2c95e43
 
e6081dc
 
 
2c95e43
 
 
 
 
 
e6081dc
 
 
028f0f5
0282daa
2c95e43
 
 
 
 
 
 
0282daa
2c95e43
 
 
 
 
 
 
 
028f0f5
2c95e43
 
ea3fcd6
2c95e43
 
 
 
 
 
 
 
0282daa
2c95e43
 
 
 
 
 
 
 
028f0f5
2c95e43
 
 
 
 
 
373bf3b
2c95e43
 
 
 
 
 
 
 
 
 
373bf3b
2c95e43
 
 
842c883
 
2c95e43
842c883
2c95e43
842c883
2c95e43
 
 
 
 
0282daa
2c95e43
 
 
 
 
842c883
 
2c95e43
 
 
 
842c883
2c95e43
842c883
2c95e43
 
 
 
 
 
 
 
 
 
 
 
 
 
842c883
2c95e43
028f0f5
 
2c95e43
 
 
 
 
842c883
 
2c95e43
 
 
 
 
 
 
 
 
 
 
373bf3b
2c95e43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
842c883
2c95e43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os
import sys
import math
import bpy
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("filepath", help="path to svg file")
parser.add_argument("-rx", "--rotate-x", help="rotate x axis",
                    type=float, default=0)
parser.add_argument("-ry", "--rotate-y", help="rotate y axis",
                    type=float, default=0)
parser.add_argument("-rz", "--rotate-z", help="rotate z axis",
                    type=float, default=0)
parser.add_argument("-th",
                    "--thickness", help="thickness of the icon", type=float, default=1)
parser.add_argument(
    "-d",
    "--distance", help="distance of the camera", type=float, default=1)
parser.add_argument(
    "-lx",
    "--light-x", help="x position of the light", type=float, default=0)
parser.add_argument(
    "-ly",
    "--light-y", help="y position of the light", type=float, default=0)
parser.add_argument(
    "-lz",
    "--light-z", help="z position of the light", type=float, default=0)
parser.add_argument(
    "-ls",
    "--light-strength", help="strength of the light", type=float, default=1)
parser.add_argument("-r", "--red", help="red color",
                    type=float, default=-1)
parser.add_argument("-g", "--green", help="green color",
                    type=float, default=-1)
parser.add_argument("-b", "--blue", help="blue color",
                    type=float, default=-1)
parser.add_argument(
    "-s",
    "--size", help="size of the image", type=int, default=2048)
parser.add_argument(
    "--bevel", help="bevel depth of the icon", type=float, default=1
)


def main():
    argv = sys.argv
    argv = argv[argv.index("--") + 1:] if "--" in argv else argv[1:]
    if len(argv) >= 1:
        args = parser.parse_args(argv)

        filepath = args.filepath
        rotate_x = args.rotate_x
        rotate_y = args.rotate_y
        rotate_z = args.rotate_z
        thickness = args.thickness
        distance = args.distance
        light_x = args.light_x
        light_y = args.light_y
        light_z = args.light_z
        light_strength = args.light_strength
        color_r = args.red
        color_g = args.green
        color_b = args.blue
        size = args.size
        bevel = args.bevel

        capture(
            filepath,
            rotate_x,
            rotate_y,
            rotate_z,
            thickness,
            bevel,
            distance,
            light_x,
            light_y,
            light_z,
            light_strength,
            color_r,
            color_g,
            color_b,
            size
        )
    else:
        parser.print_help()


def capture(
    filepath,
    rotate_x=0,
    rotate_y=0,
    rotate_z=0,
    thickness=1,
    bevel=1,
    distance=1,
    light_x=0,
    light_y=0,
    light_z=0,
    light_strength=1,
    color_r=-1,
    color_g=-1,
    color_b=-1,
    size=2048
):
    reset()

    bpy.ops.import_curve.svg(filepath=filepath)

    collection = bpy.data.collections[os.path.basename(filepath)]
    x, y, z, min_x, min_y, min_z = collection_dimensions(collection)

    for obj in collection.objects:
        obj.select_set(True)

    # scale up the objects
    factor = 1 / max(x, y, z)
    bpy.ops.transform.resize(value=(factor, factor, factor))

    # move the objects
    bpy.ops.transform.translate(
        value=(0, factor * (-0.5 * x - min_x), factor * (-0.5 * y - min_y)))

    # rotate the objects
    bpy.ops.transform.rotate(value=math.pi * -0.5 +
                             deg_to_rad(rotate_x), orient_axis="X")
    bpy.ops.transform.rotate(value=deg_to_rad(rotate_y), orient_axis="Y")
    bpy.ops.transform.rotate(value=math.pi * -0.5 +
                             deg_to_rad(rotate_z), orient_axis="Z")

    material = bpy.data.materials.new("Color")
    material.diffuse_color = (
        color_r if color_r != -1 else 1, color_g if color_g != -1 else 1, color_b if color_b != -1 else 1, 1)

    for obj in collection.objects:
        obj.data.extrude = thickness * 0.0005
        obj.data.bevel_depth = bevel * 0.0001
        if color_r != -1 or color_g != -1 or color_b != -1:
            obj.active_material = material

    #  add light
    bpy.ops.object.light_add(
        type="POINT", location=(light_x, light_y, light_z))
    bpy.data.objects["Point"].data.energy = light_strength * 10

    #  add camera
    bpy.ops.object.camera_add(
        location=(distance * 3, 0, 0), rotation=(math.pi*0.5, 0, math.pi*0.5))
    bpy.context.scene.camera = bpy.data.objects["Camera"]

    render(filepath.replace(".svg", ".png"), size)

    return


def log(any):
    keys = dir(any)
    for key in keys:
        attr = getattr(any, key)
        if not callable(attr):
            print("prop:", key, attr)
        else:
            print("func:", key)


def render(out=os.path.join(os.getcwd(), "out.png"), size=2048):
    bpy.context.scene.render.filepath = out
    bpy.context.scene.render.resolution_x = size
    bpy.context.scene.render.resolution_y = size
    bpy.context.scene.render.film_transparent = True
    bpy.ops.render.render(write_still=True)


def collection_dimensions(collection):
    min_x = min_y = min_z = float("inf")
    max_x = max_y = max_z = float("-inf")

    for obj in collection.objects:
        min_x = min(min_x, obj.bound_box[0][0])
        min_y = min(min_y, obj.bound_box[0][1])
        min_z = min(min_z, obj.bound_box[0][2])
        max_x = max(max_x, obj.bound_box[6][0])
        max_y = max(max_y, obj.bound_box[6][1])
        max_z = max(max_z, obj.bound_box[6][2])

    x, y, z = max_x - min_x, max_y - min_y, max_z - min_z

    return x, y, z, min_x, min_y, min_z


def deg_to_rad(deg):
    return deg * math.pi / 180


def reset():
    for objs in (
            bpy.data.objects,
            bpy.data.meshes,
            bpy.data.cameras,
    ):
        for obj in objs:
            objs.remove(obj)

    for collections in (
            bpy.data.collections,
    ):
        for collection in collections:
            collections.remove(collection)


if __name__ == "__main__":
    main()