Bundled scripts:
-starting updates and new additions for Blender 2.34: Some of the new scripts require Python modules not builtin with Blender, so you either need a full Python install or the needed extra modules. This is an ongoing work, there should be more scripts, better testing and also proper ways to tell users they don't have all expected modules. It's expected that Win users won't need full Python installs, since we can provide a minimal zip with the needed modules from 2.34 on. Thanks to Anthony D'Agostino (scorpius), Jean-Michel Soler (jms) and Campbell Barton (Cam / ideasman) for donating the scripts now added / updated. BPython: -added two new script menu groups: Tools and Utils. We still need to find places elsewhere in the gui where the groups can be put.
This commit is contained in:
@@ -1,88 +1,93 @@
|
||||
# +---------------------------------------------------------+
|
||||
# | Copyright (c) 2001 Anthony D'Agostino |
|
||||
# | http://ourworld.compuserve.com/homepages/scorpius |
|
||||
# | scorpius@compuserve. |
|
||||
# | http://www.redrival.com/scorpius |
|
||||
# | scorpius@netzero.com |
|
||||
# | September 28, 2002 |
|
||||
# | Released under the Blender Artistic Licence (BAL) |
|
||||
# | Import Export Suite v0.5 |
|
||||
# +---------------------------------------------------------+
|
||||
# | Common Functions For All Modules |
|
||||
# | Common Functions & Global Variables For All IO Modules |
|
||||
# +---------------------------------------------------------+
|
||||
|
||||
import Blender
|
||||
import sys#, random, operator
|
||||
import mod_flags
|
||||
import sys
|
||||
|
||||
show_progress = 1 # Set to 0 for faster performance
|
||||
average_vcols = 1 # Off for per-face, On for per-vertex
|
||||
overwrite_mesh_name = 0 # Set to 0 to increment object-name version
|
||||
|
||||
blender_version = Blender.Get('version')
|
||||
blender_version_str = `blender_version`[0] + '.' + `blender_version`[1:]
|
||||
|
||||
try:
|
||||
import random, operator
|
||||
# ===================================
|
||||
# ==== Append Faces To Face List ====
|
||||
# ===================================
|
||||
def append_faces(mesh, faces, facesuv, uvcoords):
|
||||
r = random.randrange(200, 255, 50)
|
||||
g = random.randrange(100, 200, 50)
|
||||
b = random.randrange(0, 100, 50)
|
||||
for i in range(len(faces)):
|
||||
if not i%100 and mod_flags.show_progress: Blender.Window.DrawProgressBar(float(i)/len(faces), "Generating Faces")
|
||||
numfaceverts=len(faces[i])
|
||||
if numfaceverts <= 4: # This face is a triangle or quad
|
||||
import operator
|
||||
except:
|
||||
msg = "Error: you need a full Python install to run this script."
|
||||
mod_meshtools.print_boxed(msg)
|
||||
Blender.Draw.PupMenu("ERROR%t|"+msg)
|
||||
|
||||
# =================================
|
||||
# === Append Faces To Face List ===
|
||||
# =================================
|
||||
def append_faces(mesh, faces, facesuv, uvcoords):
|
||||
for i in range(len(faces)):
|
||||
if not i%100 and show_progress: Blender.Window.DrawProgressBar(float(i)/len(faces), "Generating Faces")
|
||||
numfaceverts=len(faces[i])
|
||||
if numfaceverts <= 4: # This face is a triangle or quad
|
||||
face = Blender.NMesh.Face()
|
||||
for j in range(numfaceverts):
|
||||
index = faces[i][j]
|
||||
face.v.append(mesh.verts[index])
|
||||
if len(uvcoords) > 1:
|
||||
uvidx = facesuv[i][j]
|
||||
face.uv.append(uvcoords[uvidx])
|
||||
face.mode = 0
|
||||
face.col = [Blender.NMesh.Col()]*4
|
||||
mesh.faces.append(face)
|
||||
else: # Triangulate n-sided convex polygon.
|
||||
a, b, c = 0, 1, 2 # Indices of first triangle.
|
||||
for j in range(numfaceverts-2): # Number of triangles in polygon.
|
||||
face = Blender.NMesh.Face()
|
||||
for j in range(numfaceverts):
|
||||
index = faces[i][j]
|
||||
face.v.append(mesh.verts[index])
|
||||
if len(uvcoords) > 1:
|
||||
uvidx = facesuv[i][j]
|
||||
face.uv.append(uvcoords[uvidx])
|
||||
face.mode = 0
|
||||
#face.col = [Blender.NMesh.Col(r, g, b)]*4 # Random color
|
||||
face.col = [Blender.NMesh.Col()]*4
|
||||
face.v.append(mesh.verts[faces[i][a]])
|
||||
face.v.append(mesh.verts[faces[i][b]])
|
||||
face.v.append(mesh.verts[faces[i][c]])
|
||||
b = c; c += 1
|
||||
mesh.faces.append(face)
|
||||
else: # Triangulate n-sided convex polygon.
|
||||
a, b, c = 0, 1, 2 # Indices of first triangle.
|
||||
for j in range(numfaceverts-2): # Number of triangles in polygon.
|
||||
face = Blender.NMesh.Face()
|
||||
face.v.append(mesh.verts[faces[i][a]])
|
||||
face.v.append(mesh.verts[faces[i][b]])
|
||||
face.v.append(mesh.verts[faces[i][c]])
|
||||
b = c; c += 1
|
||||
mesh.faces.append(face)
|
||||
#face.smooth = 1
|
||||
#face.smooth = 1
|
||||
|
||||
# =====================================
|
||||
# ==== Append Verts to Vertex List ====
|
||||
# =====================================
|
||||
def append_verts(mesh, verts, normals):
|
||||
#print "Number of normals:", len(normals)
|
||||
#print "Number of verts :", len(verts)
|
||||
for i in range(len(verts)):
|
||||
if not i%100 and mod_flags.show_progress: Blender.Window.DrawProgressBar(float(i)/len(verts), "Generating Verts")
|
||||
x, y, z = verts[i]
|
||||
mesh.verts.append(Blender.NMesh.Vert(x, y, z))
|
||||
if normals:
|
||||
mesh.verts[i].no[0] = normals[i][0]
|
||||
mesh.verts[i].no[1] = normals[i][1]
|
||||
mesh.verts[i].no[2] = normals[i][2]
|
||||
# ===================================
|
||||
# === Append Verts to Vertex List ===
|
||||
# ===================================
|
||||
def append_verts(mesh, verts, normals):
|
||||
#print "Number of normals:", len(normals)
|
||||
#print "Number of verts :", len(verts)
|
||||
for i in range(len(verts)):
|
||||
if not i%100 and show_progress: Blender.Window.DrawProgressBar(float(i)/len(verts), "Generating Verts")
|
||||
x, y, z = verts[i]
|
||||
mesh.verts.append(Blender.NMesh.Vert(x, y, z))
|
||||
if normals:
|
||||
mesh.verts[i].no[0] = normals[i][0]
|
||||
mesh.verts[i].no[1] = normals[i][1]
|
||||
mesh.verts[i].no[2] = normals[i][2]
|
||||
|
||||
# =============================
|
||||
# ==== Create Blender Mesh ====
|
||||
# =============================
|
||||
def create_mesh(verts, faces, objname, facesuv=[], uvcoords=[], normals=[]):
|
||||
if normals: normal_flag = 0
|
||||
else: normal_flag = 1
|
||||
mesh = Blender.NMesh.GetRaw()
|
||||
append_verts(mesh, verts, normals)
|
||||
append_faces(mesh, faces, facesuv, uvcoords)
|
||||
if not mod_flags.overwrite_mesh_name:
|
||||
objname = versioned_name(objname)
|
||||
Blender.NMesh.PutRaw(mesh, objname, normal_flag) # Name the Mesh
|
||||
Blender.Object.GetSelected()[0].name=objname # Name the Object
|
||||
Blender.Redraw()
|
||||
# ===========================
|
||||
# === Create Blender Mesh ===
|
||||
# ===========================
|
||||
def create_mesh(verts, faces, objname, facesuv=[], uvcoords=[], normals=[]):
|
||||
if normals: normal_flag = 0
|
||||
else: normal_flag = 1
|
||||
mesh = Blender.NMesh.GetRaw()
|
||||
append_verts(mesh, verts, normals)
|
||||
append_faces(mesh, faces, facesuv, uvcoords)
|
||||
if not overwrite_mesh_name:
|
||||
objname = versioned_name(objname)
|
||||
Blender.NMesh.PutRaw(mesh, objname, normal_flag) # Name the Mesh
|
||||
Blender.Object.GetSelected()[0].name=objname # Name the Object
|
||||
Blender.Redraw()
|
||||
|
||||
except ImportError: pass
|
||||
|
||||
# ================================
|
||||
# ==== Increment Name Version ====
|
||||
# ================================
|
||||
# ==============================
|
||||
# === Increment Name Version ===
|
||||
# ==============================
|
||||
def versioned_name(objname):
|
||||
existing_names = []
|
||||
for object in Blender.Object.Get():
|
||||
@@ -104,35 +109,40 @@ def versioned_name(objname):
|
||||
break
|
||||
return objname
|
||||
|
||||
# =============================
|
||||
# ==== Print Text In A Box ====
|
||||
# =============================
|
||||
# ===========================
|
||||
# === Print Text In A Box ===
|
||||
# ===========================
|
||||
def print_boxed(text):
|
||||
lines = text.splitlines()
|
||||
maxlinelen = max(map(len, lines))
|
||||
print '+-' + '-'*maxlinelen + '-+'
|
||||
for line in lines: print '| ' + line.ljust(maxlinelen) + ' |'
|
||||
print '+-' + '-'*maxlinelen + '-+'
|
||||
|
||||
if sys.platform[:3] == "win":
|
||||
print chr(218)+chr(196) + chr(196)*maxlinelen + chr(196)+chr(191)
|
||||
for line in lines:
|
||||
print chr(179) + ' ' + line.ljust(maxlinelen) + ' ' + chr(179)
|
||||
print chr(192)+chr(196) + chr(196)*maxlinelen + chr(196)+chr(217)
|
||||
else:
|
||||
print '+-' + '-'*maxlinelen + '-+'
|
||||
for line in lines: print '| ' + line.ljust(maxlinelen) + ' |'
|
||||
print '+-' + '-'*maxlinelen + '-+'
|
||||
print '\a\r', # beep when done
|
||||
|
||||
# =================================================
|
||||
# ==== Get Euler Angles From A Rotation Matrix ====
|
||||
# =================================================
|
||||
#def mat2euler(mat):
|
||||
# angle_y = -math.asin(mat[0][2])
|
||||
# c = math.cos(angle_y)
|
||||
# if math.fabs(c) > 0.005:
|
||||
# angle_x = math.atan2(mat[1][2]/c, mat[2][2]/c)
|
||||
# angle_z = math.atan2(mat[0][1]/c, mat[0][0]/c)
|
||||
# else:
|
||||
# angle_x = 0.0
|
||||
# angle_z = -math.atan2(mat[1][0], mat[1][1])
|
||||
# return (angle_x, angle_y, angle_z)
|
||||
# ===============================================
|
||||
# === Get euler angles from a rotation matrix ===
|
||||
# ===============================================
|
||||
def mat2euler(mat):
|
||||
angle_y = -math.asin(mat[0][2])
|
||||
c = math.cos(angle_y)
|
||||
if math.fabs(c) > 0.005:
|
||||
angle_x = math.atan2(mat[1][2]/c, mat[2][2]/c)
|
||||
angle_z = math.atan2(mat[0][1]/c, mat[0][0]/c)
|
||||
else:
|
||||
angle_x = 0.0
|
||||
angle_z = -math.atan2(mat[1][0], mat[1][1])
|
||||
return (angle_x, angle_y, angle_z)
|
||||
|
||||
# ============================
|
||||
# ==== Transpose A Matrix ====
|
||||
# ============================
|
||||
# ==========================
|
||||
# === Transpose A Matrix ===
|
||||
# ==========================
|
||||
def transpose(A):
|
||||
S = len(A)
|
||||
T = len(A[0])
|
||||
@@ -142,37 +152,177 @@ def transpose(A):
|
||||
B[i][j] = A[j][i]
|
||||
return B
|
||||
|
||||
#def append_ntimes(Seq, N):
|
||||
# Seq = reduce(operator.add, Seq) # Flatten once
|
||||
# if N == 1: return Seq
|
||||
# return append_ntimes(Seq, N-1)
|
||||
# =======================
|
||||
# === Apply Transform ===
|
||||
# =======================
|
||||
def apply_transform(vertex, matrix):
|
||||
x, y, z = vertex
|
||||
xloc, yloc, zloc = matrix[3][0], matrix[3][1], matrix[3][2]
|
||||
xcomponent = x*matrix[0][0] + y*matrix[1][0] + z*matrix[2][0] + xloc
|
||||
ycomponent = x*matrix[0][1] + y*matrix[1][1] + z*matrix[2][1] + yloc
|
||||
zcomponent = x*matrix[0][2] + y*matrix[1][2] + z*matrix[2][2] + zloc
|
||||
vertex = [xcomponent, ycomponent, zcomponent]
|
||||
return vertex
|
||||
|
||||
# =========================
|
||||
# === Has Vertex Colors ===
|
||||
# =========================
|
||||
def has_vertex_colors(mesh):
|
||||
# My replacement/workaround for hasVertexColours()
|
||||
# The docs say:
|
||||
# "Warning: If a mesh has both vertex colours and textured faces,
|
||||
# this function will return False. This is due to the way Blender
|
||||
# deals internally with the vertex colours array (if there are
|
||||
# textured faces, it is copied to the textured face structure and
|
||||
# the original array is freed/deleted)."
|
||||
try:
|
||||
return mesh.faces[0].col[0]
|
||||
except:
|
||||
return 0
|
||||
|
||||
# ===========================
|
||||
# === Generate Edge Table ===
|
||||
# ===========================
|
||||
def generate_edgetable(mesh):
|
||||
edge_table = {}
|
||||
numfaces = len(mesh.faces)
|
||||
|
||||
# print "mesh.has_col ", mesh.has_col
|
||||
# print "mesh.hasVertexColours()", mesh.hasVertexColours()
|
||||
# print "mesh.hasFaceUV() ", mesh.hasFaceUV()
|
||||
# print "mesh.has_uvco ", mesh.has_uvco
|
||||
for i in range(numfaces):
|
||||
if not i%100 and show_progress:
|
||||
Blender.Window.DrawProgressBar(float(i)/numfaces, "Generating Edge Table")
|
||||
if len(mesh.faces[i].v) == 4: # Process Quadrilaterals
|
||||
generate_entry_from_quad(mesh, i, edge_table)
|
||||
elif len(mesh.faces[i].v) == 3: # Process Triangles
|
||||
generate_entry_from_tri(mesh, i, edge_table)
|
||||
else: # Skip This Face
|
||||
print "Face #", i, "was skipped."
|
||||
|
||||
# # =============================
|
||||
# # ==== Create Blender Mesh ====
|
||||
# # =============================
|
||||
# def create_mesh_old(verts, faces, objname):
|
||||
# mesh = Blender.NMesh.GetRaw()
|
||||
# # === Vertex List ===
|
||||
# for i in range(len(verts)):
|
||||
# x, y, z = verts[i]
|
||||
# mesh.verts.append(Blender.NMesh.Vert(x, y ,z))
|
||||
# # === Face List ===
|
||||
# for i in range(len(faces)):
|
||||
# face = Blender.NMesh.Face()
|
||||
# for j in range(len(faces[i])):
|
||||
# index = faces[i][j]
|
||||
# face.v.append(mesh.verts[index])
|
||||
# mesh.faces.append(face)
|
||||
# # === Name the Object ===
|
||||
# Blender.NMesh.PutRaw(mesh, objname)
|
||||
# object = Blender.Object.GetSelected()
|
||||
# object[0].name=objname
|
||||
# Blender.Redraw()
|
||||
# === Sort Edge_Table Keys & Add Edge Indices ===
|
||||
i = 0
|
||||
keys = edge_table.keys()
|
||||
keys.sort()
|
||||
for key in keys:
|
||||
edge_table[key][6] = i
|
||||
i += 1
|
||||
|
||||
# === Replace Tuples With Indices ===
|
||||
for key in keys:
|
||||
for i in [2,3,4,5]:
|
||||
if edge_table.has_key(edge_table[key][i]):
|
||||
edge_table[key][i] = edge_table[edge_table[key][i]][6]
|
||||
else:
|
||||
keyrev = (edge_table[key][i][1], edge_table[key][i][0])
|
||||
edge_table[key][i] = edge_table[keyrev][6]
|
||||
|
||||
return edge_table
|
||||
|
||||
# ================================
|
||||
# === Generate Entry From Quad ===
|
||||
# ================================
|
||||
def generate_entry_from_quad(mesh, i, edge_table):
|
||||
vertex4, vertex3, vertex2, vertex1 = mesh.faces[i].v
|
||||
|
||||
if has_vertex_colors(mesh):
|
||||
vcolor4, vcolor3, vcolor2, vcolor1 = mesh.faces[i].col
|
||||
Acol = (vcolor1.r/255.0, vcolor1.g/255.0, vcolor1.b/255.0)
|
||||
Bcol = (vcolor2.r/255.0, vcolor2.g/255.0, vcolor2.b/255.0)
|
||||
Ccol = (vcolor3.r/255.0, vcolor3.g/255.0, vcolor3.b/255.0)
|
||||
Dcol = (vcolor4.r/255.0, vcolor4.g/255.0, vcolor4.b/255.0)
|
||||
|
||||
# === verts are upper case, edges are lower case ===
|
||||
A, B, C, D = vertex1.index, vertex2.index, vertex3.index, vertex4.index
|
||||
a, b, c, d = (A, B), (B, C), (C, D), (D, A)
|
||||
|
||||
if edge_table.has_key((B, A)):
|
||||
edge_table[(B, A)][1] = i
|
||||
edge_table[(B, A)][4] = d
|
||||
edge_table[(B, A)][5] = b
|
||||
if has_vertex_colors(mesh): edge_table[(B, A)][8] = Bcol
|
||||
else:
|
||||
if has_vertex_colors(mesh):
|
||||
edge_table[(A, B)] = [i, None, d, b, None, None, None, Bcol, None]
|
||||
else:
|
||||
edge_table[(A, B)] = [i, None, d, b, None, None, None]
|
||||
|
||||
if edge_table.has_key((C, B)):
|
||||
edge_table[(C, B)][1] = i
|
||||
edge_table[(C, B)][4] = a
|
||||
edge_table[(C, B)][5] = c
|
||||
if has_vertex_colors(mesh): edge_table[(C, B)][8] = Ccol
|
||||
else:
|
||||
if has_vertex_colors(mesh):
|
||||
edge_table[(B, C)] = [i, None, a, c, None, None, None, Ccol, None]
|
||||
else:
|
||||
edge_table[(B, C)] = [i, None, a, c, None, None, None]
|
||||
|
||||
if edge_table.has_key((D, C)):
|
||||
edge_table[(D, C)][1] = i
|
||||
edge_table[(D, C)][4] = b
|
||||
edge_table[(D, C)][5] = d
|
||||
if has_vertex_colors(mesh): edge_table[(D, C)][8] = Dcol
|
||||
else:
|
||||
if has_vertex_colors(mesh):
|
||||
edge_table[(C, D)] = [i, None, b, d, None, None, None, Dcol, None]
|
||||
else:
|
||||
edge_table[(C, D)] = [i, None, b, d, None, None, None]
|
||||
|
||||
if edge_table.has_key((A, D)):
|
||||
edge_table[(A, D)][1] = i
|
||||
edge_table[(A, D)][4] = c
|
||||
edge_table[(A, D)][5] = a
|
||||
if has_vertex_colors(mesh): edge_table[(A, D)][8] = Acol
|
||||
else:
|
||||
if has_vertex_colors(mesh):
|
||||
edge_table[(D, A)] = [i, None, c, a, None, None, None, Acol, None]
|
||||
else:
|
||||
edge_table[(D, A)] = [i, None, c, a, None, None, None]
|
||||
|
||||
# ====================================
|
||||
# === Generate Entry From Triangle ===
|
||||
# ====================================
|
||||
def generate_entry_from_tri(mesh, i, edge_table):
|
||||
vertex3, vertex2, vertex1 = mesh.faces[i].v
|
||||
|
||||
if has_vertex_colors(mesh):
|
||||
vcolor3, vcolor2, vcolor1, _vcolor4_ = mesh.faces[i].col
|
||||
Acol = (vcolor1.r/255.0, vcolor1.g/255.0, vcolor1.b/255.0)
|
||||
Bcol = (vcolor2.r/255.0, vcolor2.g/255.0, vcolor2.b/255.0)
|
||||
Ccol = (vcolor3.r/255.0, vcolor3.g/255.0, vcolor3.b/255.0)
|
||||
|
||||
# === verts are upper case, edges are lower case ===
|
||||
A, B, C = vertex1.index, vertex2.index, vertex3.index
|
||||
a, b, c = (A, B), (B, C), (C, A)
|
||||
|
||||
if edge_table.has_key((B, A)):
|
||||
edge_table[(B, A)][1] = i
|
||||
edge_table[(B, A)][4] = c
|
||||
edge_table[(B, A)][5] = b
|
||||
if has_vertex_colors(mesh): edge_table[(B, A)][8] = Bcol
|
||||
else:
|
||||
if has_vertex_colors(mesh):
|
||||
edge_table[(A, B)] = [i, None, c, b, None, None, None, Bcol, None]
|
||||
else:
|
||||
edge_table[(A, B)] = [i, None, c, b, None, None, None]
|
||||
|
||||
if edge_table.has_key((C, B)):
|
||||
edge_table[(C, B)][1] = i
|
||||
edge_table[(C, B)][4] = a
|
||||
edge_table[(C, B)][5] = c
|
||||
if has_vertex_colors(mesh): edge_table[(C, B)][8] = Ccol
|
||||
else:
|
||||
if has_vertex_colors(mesh):
|
||||
edge_table[(B, C)] = [i, None, a, c, None, None, None, Ccol, None]
|
||||
else:
|
||||
edge_table[(B, C)] = [i, None, a, c, None, None, None]
|
||||
|
||||
if edge_table.has_key((A, C)):
|
||||
edge_table[(A, C)][1] = i
|
||||
edge_table[(A, C)][4] = b
|
||||
edge_table[(A, C)][5] = a
|
||||
if has_vertex_colors(mesh): edge_table[(A, C)][8] = Acol
|
||||
else:
|
||||
if has_vertex_colors(mesh):
|
||||
edge_table[(C, A)] = [i, None, b, a, None, None, None, Acol, None]
|
||||
else:
|
||||
edge_table[(C, A)] = [i, None, b, a, None, None, None]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user