+SCENE=None
+def initialize(name, scene):
+ global SCENE
+ SCENE=scene
+ progress_start(name)
+ # set object mode
+ mode_edit = Blender.Window.EditMode()
+ if mode_edit:
+ Blender.Window.EditMode(0)
+
+def finalize():
+ scene.update(SCENE)
+ progress_finish()
+ # restore edit mode
+ #if mode_edit:
+ # Blender.Window.EditMode(1)
+
+def message(msg):
+ res=Blender.Draw.PupMenu(msg + "%t|OK")
+ print(res)
+
+def enterEditMode():
+ Blender.Window.EditMode(1)
+
+def exitEditMode():
+ Blender.Window.EditMode(0)
+
+def createVector(x, y, z):
+ return Mathutils.Vector(x, y, z)
+
+
+class Writer(object):
+ '''
+ io wrapper
+ '''
+ __slots__=[
+ 'io', 'encoding',
+ ]
+ def __init__(self, path, encoding):
+ '''
+ path: file path to open
+ encoding: text encoding to write
+
+ '''
+ self.io=open(path, "wb")
+ self.encoding=encoding
+
+ def write(self, s):
+ self.io.write(s)
+
+ def flush(self):
+ self.io.flush()
+
+ def close(self):
+ self.io.close()
+
+
+class ProgressBar(object):
+ '''
+ progress bar wrapper
+ '''
+ __slots__=['base', 'start', 'progress',]
+ def __init__(self, base):
+ print("#### %s ####" % base)
+ self.base=base
+ self.start=Blender.sys.time()
+ self.set('<start>', 0)
+ Blender.Window.WaitCursor(1)
+
+ def advance(self, message, progress):
+ self.progress+=float(progress)
+ self._print(message)
+
+ def set(self, message, progress):
+ self.progress=float(progress)
+ self._print(message)
+
+ def _print(self, message):
+ print(message)
+ message="%s: %s" % (self.base, message)
+ if message.__class__ is unicode:
+ message=message.encode(FS_ENCODING)
+ Blender.Window.DrawProgressBar(self.progress, message)
+
+ def finish(self):
+ self.progress=1.0
+ message='finished in %.2f sec' % (Blender.sys.time()-self.start)
+ self.set(message, 1.0)
+ Blender.Window.WaitCursor(0)
+ Blender.Redraw()
+
+def progress_start(base):
+ global progressBar
+ progressBar=ProgressBar(base)
+
+def progress_finish():
+ global progressBar
+ progressBar.finish()
+
+def progress_print(message, progress=0.05):
+ global progressBar
+ progressBar.advance(message, progress)
+
+def progress_set(message, progress):
+ global progressBar
+ progressBar.set(message, progress)
+
+
+class scene:
+ @staticmethod
+ def update(scene):
+ scene.update(0)
+
+
+class object:
+ @staticmethod
+ def createEmpty(name):
+ global SCENE
+ empty=SCENE.objects.new("Empty")
+ empty.setName(name)
+ return empty
+
+ @staticmethod
+ def makeParent(parent, child):
+ parent.makeParent([child])
+
+ @staticmethod
+ def duplicate(o):
+ new_mesh, new_object=mesh.create(o.name.decode(INTERNAL_ENCODING))
+ # not apply modifiers
+ new_mesh.getFromObject(o.name, 1)
+ # apply matrix
+ #o.setMatrix(o.matrixWorld)
+ return new_mesh, new_object
+
+ @staticmethod
+ def delete(o):
+ global SCENE
+ SCENE.objects.unlink(o)
+
+ @staticmethod
+ def getData(o):
+ return o.getData(mesh=True)
+
+ @staticmethod
+ def select(o):
+ o.select(True)
+
+ @staticmethod
+ def activate(o):
+ global SCENE
+ o.select(True )
+ SCENE.objects.active=o
+
+ @staticmethod
+ def getActive():
+ global SCENE
+ return SCENE.objects.active
+
+ @staticmethod
+ def deselectAll():
+ for o in bpy.data.scenes.active.objects:
+ o.select(False)
+
+ @staticmethod
+ def setLayerMask(o, layers):
+ mask=0
+ for i, enable in enumerate(layers):
+ if enable!=0:
+ mask+=(1<<i)
+ o.Layers=mask
+
+ @staticmethod
+ def isVisible(o):
+ return o.restrictDisplay
+
+ @staticmethod
+ def getShapeKeys(o):
+ return o.getData(mesh=True).key.blocks
+
+ @staticmethod
+ def addShapeKey(o, name):
+ mesh=o.getData(mesh=True)
+ mesh.insertKey()
+ block=mesh.key.blocks[-1]
+ block.name=name.encode(INTERNAL_ENCODING)
+ return block
+
+ @staticmethod
+ def hasShapeKey(o):
+ return o.getData(mesh=True).key
+
+ @staticmethod
+ def pinShape(o, enable):
+ o.pinShape=enable
+
+ @staticmethod
+ def setActivateShapeKey(o, index):
+ o.activeShape=index
+
+ @staticmethod
+ def getPose(o):
+ return o.getPose()
+
+ @staticmethod
+ def addVertexGroup(o, name):
+ o.getData(mesh=True).addVertGroup(name)
+
+ @staticmethod
+ def assignVertexGroup(o, name, index, weight):
+ o.getData(mesh=True).assignVertsToGroup(name,
+ [index], weight, Blender.Mesh.AssignModes.ADD)
+
+ @staticmethod
+ def getVertexGroupNames(o):
+ return o.getData(mesh=True).getVertGroupNames()
+
+ @staticmethod
+ def getVertexGroup(o, name):
+ indices=[]
+ for index in o.getData(mesh=True).getVertsFromGroup(name):
+ indices.append(index)
+ return indices
+
+
+class modifier:
+ @staticmethod
+ def addMirror(mesh_object):
+ return mesh_object.modifiers.append(Blender.Modifier.Types.MIRROR)
+
+ @staticmethod
+ def addArmature(mesh_object, armature_object):
+ mod=mesh_object.modifiers.append(Blender.Modifier.Types.ARMATURE)
+ mod[Blender.Modifier.Settings.OBJECT] = armature_object
+ mod[Blender.Modifier.Settings.ENVELOPES] = False
+
+ @staticmethod
+ def hasType(mesh_object, type_name):
+ for mod in mesh_object.modifiers:
+ if mod.name.upper()==type_name.upper():
+ return True
+
+ @staticmethod
+ def isType(m, type_name):
+ return m.name.upper()==type_name.upper()
+
+ @staticmethod
+ def getArmatureObject(m):
+ return m[Blender.Modifier.Settings.OBJECT]
+
+
+class shapekey:
+ @staticmethod
+ def assign(b, index, pos):
+ b.data[index]=pos
+
+ @staticmethod
+ def getByIndex(b, index):
+ return b.data[index]
+
+ @staticmethod
+ def get(b):
+ return b.data
+
+
+class texture:
+ @staticmethod
+ def create(path):
+ image = Blender.Image.Load(path.encode(INTERNAL_ENCODING))
+ texture = Blender.Texture.New(path.encode(INTERNAL_ENCODING))
+ texture.type = Blender.Texture.Types.IMAGE
+ texture.image = image
+ texture.imageFlags|=Blender.Texture.ImageFlags.USEALPHA
+ return texture, image
+
+
+class material:
+ @staticmethod
+ def create(name):
+ m = Blender.Material.New(name)
+ return m
+
+ @staticmethod
+ def get(material_name):
+ return Blender.Material.Get(material_name)
+
+ @staticmethod
+ def addTexture(material, texture):
+ material.mode = material.mode | Blender.Material.Modes.TEXFACE
+ material.setTexture(0, texture, Blender.Texture.TexCo.UV)
+
+ @staticmethod
+ def hasTexture(material):
+ return len(material.getTextures())>0
+
+ @staticmethod
+ def eachTexturePath(m, dirname):
+ for texture in m.getTextures():
+ if texture and texture.tex and texture.tex.getImage():
+ image=texture.tex.getImage()
+ if not image:
+ continue
+ yield image.getFilename()
+
+
+class mesh:
+ @staticmethod
+ def create(name):
+ global SCENE
+ m=Blender.Mesh.New()
+ o=SCENE.objects.new(m, name.encode(INTERNAL_ENCODING))
+ return m, o
+
+ @staticmethod
+ def addGeometry(m, vertices, faces):
+ m.verts.extend(vertices)
+ new_faces=m.faces.extend(faces,
+ #ignoreDups=True,
+ indexList=True)
+ m.update()
+
+ @staticmethod
+ def hasUV(m):
+ return m.faceUV
+
+ @staticmethod
+ def useVertexUV(m):
+ m.vertexUV = 1
+
+ @staticmethod
+ def addUV(m):
+ m.addUVLayer('NewUV')
+
+ @staticmethod
+ def hasFaceUV(m, i, face):
+ return len(face.uv)>0
+
+ @staticmethod
+ def getFaceUV(m, i, face, count=3):
+ return face.uv
+
+ @staticmethod
+ def setFaceUV(m, i, face, uv_array, image):
+ face.uv=[Mathutils.Vector(uv[0], uv[1]) for uv in uv_array]
+ if image:
+ face.image=image
+
+ @staticmethod
+ def vertsDelete(m, remove_vertices):
+ m.verts.delete(remove_vertices)
+
+ @staticmethod
+ def setSmooth(m, smoothing):
+ m.mode |= Blender.Mesh.Modes.AUTOSMOOTH
+ m.degr=int(smoothing)
+ #m.smooth()
+
+ @staticmethod
+ def recalcNormals(mesh_object):
+ m=mesh_object.getData(mesh=True)
+ m.calcNormals()
+
+ @staticmethod
+ def flipNormals(m):
+ m.flipNormals()
+
+ @staticmethod
+ def addMaterial(m, material):
+ m.materials+=[material]
+
+
+class vertex:
+ @staticmethod
+ def setNormal(mvert, normal):
+ mvert.no=Mathutils.Vector(*normal)
+
+ @staticmethod
+ def setUv(mvert, uv):
+ mvert.uvco=uv
+
+
+class face:
+ @staticmethod
+ def getVertexCount(face):
+ return len(face.v)
+
+ @staticmethod
+ def getVertices(face):
+ return [v.index for v in face.v]
+
+ @staticmethod
+ def getIndices(face):
+ return [face.verts[0].index, face.verts[1].index, face.verts[2].index]
+
+ @staticmethod
+ def setMaterial(face, material_index):
+ face.mat=material_index
+
+ @staticmethod
+ def getMaterialIndex(face):
+ return face.mat
+
+ @staticmethod
+ def setNormal(face, normal):
+ face.no=normal
+
+ @staticmethod
+ def getNormal(face):
+ return face.no
+
+ @staticmethod
+ def setSmooth(face, isSmooth):
+ face.smooth=1 if isSmooth else 0
+
+
+class armature:
+ @staticmethod
+ def create():
+ global SCENE
+ armature = Blender.Armature.New()
+ armature_object = SCENE.objects.new(armature)
+
+ # set XRAY
+ armature_object.drawMode = (
+ armature_object.drawMode | Blender.Object.DrawModes.XRAY)
+ # armature settings
+ armature.drawType = Blender.Armature.OCTAHEDRON
+ armature.drawNames=True
+ armature.envelopes = False
+ armature.vertexGroups = True
+ armature.mirrorEdit = True
+
+ return armature, armature_object
+
+ @staticmethod
+ def makeEditable(armature_object):
+ # create armature
+ armature_object.getData().makeEditable()
+
+ @staticmethod
+ def createIkConstraint(armature_object, p_bone, effector_name, ik):
+ cSetting = Blender.Constraint.Settings
+ # IK solver
+ constraint = p_bone.constraints.append(Blender.Constraint.Type.IKSOLVER)
+ constraint[cSetting.CHAINLEN]=len(ik.children)
+ constraint[cSetting.TARGET]=armature_object
+ constraint[cSetting.USETIP]=False
+ constraint[cSetting.BONE]=effector_name
+ # not used. place folder when export.
+ constraint[cSetting.ROTWEIGHT]=ik.weight
+ constraint[cSetting.ITERATIONS]=ik.iterations * 10
+ return constraint
+
+ @staticmethod
+ def createBone(armature_object, name):
+ bone=Blender.Armature.Editbone()
+ bone.name=name.encode(INTERNAL_ENCODING)
+ armature_object.bones[name]=bone
+ return bone
+
+ @staticmethod
+ def update(armature):
+ armature.update()
+
+
+class bone:
+ @staticmethod
+ def setConnected(bone):
+ bone.options+=[Blender.Armature.CONNECTED]
+
+ @staticmethod
+ def isConnected(b):
+ return Blender.Armature.CONNECTED in b.options
+
+ @staticmethod
+ def setLayerMask(bone, layers):
+ mask=0
+ for i, enable in enumerate(layers):
+ if enable!=0:
+ mask+=(1<<i)
+ bone.layerMask=mask
+
+ @staticmethod
+ def getHeadLocal(b):
+ return b.head['ARMATURESPACE'][0:3]
+
+ @staticmethod
+ def getTailLocal(b):
+ return b.tail['ARMATURESPACE'][0:3]
+
+
+class constraint:
+ @staticmethod
+ def ikChainLen(c):
+ return c[Blender.Constraint.Settings.CHAINLEN]
+
+ @staticmethod
+ def ikTarget(c):
+ return c[Blender.Constraint.Settings.BONE]
+
+ @staticmethod
+ def ikItration(c):
+ return c[Blender.Constraint.Settings.ITERATIONS]
+
+ @staticmethod
+ def ikRotationWeight(c):
+ return c[Blender.Constraint.Settings.ROTWEIGHT]
+
+ @staticmethod
+ def isIKSolver(c):
+ return c.type==Blender.Constraint.Type.IKSOLVER
+