OSDN Git Service

add pymeshio.
[meshio/meshio.git] / swig / blender / bl25.py
index c297838..e002437 100755 (executable)
@@ -18,6 +18,12 @@ if os.path.exists(os.path.dirname(sys.argv[0])+"/utf8"):
 else:
     INTERNAL_ENCODING=FS_ENCODING
 
+def register():
+    pass
+
+def unregister():
+    pass
+
 SCENE=None
 def initialize(name, scene):
     global SCENE
@@ -29,14 +35,17 @@ def finalize():
     progress_finish()
 
 def message(msg):
-    pass
+    print(msg)
 
 def enterEditMode():
     bpy.ops.object.mode_set(mode='EDIT', toggle=False)
 
-def exitEditMode():
+def enterObjectMode():
     bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
 
+def enterPoseMode():
+    bpy.ops.object.mode_set(mode='POSE', toggle=False)
+
 def createVector(x, y, z):
     return mathutils.Vector([x, y, z])
 
@@ -119,6 +128,11 @@ class object:
         return empty
 
     @staticmethod
+    def each():
+        for o in SCENE.objects:
+            yield o
+
+    @staticmethod
     def makeParent(parent, child):
         child.parent=parent
 
@@ -126,7 +140,7 @@ class object:
     def duplicate(o):
         global SCENE
         bpy.ops.object.select_all(action='DESELECT')
-        o.selected=True
+        o.select=True
         SCENE.objects.active=o
         bpy.ops.object.duplicate()
         dumy=SCENE.objects.active
@@ -146,12 +160,12 @@ class object:
 
     @staticmethod
     def select(o):
-        o.selected=True
+        o.select=True
 
     @staticmethod
     def activate(o):
         global SCENE
-        o.selected=True 
+        o.select=True 
         SCENE.objects.active=o
 
     @staticmethod
@@ -175,7 +189,7 @@ class object:
 
     @staticmethod
     def isVisible(o):
-        return o.restrict_view
+        return False if o.hide else True
 
     @staticmethod
     def getShapeKeys(o):
@@ -224,6 +238,22 @@ class object:
         o.add_vertex_to_group(index, 
                     o.vertex_groups[name], weight, 'ADD')
 
+    @staticmethod
+    def createBoneGroup(o, name, color_set='DEFAULT'):
+        # create group
+        object.activate(o)
+        enterPoseMode()
+        bpy.ops.pose.group_add()
+        # set name
+        pose=object.getPose(o)
+        g=pose.active_bone_group
+        g.name=name
+        g.color_set=color_set
+
+    @staticmethod
+    def boneGroups(o):
+        return object.getPose(o).bone_groups
+
 
 class modifier:
     @staticmethod
@@ -272,13 +302,23 @@ class texture:
         texture=bpy.data.textures.new(os.path.basename(path))
         texture.type='IMAGE'
         texture=texture.recast_type()
-        image=bpy.data.images.load(path)
-        texture.image=image
         texture.mipmap=True
         texture.interpolation=True
         texture.use_alpha=True
+        try:
+            image=bpy.data.images.load(path)
+        except SystemError:
+            image=bpy.data.images.new('Image')
+        texture.image=image
         return texture, image
 
+    @staticmethod
+    def getPath(t):
+        if  t.type=="IMAGE":
+            image=t.image
+            if image:
+                return image.filepath
+
 
 class material:
     @staticmethod
@@ -290,15 +330,37 @@ class material:
         return bpy.data.materials[material_name]
 
     @staticmethod
-    def addTexture(material, texture):
-        material.add_texture(texture, "UV", "COLOR")
-        slot=material.texture_slots[material.active_texture_index]
-        slot.blend_type='MULTIPLY'
-        slot.map_alpha=True
+    def addTexture(material, texture, enable=True):
+        # search free slot
+        index=None
+        for i, slot in enumerate(material.texture_slots):
+            if not slot:
+                index=i
+                break
+        if index==None:
+            return
+
+        if enable:
+            material.add_texture(texture, "UV", "COLOR")
+            slot=material.texture_slots[index]
+            slot.blend_type='MULTIPLY'
+            slot.map_alpha=True
+        else:
+            material.add_texture(texture)
+            material.use_textures[index]=False
+        return index
+
+    @staticmethod
+    def getTexture(m, index):
+        return m.texture_slots[index].texture
 
     @staticmethod
-    def hasTexture(material):
-        return material.texture_slots[0]
+    def hasTexture(m):
+        return m.texture_slots[0]
+
+    @staticmethod
+    def setUseTexture(m, index, enable):
+        m.use_textures[index]=enable
 
     @staticmethod
     def eachTexturePath(m):
@@ -309,7 +371,19 @@ class material:
                     image=texture.image
                     if not image:
                         continue
-                    yield image.filename
+                    yield image.filepath
+
+    @staticmethod
+    def eachEnalbeTexturePath(m):
+        for i, slot in enumerate(m.texture_slots):
+            if m.use_textures[i] and slot and slot.texture:
+                texture=slot.texture
+                if  texture.type=="IMAGE":
+                    image=texture.image
+                    if not image:
+                        continue
+                    yield image.filepath
+
 
 class mesh:
     @staticmethod
@@ -363,44 +437,47 @@ class mesh:
 
     @staticmethod
     def hasFaceUV(mesh, i, face):
-        return mesh.active_uv_texture.data[i]
+        return mesh.active_uv_texture and mesh.active_uv_texture.data[i]
 
     @staticmethod
     def getFaceUV(mesh, i, faces, count=3):
-        uvFace=mesh.active_uv_texture.data[i]
-        if count==3:
-            return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
-        elif count==4:
-            return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
+        if mesh.active_uv_texture and mesh.active_uv_texture.data[i]:
+            uvFace=mesh.active_uv_texture.data[i]
+            if count==3:
+                return (uvFace.uv1, uvFace.uv2, uvFace.uv3)
+            elif count==4:
+                return (uvFace.uv1, uvFace.uv2, uvFace.uv3, uvFace.uv4)
+            else:
+                print(count)
+                assert(False)
         else:
-            print(count)
-            assert(False)
+            return ((0, 0), (0, 0), (0, 0), (0, 0))
 
     @staticmethod
-    def setFaceUV(mesh, i, face, uv_array, image):
-        uv_face=mesh.uv_textures[0].data[i]
+    def setFaceUV(m, i, face, uv_array, image):
+        uv_face=m.uv_textures[0].data[i]
         uv_face.uv=uv_array
         if image:
             uv_face.image=image
             uv_face.tex=True
 
     @staticmethod
-    def vertsDelete(mesh, remove_vertices):
+    def vertsDelete(m, remove_vertices):
         enterEditMode()
         bpy.ops.mesh.select_all(action='DESELECT')
-        exitEditMode()
+        enterObjectMode()
 
         for i in remove_vertices:
-            mesh.verts[i].selected=True
+            m.verts[i].select=True
 
         enterEditMode()
         bpy.ops.mesh.delete(type='VERT')
-        exitEditMode()
+        enterObjectMode()
 
     @staticmethod
-    def setSmooth(mesh, smoothing):
-        mesh.autosmooth_angle=int(smoothing)
-        mesh.autosmooth=True
+    def setSmooth(m, smoothing):
+        m.autosmooth_angle=int(smoothing)
+        m.autosmooth=True
 
     @staticmethod
     def recalcNormals(mesh_object):
@@ -408,24 +485,33 @@ class mesh:
         object.activate(mesh_object)
         enterEditMode()
         bpy.ops.mesh.normals_make_consistent()
-        exitEditMode()
+        enterObjectMode()
 
     @staticmethod
-    def flipNormals(mesh):
-        mesh.flipNormals()
+    def flipNormals(m):
+        m.flipNormals()
 
     @staticmethod
-    def addMaterial(mesh, material):
-        mesh.add_material(material)
+    def addMaterial(m, material):
+        m.add_material(material)
+
+    @staticmethod
+    def getMaterial(m, index):
+        return m.materials[index]
 
 
 class vertex:
     @staticmethod
-    def setNormal(mvert, normal):
-        mvert.normal=mathutils.Vector(normal)
+    def setNormal(v, normal):
+        v.normal=mathutils.Vector(normal)
+
+    @staticmethod
+    def getNormal(v):
+        return v.normal
 
     @staticmethod
-    def setUv(mvert, uv):
+    def setUv(v, uv):
+        # sticky ?
         pass
 
 
@@ -478,7 +564,8 @@ class armature:
 
         armature_object.x_ray=True
         armature.draw_names=True
-        armature.drawtype='OCTAHEDRAL'
+        #armature.drawtype='OCTAHEDRAL'
+        armature.drawtype='STICK'
         armature.deform_envelope=False
         armature.deform_vertexgroups=True
         armature.x_axis_mirror=True
@@ -563,4 +650,3 @@ class constraint:
     def isIKSolver(c):
         return c.type=='IK'
 
-