This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
blender:pythonorg [2018/12/08 18:44] dwheele |
— (current) | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Python Notes, Used within Blender ====== | ||
| - | |||
| - | [[addonnotes: | ||
| - | |||
| - | To view "Run Script" | ||
| - | |||
| - | http:// | ||
| - | |||
| - | When Blender is started on a Windows operating system, the Console Window is first created as a separate window on the desktop. Assuming that the right start-up conditions are met, the main Blender window should also appear and the Console Window will then be toggled off. This is unlike the 2.4x series where the Console Window would remain visible while the main Blender window was open. To display the console in current versions of Blender, go to Help » Toggle System Console. | ||
| - | |||
| - | 24/Sep/2011 18:01 | ||
| - | http:// | ||
| - | |||
| - | bpy.data | ||
| - | Access to blenders internal data | ||
| - | Type : | ||
| - | |||
| - | Ran this in the console: | ||
| - | < | ||
| - | >>> | ||
| - | < | ||
| - | </ | ||
| - | |||
| - | It's possible that the vertexes aren't available. Saw note regarding built-in mesh object. | ||
| - | |||
| - | http:// | ||
| - | 24/Sep/2011 21:05 | ||
| - | |||
| - | ==== User Scripts Path ==== | ||
| - | |||
| - | The user preferences script path provides a way to set your own directory which is used for scripts as well as the user scripts path. Be sure to create subfolders within this directory which match the structure of blenders scripts directory, startup/, addons/, modules/ etc. because copying scripts directly into this folder will not load them on startup or as addons. | ||
| - | |||
| - | |||
| - | http:// | ||
| - | |||
| - | |||
| - | bpy.context.active_object.data.uv_textures[0].data[0] | ||
| - | (can't be in edit mode) | ||
| - | |||
| - | ....I run a count through the number of vertices in the mesh. The current count number is passed to a routine as the index number of a vertex. The routine iterates through the faces of the mesh. When the vertex number is found in one of the face vertices, the appropriate uv is retrieved, and the routine exits, returning the uv. | ||
| - | ... | ||
| - | |||
| - | uvs = {} | ||
| - | for f, uv in zip(mesh.faces, | ||
| - | for j, v in enumerate(f.vertices): | ||
| - | uvs[v] = uv.uv_raw[2*j: | ||
| - | |||
| - | http:// | ||
| - | |||
| - | < | ||
| - | >>> | ||
| - | 1053 | ||
| - | |||
| - | bpy.data.meshes[" | ||
| - | doesn' | ||
| - | </ | ||
| - | |||
| - | <code python> | ||
| - | Has " | ||
| - | from: http:// | ||
| - | |||
| - | Run in text window, click "Run Script" | ||
| - | |||
| - | import bpy | ||
| - | file = open('/ | ||
| - | mesh = bpy.data.meshes[" | ||
| - | for uvTexture in mesh.uv_textures: | ||
| - | | ||
| - | | ||
| - | for i in range(uvDataCount): | ||
| - | # UV per face index, not per vertex | ||
| - | if i > 0: | ||
| - | | ||
| - | | ||
| - | for j in range(uvCount): | ||
| - | if j > 0: | ||
| - | file.write(" | ||
| - | file.write(" | ||
| - | file.write("</ | ||
| - | |||
| - | file.close() | ||
| - | </ | ||
| - | |||
| - | To run example, do a File-> | ||
| - | |||
| - | 27/Sep/2011 23:24 | ||
| - | |||
| - | ----------------------- | ||
| - | |||
| - | ==== Make a script to change one UV coordinate: ==== | ||
| - | <code python> | ||
| - | import bpy | ||
| - | mesh = bpy.data.meshes[" | ||
| - | |||
| - | mesh.uv_textures[0].data[0].uv[0][0] = mesh.uv_textures[0].data[0].uv[0][0] - .1 | ||
| - | .. moved x coordinate of first UV a little to the left. | ||
| - | </ | ||
| - | |||
| - | This works. | ||
| - | |||
| - | Need to make an algorithm to straighten the curve. | ||
| - | |||
| - | Some thoughts. | ||
| - | * form a virtual line that goes through points. Straighten those points. Mark them moved, repeat. | ||
| - | * Hard to determine the corner points, to do the line above. | ||
| - | * The UVs will be evenly spaced that are together for straightening. May be able to use this fact | ||
| - | to determine those points that are together for straightening. | ||
| - | * May need to investigate to see if there is a line between UVs that can be used to get neighbor. | ||
| - | |||
| - | -------------------------- | ||
| - | <code python> | ||
| - | import bpy | ||
| - | file = open('/ | ||
| - | mesh = bpy.data.meshes[" | ||
| - | # for one UV " | ||
| - | for uvTexture in mesh.uv_textures: | ||
| - | | ||
| - | | ||
| - | for i in range(uvDataCount): | ||
| - | # UV per face index, not per vertex | ||
| - | if i > 0: | ||
| - | | ||
| - | | ||
| - | for j in range(uvCount): | ||
| - | if j > 0: | ||
| - | file.write(" | ||
| - | file.write(" | ||
| - | file.write("</ | ||
| - | |||
| - | file.close() | ||
| - | </ | ||
| - | |||
| - | <code python> | ||
| - | # For spiralWrapTestBA.blend, | ||
| - | import bpy | ||
| - | mesh = bpy.data.meshes[" | ||
| - | len(mesh.uv_textures) | ||
| - | # prints 1 | ||
| - | len(mesh.uv_textures[0].data) | ||
| - | # prints 960 | ||
| - | # now, lets check to see | ||
| - | import bpy | ||
| - | mesh = bpy.data.meshes[" | ||
| - | print ("Mess quantity", | ||
| - | print ("data quantity in Mesh 0:", len(mesh.uv_textures[0].data)) | ||
| - | for i in range(20): | ||
| - | if i > 0: | ||
| - | print (" | ||
| - | # each shows 4 | ||
| - | </ | ||
| - | |||
| - | ---------------------- | ||
| - | |||
| - | Lets look more closely at the array content: | ||
| - | <code python> | ||
| - | import bpy | ||
| - | file = open('/ | ||
| - | mesh = bpy.data.meshes[" | ||
| - | # for one UV " | ||
| - | for uvTexture in mesh.uv_textures: | ||
| - | | ||
| - | | ||
| - | | ||
| - | for i in range(uvDataCount): | ||
| - | # UV per face index, not per vertex | ||
| - | if i >= 0: | ||
| - | file.write(" | ||
| - | uvCount = len(uvTexture.data[i].uv) | ||
| - | for j in range(uvCount): | ||
| - | if j >= 0: | ||
| - | file.write(" | ||
| - | file.write(" | ||
| - | file.write(" | ||
| - | file.write("</ | ||
| - | |||
| - | file.close() | ||
| - | |||
| - | def | ||
| - | </ | ||
| - | |||
| - | It looks like a lot of the data is repeated. Need to store muliple occurrences so that when the UVs are moved, | ||
| - | we move all of them together. | ||
| - | |||
| - | absorb uvs into a usable datastructure: | ||
| - | |||
| - | <code python> | ||
| - | import bpy | ||
| - | file = open('/ | ||
| - | mesh = bpy.data.meshes[" | ||
| - | # Creating a blank dictionary | ||
| - | d = {} | ||
| - | # for one UV " | ||
| - | for uvTexture in mesh.uv_textures: | ||
| - | | ||
| - | for i in range(uvDataCount): | ||
| - | # UV per face index, not per vertex | ||
| - | if i >= 0: | ||
| - | uvCount = len(uvTexture.data[i].uv) | ||
| - | for j in range(uvCount): | ||
| - | if j >= 0: | ||
| - | myU = uvTexture.data[i].uv[j][0] | ||
| - | myV = uvTexture.data[i].uv[j][1] | ||
| - | tuv = myU, myV | ||
| - | if not tuv in d: | ||
| - | # create new UV record | ||
| - | d[tuv] = [] | ||
| - | # add blender indices that had those UV values | ||
| - | tcoord = i, j | ||
| - | d[tuv].append(tcoord) | ||
| - | |||
| - | # Now, write to file to see if it worked | ||
| - | for k in d.keys(): | ||
| - | | ||
| - | sf = '' | ||
| - | for t3 in d[k]: | ||
| - | sf += "(%i, %i) " % (t3[0], t3[1]) | ||
| - | | ||
| - | | ||
| - | file.close() | ||
| - | </ | ||
| - | |||
| - | --------------------------------------------- | ||
| - | |||
| - | To see if this will work, read in UVs, multiply each by 1.2, and see if it works | ||
| - | <code python> | ||
| - | import bpy | ||
| - | mesh = bpy.data.meshes[" | ||
| - | # Creating a blank dictionary | ||
| - | d = {} | ||
| - | # for one UV " | ||
| - | for uvTexture in mesh.uv_textures: | ||
| - | | ||
| - | for i in range(uvDataCount): | ||
| - | # UV per face index, not per vertex | ||
| - | if i >= 0: | ||
| - | uvCount = len(uvTexture.data[i].uv) | ||
| - | for j in range(uvCount): | ||
| - | if j >= 0: | ||
| - | myU = uvTexture.data[i].uv[j][0] | ||
| - | myV = uvTexture.data[i].uv[j][1] | ||
| - | tuv = myU, myV | ||
| - | if not tuv in d: | ||
| - | # create new UV record | ||
| - | d[tuv] = [] | ||
| - | # add blender indices that had those UV values | ||
| - | tcoord = i, j | ||
| - | d[tuv].append(tcoord) | ||
| - | |||
| - | |||
| - | # at this point, each record in d looks like this: | ||
| - | # (2.5, 3.2), [(1, 5), (2, 9), (13, 19)] | ||
| - | # where there is a UV coordinate tuple as a key, and an | ||
| - | # array of tuples, representing the i, j locations in the array above | ||
| - | # where these tuplies are found | ||
| - | |||
| - | # Now, write back to initial array locations | ||
| - | for k in d.keys(): | ||
| - | for t3 in d[k]: | ||
| - | uvTexture.data[t3[0]].uv[t3[1]][0] = k[0] * 1.2 | ||
| - | uvTexture.data[t3[0]].uv[t3[1]][1] = k[1] * 1.2 | ||
| - | |||
| - | # Worked! 3/Oct/2011 21:07 | ||
| - | </ | ||
| - | --------------------------- | ||
| - | |||
| - | # Moving on, let's do some calculations | ||
| - | |||
| - | <code python> | ||
| - | def slope(tup1, tup2): | ||
| - | if (abs(tup1[0] - tup2[0]) < .001): | ||
| - | | ||
| - | return ((tup1[1] - tup2[1])/ | ||
| - | |||
| - | |||
| - | |||
| - | |||
| - | import bpy | ||
| - | mesh = bpy.data.meshes[" | ||
| - | # Creating a blank dictionary | ||
| - | d = {} | ||
| - | # for one UV " | ||
| - | for uvTexture in mesh.uv_textures: | ||
| - | | ||
| - | for i in range(uvDataCount): | ||
| - | # UV per face index, not per vertex | ||
| - | if i >= 0: | ||
| - | uvCount = len(uvTexture.data[i].uv) | ||
| - | for j in range(uvCount): | ||
| - | if j >= 0: | ||
| - | myU = uvTexture.data[i].uv[j][0] | ||
| - | myV = uvTexture.data[i].uv[j][1] | ||
| - | tuv = myU, myV | ||
| - | if not tuv in d: | ||
| - | # create new UV record | ||
| - | d[tuv] = [] | ||
| - | # add blender indices that had those UV values | ||
| - | tcoord = i, j | ||
| - | d[tuv].append(tcoord) | ||
| - | |||
| - | |||
| - | # at this point, each record in d looks like this: | ||
| - | # (2.5, 3.2), [(1, 5), (2, 9), (13, 19)] | ||
| - | # where there is a UV coordinate tuple as a key, and an | ||
| - | # array of tuples, representing the i, j locations in the array above | ||
| - | # where these tuplies are found | ||
| - | |||
| - | # Now, write back to initial array locations | ||
| - | #for k in d.keys(): | ||
| - | # for t3 in d[k]: | ||
| - | # uvTexture.data[t3[0]].uv[t3[1]][0] = k[0] * 1.2 | ||
| - | # uvTexture.data[t3[0]].uv[t3[1]][1] = k[1] * 1.2 | ||
| - | |||
| - | # Determine center point for x and y directions | ||
| - | |||
| - | minx = 0 | ||
| - | maxx = 0 | ||
| - | miny = 0 | ||
| - | maxy = 0 | ||
| - | |||
| - | for k in d.keys(): | ||
| - | if minx == 0 or k[0] < minx: | ||
| - | minx = k[0] | ||
| - | if maxx == 0 or k[0] > maxx: | ||
| - | maxx = k[0] | ||
| - | if miny == 0 or k[1] < miny: | ||
| - | miny = k[1] | ||
| - | if maxy == 0 or k[1] > maxy: | ||
| - | maxy = k[1] | ||
| - | |||
| - | centerx = (minx + maxx) / 2 | ||
| - | centery = (miny + maxy) / 2 | ||
| - | |||
| - | print (" | ||
| - | print (" | ||
| - | print (" | ||
| - | print (" | ||
| - | print (" | ||
| - | print (" | ||
| - | |||
| - | # this will hold the new locations | ||
| - | newloc = {} | ||
| - | |||
| - | # later, loop starting here | ||
| - | # find the upper-left most point that isn't in newloc | ||
| - | |||
| - | # start off with lower-right | ||
| - | upperleftUnmarked = maxx, miny | ||
| - | for k in d.keys(): | ||
| - | if k in newloc: | ||
| - | | ||
| - | if k == upperleftUnmarked: | ||
| - | | ||
| - | | ||
| - | if distSq < (upperleftUnmarked[1] - maxy) * (upperleftUnmarked[1] - maxy) + (upperleftUnmarked[0] - minx) * (upperleftUnmarked[0] - minx): | ||
| - | upperleftUnmarked = k | ||
| - | # upperleftUnmarked now has upperleft most non-newloc marked point | ||
| - | |||
| - | # Find the highest-sloped point from here | ||
| - | highslope = 0, 0 | ||
| - | for k in d.keys(): | ||
| - | if k in newloc: | ||
| - | | ||
| - | if k == upperleftUnmarked: | ||
| - | | ||
| - | if (highslope[0] == 0 and highslope[1] == 0) \ | ||
| - | or slope(k, upperleftUnmarked) > slope(highslope, | ||
| - | or (slope(k, upperleftUnmarked) == slope(highslope, | ||
| - | and (upperleftUnmarked[1] - k[1]) > (upperleftUnmarked[1] - highslope[1])): | ||
| - | | ||
| - | |||
| - | print (' | ||
| - | print (' | ||
| - | </ | ||