{"id":251,"date":"2011-03-02T02:49:08","date_gmt":"2011-03-02T02:49:08","guid":{"rendered":"http:\/\/www.funkboxing.com\/wordpress\/?p=251"},"modified":"2021-01-03T18:15:02","modified_gmt":"2021-01-03T18:15:02","slug":"diffusion-limited-aggregation-mesh-generator","status":"publish","type":"post","link":"http:\/\/funkboxing.com\/wordpress\/?p=251","title":{"rendered":"Diffusion-Limited Aggregation Mesh Generator"},"content":{"rendered":"<p>This is a simple script I wrote that generates meshes using DLA.<\/p>\n<p>I made a little demo animation where I ran the mesh as a cloth sim.<\/p>\n<p><iframe loading=\"lazy\" title=\"YouTube video player\" width=\"480\" height=\"390\" src=\"http:\/\/www.youtube.com\/embed\/KdDp1hC1pdg\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<pre class=\"brush:py\">\nprint(\"-----GO-----\")\nimport bpy\nimport random\nfrom math import sin, cos, pi, sqrt\nfrom mathutils import Vector\n\nHardRadius = 2\nRandWalkRange = .15\nStickThresh = .15\t   #distance threshold to accrete\nMaxVerts = 100\t\t#stop when this many verts have been added\nseedLOC = Vector((0.0,0.0,0.0))\n\nD3 = True\n\nmname = \"DLAtest\"\n\ndef brownian():\n\tdx = random.gauss(0, RandWalkRange)\n\tdy = random.gauss(0, RandWalkRange)\n\tdz = random.gauss(0, RandWalkRange) \n\treturn Vector((dx, dy, dz))\n\n####returns a new wanderer on sphere\ndef newWanderer():  \n\trandAngleA = random.uniform(0,2*pi)\n\trandAngleB = random.uniform(0,2*pi) \n\tx = HardRadius * sin(randAngleA) * cos(randAngleB)\n\ty = HardRadius * sin(randAngleA) * sin(randAngleB)\n\tz = HardRadius * cos(randAngleA)\n\treturn Vector((x, y, z))\n\ndef dist(A, B):\n\txd = B.x-A.x\n\tyd = B.y-A.y\n\tzd = B.z-A.z\n\treturn sqrt(xd*xd + yd*yd + zd*zd)\n\ndef wanderLoop(ob):\n\twand = newWanderer()\n\tmmesh = ob.data\n\twhile len(mmesh.vertices) < MaxVerts:\n\t\tbrown = brownian()\n\t\twand.x += brown.x\n\t\twand.y += brown.y\n\t\tif D3: wand.z += brown.z\n\t\telse: wand.z = 0\n\t\tif  abs(wand.x) > HardRadius or \n\t\t\tabs(wand.y) > HardRadius or \n\t\t\tabs(wand.z) > HardRadius:\n\t\t\t\twand = newWanderer()\n\t\t\t\t\n\t\tfor vert in range(len(mmesh.vertices)):\n\t\t\tdpt = dist(wand, mmesh.vertices[vert].co)\n\t\t\tdZ = dist(wand, seedLOC)\t\t\t\n\t\t\tif dpt < StickThresh:\n\t\t\t\tst = str(len(mmesh.vertices)+1) + \" of \" + str(MaxVerts) + \" has been assimilated\"\n\t\t\t\tprint(st, dpt)\n\t\t\t\taddPoint(ob, wand, vert)\n\t\t\t\twand = newWanderer()\n\t\t\t\tpass\n\n####Object, point to add, index of vert to connect to (None is -1)\ndef addPoint(ob, pt, conni):\n\tmmesh = ob.data\n\tmmesh.vertices.add(1)\n\tvcounti = len(mmesh.vertices)-1\n\tmmesh.vertices[vcounti].co = [pt.x, pt.y, pt.z]\n\tif conni > -1:\n\t\tmmesh.edges.add(1)\n\t\tecounti = len(mmesh.edges)-1\n\t\tmmesh.edges[ecounti].vertices = [conni, vcounti]\n\n\ndef newDLAMesh(mname):\n\tmmesh = bpy.data.meshes.new(mname)\n\tomesh = bpy.data.objects.new(mname, mmesh)\n\tbpy.context.scene.objects.link(omesh)\n\taddPoint(omesh, seedLOC, -1)\t\n\treturn omesh\n\nob = newDLAMesh(\"testDLA\")\nwanderLoop(ob)\n\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a simple script I wrote that generates meshes using DLA. I made a little demo animation where I ran the mesh as a cloth sim. print(&#8220;&#8212;&#8211;GO&#8212;&#8211;&#8220;) import bpy import random from math import sin, cos, pi, sqrt from mathutils import Vector HardRadius = 2 RandWalkRange = .15 StickThresh = .15 #distance threshold to <a href='http:\/\/funkboxing.com\/wordpress\/?p=251' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,5,47],"tags":[28],"_links":{"self":[{"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/251"}],"collection":[{"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=251"}],"version-history":[{"count":1,"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/251\/revisions"}],"predecessor-version":[{"id":2636,"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/251\/revisions\/2636"}],"wp:attachment":[{"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}