{"id":236,"date":"2011-02-20T03:25:37","date_gmt":"2011-02-20T03:25:37","guid":{"rendered":"http:\/\/www.funkboxing.com\/wordpress\/?p=236"},"modified":"2021-01-03T18:17:40","modified_gmt":"2021-01-03T18:17:40","slug":"blender-python-driven-animation-v1","status":"publish","type":"post","link":"http:\/\/funkboxing.com\/wordpress\/?p=236","title":{"rendered":"Blender &#8211; Python Driven Animation"},"content":{"rendered":"<p>[EDIT] &#8211; Just wanted to post this here in case I forget:<\/p>\n<p>A driver can use the current frame in the expression with &#8211;\u00a0bpy.context.scene.frame_current<\/p>\n<p>So you could do something like: sin((bpy.context.scene.frame_current-10)\/5)<\/p>\n<p>BUT!!! After 2.68 expressions in drivers only work if you have Auto Run Python Scripts turned on in UserPrefs&gt;File<\/p>\n<p style=\"text-align: center;\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>B2.56 doesn&#8217;t have proper events yet &#8211; so this is a hacky way to drive scene animation. I forgot where I found this info &#8211; but it worked so I wanted to make it available.<br \/>\n1) create Empty<br \/>\n2) add single keyframe to Zrot (frame 1)<br \/>\n&#8211; should have default expanded poly generator on it<br \/>\n3) add single driver to Yloc (frame1)<br \/>\n&#8211; in Drivers&gt; choose &#8211; Scripted sxpression<br \/>\n&#8211; enter this expression<\/p>\n<pre><code>float(exec(\"\\n\".join([l.body for l in bpy.data.texts[\"RUN\"].lines]))==None)<\/code><\/pre>\n<p>4) then a script called &#8216;RUN&#8217; should execute every time the frame changes<br \/>\n&#8211; Zrot of Empty drives expression which (i think) grabs the text datablock of the script and runs it<\/p>\n<p>here&#8217;s a quick thing I did. set up the empty as above&#8230;<br \/>\nthen make a cube (named &#8216;Cube&#8217;) subdivide it a lot, then run this-<\/p>\n<pre><code> import bpy import random ob = bpy.data.objects['Cube'] verts = ob.data.vertices for v in verts: rxnum = round(random.uniform(-0.01 ,0.01), 4) rynum = round(random.uniform(-0.01 ,0.01), 4) rznum = round(random.uniform(-0.01 ,0.01), 4) v.co[0] = v.co[0] + rxnum v.co[1] = v.co[1] + rynum v.co[2] = v.co[2] + rznum scene = bpy.context.scene <\/code><\/pre>\n<p><a href=\"http:\/\/blenderartists.org\/forum\/showthread.php?t=208378&amp;p=1783382&amp;highlight=#post1783382\" target=\"_blank\" rel=\"noopener noreferrer\">BLENDER ARTIST THREAD<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[EDIT] &#8211; Just wanted to post this here in case I forget: A driver can use the current frame in the expression with &#8211;\u00a0bpy.context.scene.frame_current So you could do something like: sin((bpy.context.scene.frame_current-10)\/5) BUT!!! After 2.68 expressions in drivers only work if you have Auto Run Python Scripts turned on in UserPrefs&gt;File &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; B2.56 doesn&#8217;t have proper <a href='http:\/\/funkboxing.com\/wordpress\/?p=236' 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":[],"_links":{"self":[{"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/236"}],"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=236"}],"version-history":[{"count":6,"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/236\/revisions"}],"predecessor-version":[{"id":2641,"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/236\/revisions\/2641"}],"wp:attachment":[{"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=236"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/funkboxing.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}