sourceextensions/blender/demos/CosyLogo.xtl

1⍝│ CosyLogo.xtl -- the CoSy logo as a Blender scene: the letters C, o, 2⍝│ S, y as round tubes, toothpaste style, fused where they touch, glossy 3⍝│ magenta on yellow, turned once round. Each letter is its center line, 4⍝│ an array of points traced from the logo (pixels, y down); X_eTaL 5⍝│ places them in meters, and Blender gives each line a round tube and 6⍝│ round ends, then melts the tubes into one mesh. Shared by the demo 7⍝│ (demos/cosy.xtl: the video) and its test (a tiny still). 8⍝│ Import: "cy:" u_se< "CosyLogo" 9 10"bp:" u̲se< "Bpy" 11 12⍝━━ The letters 13 14⍝│ the C: a big arc open to the right, top end to bottom end (pixels, x 15⍝│ right, y down) 16ˡletterC ← 10 2 r̲eshape 1045 160 950 108 820 100 705 140 652 250 665 385 745 480 880 515 1000 492 1062 440 17⍝│ the S: top curl to bottom curl 18ˡletterS ← 11 2 r̲eshape 1285 190 1250 120 1185 105 1125 140 1118 220 1180 285 1245 340 1265 430 1220 505 1150 505 1105 445 19⍝│ the y's arm: down from the left and up into the stem 20ˡyArm ← 6 2 r̲eshape 1312 235 1330 330 1370 410 1430 425 1485 360 1508 215 21⍝│ the y's stem: down from the top right, below the line and back left 22ˡyStem ← 6 2 r̲eshape 1512 215 1505 380 1475 520 1420 585 1360 560 1330 470 23 24⍝ the o's eight angles 25ʰringAngles ← (3.141592653589793 ÷ 4.0) × f̲loat (r̲ange 8) − 1 26 27⍝│ the o: a ring of eight points about (912, 355), 75 pixels out (a 28⍝│ closed line) 29⍝│ >> "cy:" u_se< "CosyLogo" 30⍝│ >> s_hape cy:letterO 31⍝│ 8 2 32ˡletterO ← 2 1 t̲ranspose (2 c̲at 8) r̲eshape (912.0 + 75.0 × c̲os ʰringAngles) c̲at 355.0 + 75.0 × s̲in ʰringAngles 33⍝│ the tubes' radius: 52 pixels, in meters 34ˡradius ← 0.004 × 52.0 35 36⍝│ cy:p_lace points: pixels (n by 2) to meters (n by 3, z up, the 37⍝│ letters upright in x z, facing -y): 250 pixels a meter, the logo's 38⍝│ middle (1100, 340) at the origin 39⍝│ >> "cy:" u_se< "CosyLogo" 40⍝│ >> cy:p_lace 2 2 r_eshape 1100 340 1350 90 41⍝│ 0.0 0.0 0.0 42⍝│ 1.0 0.0 1.0 43ˡp̲lace ← { p → 44 n ← 1 s̲elect s̲hape p 45 x ← 0.004 × (f̲loat 1 s̲elect₂ p) − 1100.0 46 z ← 0.004 × 340.0 − f̲loat 2 s̲elect₂ p 47 2 1 t̲ranspose (3 c̲at n) r̲eshape x c̲at (n r̲eshape 0.0) c̲at z 48} 49 50⍝━━ The scene 51 52⍝ numbers into the script: the points, k to a row 53ʰn̲ums ← { (v, k) z → "nums('" c̲at (f̲ormat r̲avel v) c̲at "', " c̲at (f̲ormat k) c̲at ")" } 54 55⍝ the scene's Python, fixed: tube makes a line's round tube (a curve, 56⍝ beveled round) and, when open, a sphere on each end; fuse joins them 57⍝ all, remeshes the union in voxels, smooths the seams and drops the parts 58ʰplayer ← ("parts = []\n" c̲at 59 "\n" c̲at 60 "def tube(name, pts, r, closed):\n" c̲at 61 " cu = bpy.data.curves.new(name, 'CURVE')\n" c̲at 62 " cu.dimensions = '3D'\n" c̲at 63 " cu.bevel_mode = 'ROUND'\n" c̲at 64 " cu.bevel_depth = r\n" c̲at 65 " cu.bevel_resolution = 8\n" c̲at 66 " cu.resolution_u = 24\n" c̲at 67 " cu.use_fill_caps = True\n" c̲at 68 " sp = cu.splines.new('BEZIER')\n" c̲at 69 " sp.bezier_points.add(len(pts) - 1)\n" c̲at 70 " for b, p in zip(sp.bezier_points, pts):\n" c̲at 71 " b.co = p\n" c̲at 72 " b.handle_left_type = b.handle_right_type = 'AUTO'\n" c̲at 73 " sp.use_cyclic_u = closed\n" c̲at 74 " o = bpy.data.objects.new(name, cu)\n" c̲at 75 " scene.collection.objects.link(o)\n" c̲at 76 " parts.append(o)\n" c̲at 77 " if not closed:\n" c̲at 78 " for e in (pts[0], pts[-1]):\n" c̲at 79 " bpy.ops.mesh.primitive_uv_sphere_add(radius=r, location=e, segments=48, ring_count=24)\n" c̲at 80 " parts.append(bpy.context.active_object)\n" c̲at 81 "\n" c̲at 82 "def fuse(name, voxel):\n" c̲at 83 " bpy.ops.object.select_all(action='DESELECT')\n" c̲at 84 " for o in parts:\n" c̲at 85 " o.select_set(True)\n" c̲at 86 " bpy.context.view_layer.objects.active = parts[0]\n" c̲at 87 " bpy.ops.object.convert(target='MESH')\n" c̲at 88 " bpy.ops.object.join()\n" c̲at 89 " o = bpy.context.active_object\n" c̲at 90 " o.name = o.data.name = name\n" c̲at 91 " bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS')\n" c̲at 92 " o.location = (0.0, 0.0, 0.0)\n" c̲at 93 " m = o.modifiers.new('remesh', 'REMESH')\n" c̲at 94 " m.mode = 'VOXEL'\n" c̲at 95 " m.voxel_size = voxel\n" c̲at 96 " m = o.modifiers.new('smooth', 'LAPLACIANSMOOTH')\n" c̲at 97 " m.iterations = 8\n" c̲at 98 " m.lambda_factor = 0.6\n" c̲at 99 " m.use_volume_preserve = True\n" c̲at 100 " bpy.ops.object.modifier_apply(modifier='remesh')\n" c̲at 101 " bpy.ops.object.modifier_apply(modifier='smooth')\n" c̲at 102 " bpy.ops.object.shade_smooth()\n" c̲at 103 " bpy.data.orphans_purge(do_recursive=True)\n" c̲at 104 " return o\n" c̲at 105 "\n") 106 107⍝ Python making a tube along the points (pixels), closed or not 108ʰt̲ube ← { (name, p, closed) z → "tube('" c̲at name c̲at "', " c̲at ((ˡp̲lace p, 3) ʰn̲ums 0) c̲at ", " c̲at (f̲ormat ˡradius) c̲at ", " c̲at closed c̲at ")\n" } 109 110⍝ Python: glossy plastic (a clear coat over it), the logo's magenta 111ʰplastic ← "mat = bpy.data.materials.new('magenta plastic')\nmat.use_nodes = True\np = mat.node_tree.nodes['Principled BSDF']\np.inputs['Base Color'].default_value = (0.85, 0.0, 0.85, 1.0)\np.inputs['Roughness'].default_value = 0.28\np.inputs['Coat Weight'].default_value = 0.6\np.inputs['Coat Roughness'].default_value = 0.05\nlogo.data.materials.append(mat)\n\n" 112 113⍝ Python: the logo on a pivot turned once round its upright axis in the 114⍝ frames (keyed at 0 and a full turn, linear: the video loops) 115ʰt̲urntable ← { frames → "pivot = bpy.data.objects.new('pivot', None)\nscene.collection.objects.link(pivot)\nlogo.parent = pivot\nscene.frame_start = 1\nscene.frame_end = " c̲at (f̲ormat frames) c̲at "\npivot.rotation_euler = (0.0, 0.0, 0.0)\npivot.keyframe_insert('rotation_euler', index=2, frame=1)\npivot.rotation_euler = (0.0, 0.0, 2 * math.pi)\npivot.keyframe_insert('rotation_euler', index=2, frame=" c̲at (f̲ormat frames + 1) c̲at ")\n\n" } 116 117⍝ Python: a camera in front, three soft lights (key, fill, rim), a 118⍝ yellow world 119ʰstudio ← ("cam = bpy.data.objects.new('camera', bpy.data.cameras.new('camera'))\ncam.data.lens = 50\ncam.location = (0.0, -9.5, 0.6)\ncam.rotation_euler = (math.radians(86.4), 0.0, 0.0)\nscene.collection.objects.link(cam)\nscene.camera = cam\n" c̲at 120 "for name, energy, size, at, turn in (('key', 900, 4, (-4, -5, 5), (50, 0, -35)), ('fill', 250, 6, (5, -4, -1), (95, 0, 50)), ('rim', 400, 4, (0, 5, 4), (-50, 0, 180))):\n" c̲at 121 " light = bpy.data.objects.new(name, bpy.data.lights.new(name, 'AREA'))\n" c̲at 122 " light.data.energy = energy\n" c̲at 123 " light.data.size = size\n" c̲at 124 " light.location = at\n" c̲at 125 " light.rotation_euler = tuple(math.radians(a) for a in turn)\n" c̲at 126 " scene.collection.objects.link(light)\n" c̲at 127 "world = bpy.data.worlds.new('yellow')\nworld.use_nodes = True\nworld.node_tree.nodes['Background'].inputs['Color'].default_value = (1.0, 0.92, 0.0, 1.0)\nscene.world = world\n\n") 128 129⍝ Python: Cycles on the GPU (Metal, when there is one) at that size and 130⍝ samples, denoised, colors as they are; the Metal kernels unoptimized 131⍝ (Blender 5.1.2 crashes optimizing them in the background mid-render) 132ʰc̲ycles ← { (w, h, samples) z → "scene.render.engine = 'CYCLES'\nprefs = bpy.context.preferences.addons['cycles'].preferences\nif 'METAL' in [t[0] for t in prefs.get_device_types(bpy.context)]:\n prefs.compute_device_type = 'METAL'\n prefs.kernel_optimization_level = 'OFF'\n prefs.get_devices()\n for d in prefs.devices:\n d.use = True\n scene.cycles.device = 'GPU'\nscene.cycles.samples = " c̲at (f̲ormat samples) c̲at "\nscene.cycles.use_denoising = True\nscene.view_settings.view_transform = 'Standard'\nscene.render.resolution_x = " c̲at (f̲ormat w) c̲at "\nscene.render.resolution_y = " c̲at (f̲ormat h) c̲at "\nscene.render.resolution_percentage = 100\n\n" } 133 134⍝│ (width, height, frames, voxel, samples) cy:s_cene 0: the whole 135⍝│ script Blender runs: the five lines as tubes, fused into one mesh, 136⍝│ CoSy (remeshed in voxels that size: smaller is smoother and slower), 137⍝│ the plastic, the turntable in the frames, the studio, Cycles at that 138⍝│ size and samples; then the report. The video is left to the caller 139⍝│ (bp:v_ideo or bp:s_till), added after. 140ˡs̲cene ← { (w, h, frames, voxel, samples) z → 141 ((ᵇᵖhead c̲at ʰplayer c̲at (("C", ˡletterC, "False") ʰt̲ube 0) c̲at (("o", ˡletterO, "True") ʰt̲ube 0)) c̲at 142 ((("S", ˡletterS, "False") ʰt̲ube 0) c̲at (("y arm", ˡyArm, "False") ʰt̲ube 0) c̲at (("y stem", ˡyStem, "False") ʰt̲ube 0)) c̲at 143 ("logo = fuse('CoSy', " c̲at (f̲ormat voxel) c̲at ")\n\n" c̲at ʰplastic c̲at (ʰt̲urntable frames) c̲at ʰstudio) c̲at 144 ((w, h, samples) ʰc̲ycles 0) c̲at ᵇᵖreport) 145}