[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [43232] trunk/blender/source/tests/ bl_mesh_modifiers.py: WIP script to check results of different modifier combinations and display modes , useful for validating bmesh changes don' t break specific modifier combinations.

Campbell Barton ideasman42 at gmail.com
Mon Jan 9 11:30:42 CET 2012


Revision: 43232
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=43232
Author:   campbellbarton
Date:     2012-01-09 10:30:27 +0000 (Mon, 09 Jan 2012)
Log Message:
-----------
WIP script to check results of different modifier combinations and display modes, useful for validating bmesh changes don't break specific modifier combinations.

Added Paths:
-----------
    trunk/blender/source/tests/bl_mesh_modifiers.py

Added: trunk/blender/source/tests/bl_mesh_modifiers.py
===================================================================
--- trunk/blender/source/tests/bl_mesh_modifiers.py	                        (rev 0)
+++ trunk/blender/source/tests/bl_mesh_modifiers.py	2012-01-09 10:30:27 UTC (rev 43232)
@@ -0,0 +1,698 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+#  This program is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License
+#  as published by the Free Software Foundation; either version 2
+#  of the License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8 compliant>
+
+# Currently this script only generates images from different modifier
+# combinations and does not validate they work correctly,
+# this is because we dont get 1:1 match with bmesh.
+#
+# Later, we may have a way to check the results are valid.
+
+
+# ./blender.bin --factory-startup --python bl_mesh_modifiers.py
+
+
+# -----------------------------------------------------------------------------
+# utility funcs
+
+def render_gl(context, filepath, shade):
+
+    def ctx_viewport_shade(context, shade):
+        for area in context.window.screen.areas:
+            if area.type == 'VIEW_3D':
+                space = area.spaces.active
+                # rv3d = space.region_3d
+                space.viewport_shade = shade
+
+    import bpy
+    scene = context.scene
+    render = scene.render
+    render.filepath = filepath
+    render.image_settings.file_format = 'PNG'
+    render.use_file_extension = True
+    render.use_antialiasing = False
+
+    # render size
+    render.resolution_percentage = 100
+    render.resolution_x = 512
+    render.resolution_y = 512
+
+    ctx_viewport_shade(context, shade)
+
+    bpy.ops.render.opengl(write_still=True,
+                          view_context=True)
+
+
+    # stop to inspect!
+    # if filepath == "test_cube_shell_solidify_subsurf_ob_textured":
+    #     assert(0)
+
+
+def render_gl_all_modes(context, obj, filepath=""):
+
+    assert(obj != None)
+    assert(filepath != "")
+
+    scene = context.scene
+
+    # avoid drawing outline/center dot
+    bpy.ops.object.select_all(action='DESELECT')
+    scene.objects.active = None
+
+    # editmode
+    scene.tool_settings.mesh_select_mode = False, True, False
+
+    # render
+    render_gl(context, filepath + "_ob_wire", shade='WIREFRAME')
+    render_gl(context, filepath + "_ob_solid", shade='SOLID')
+    render_gl(context, filepath + "_ob_textured", shade='TEXTURED')
+
+    # -------------------------------------------------------------------------
+    # not just draw modes, but object modes!
+    scene.objects.active = obj
+
+    bpy.ops.object.mode_set(mode='EDIT', toggle=False)
+    bpy.ops.mesh.select_all(action='DESELECT')
+    render_gl(context, filepath + "_edit_wire", shade='WIREFRAME')
+    render_gl(context, filepath + "_edit_solid", shade='SOLID')
+    render_gl(context, filepath + "_edit_textured", shade='TEXTURED')
+    bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
+
+    bpy.ops.object.mode_set(mode='WEIGHT_PAINT', toggle=False)
+
+    render_gl(context, filepath + "_wp_wire", shade='WIREFRAME')
+
+    assert(1)
+
+    bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
+
+    scene.objects.active = None
+
+
+def ctx_clear_scene():  # copied from batch_import.py
+    import bpy
+    unique_obs = set()
+    for scene in bpy.data.scenes:
+        for obj in scene.objects[:]:
+            scene.objects.unlink(obj)
+            unique_obs.add(obj)
+
+    # remove obdata, for now only worry about the startup scene
+    for bpy_data_iter in (bpy.data.objects,
+                          bpy.data.meshes,
+                          bpy.data.lamps,
+                          bpy.data.cameras,
+                          ):
+
+        for id_data in bpy_data_iter:
+            bpy_data_iter.remove(id_data)
+
+
+def ctx_viewport_camera(context):
+    # because gl render without view_context has no shading option.
+    for area in context.window.screen.areas:
+        if area.type == 'VIEW_3D':
+            space = area.spaces.active
+            space.region_3d.view_perspective = 'CAMERA'
+
+
+def ctx_camera_setup(context,
+                     location=(0.0, 0.0, 0.0),
+                     lookat=(0.0, 0.0, 0.0),
+                     # most likely the followuing vars can be left as defaults
+                     up=(0.0, 0.0, 1.0),
+                     lookat_axis='-Z',
+                     up_axis='Y',
+                     ):
+
+    camera = bpy.data.cameras.new(whoami())
+    obj = bpy.data.objects.new(whoami(), camera)
+
+    scene = context.scene
+    scene.objects.link(obj)
+    scene.camera = obj
+
+    from mathutils import Vector, Matrix
+
+    # setup transform
+    view_vec = Vector(lookat) - Vector(location)
+    rot_mat = view_vec.to_track_quat(lookat_axis, up_axis).to_matrix().to_4x4()
+    tra_mat = Matrix.Translation(location)
+
+    obj.matrix_world = tra_mat * rot_mat
+
+    ctx_viewport_camera(context)
+
+    return obj
+
+
+# -----------------------------------------------------------------------------
+# inspect functions
+
+import inspect
+
+
+# functions
+
+def whoami():
+    return inspect.stack()[1][3]
+
+
+def whosdaddy():
+    return inspect.stack()[2][3]
+
+
+# -----------------------------------------------------------------------------
+# models (defaults)
+
+def defaults_object(obj):
+    obj.show_wire = True
+
+    if obj.type == 'MESH':
+        mesh = obj.data
+        mesh.show_all_edges = True
+
+        mesh.show_normal_vertex = True
+
+        # lame!
+        for face in mesh.faces:
+            face.use_smooth = True
+
+
+# -----------------------------------------------------------------------------
+# models (utils)
+
+def mesh_bounds(mesh):
+    xmin = ymin = zmin = +100000000.0
+    xmax = ymax = zmax = -100000000.0
+
+    for v in mesh.vertices:
+        x, y, z = v.co
+        xmax = max(x, xmax)
+        ymax = max(y, ymax)
+        zmax = max(z, zmax)
+
+        xmin = min(x, xmin)
+        ymin = min(y, ymin)
+        zmin = min(z, zmin)
+
+    return (xmin, ymin, zmin), (xmax, ymax, zmax)
+
+
+def mesh_uv_add(obj):
+    uv_lay = obj.data.uv_textures.new()
+    for uv in uv_lay.data:
+        uv.uv1 = 0.0, 0.0
+        uv.uv2 = 0.0, 1.0
+        uv.uv3 = 1.0, 1.0
+        uv.uv4 = 1.0, 0.0
+
+    return uv_lay
+
+
+def mesh_vcol_add(obj, mode=0):
+    vcol_lay = obj.data.vertex_colors.new()
+    for col in vcol_lay.data:
+        col.color1 = 1.0, 0.0, 0.0
+        col.color2 = 0.0, 1.0, 0.0
+        col.color3 = 0.0, 0.0, 1.0
+        col.color4 = 0.0, 0.0, 0.0
+
+    return vcol_lay
+
+
+def mesh_vgroup_add(obj, name="Group", axis=0, invert=False, mode=0):
+    mesh = obj.data
+    vgroup = obj.vertex_groups.new(name=name)
+    vgroup.add(list(range(len(mesh.vertices))), 1.0, 'REPLACE')
+    group_index = len(obj.vertex_groups) - 1
+
+    min_bb, max_bb = mesh_bounds(mesh)
+
+    range_axis = max_bb[axis] - min_bb[axis]
+
+    # gradient
+    for v in mesh.vertices:
+        for vg in v.groups:
+            if vg.group == group_index:
+                f = (v.co[axis] - min_bb[axis]) / range_axis
+                vg.weight = 1.0 - f if invert else f
+
+    return vgroup
+
+
+def mesh_shape_add(obj, mode=0):
+    pass
+
+
+def mesh_armature_add(obj, mode=0):
+    pass
+
+
+# -----------------------------------------------------------------------------
+# modifiers
+
+def modifier_subsurf_add(obj, levels=2):
+    mod = obj.modifiers.new(name=whoami(), type='SUBSURF')
+    mod.show_in_editmode = True
+
+    mod.levels = levels
+    mod.render_levels = levels
+    return mod
+
+
+def modifier_armature_add(obj):
+    mod = obj.modifiers.new(name=whoami(), type='ARMATURE')
+    mod.show_in_editmode = True
+
+    arm_data = bpy.data.armatures.new(whoami())
+    arm_ob = bpy.data.objects.new(whoami(), arm_data)
+
+    scene = bpy.context.scene
+    scene.objects.link(arm_ob)
+
+    arm_ob.select = True
+    scene.objects.active = arm_ob
+
+    bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
+    bpy.ops.object.mode_set(mode='EDIT', toggle=False)
+
+    # XXX, annoying, remove bone.
+    while arm_data.edit_bones:
+        arm_ob.edit_bones.remove(arm_data.edit_bones[-1])
+
+    bone_a = arm_data.edit_bones.new("Bone.A")
+    bone_b = arm_data.edit_bones.new("Bone.B")
+    bone_b.parent = bone_a
+
+    bone_a.head = -1, 0, 0
+    bone_a.tail = 0, 0, 0
+    bone_b.head = 0, 0, 0
+    bone_b.tail = 1, 0, 0
+
+    # Get armature animation data
+    bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
+
+    # 45d armature
+    arm_ob.pose.bones["Bone.B"].rotation_quaternion = 1, -0.5, 0, 0
+
+    # set back to the original
+    scene.objects.active = obj
+
+    # display options
+    arm_ob.show_x_ray = True
+    arm_data.draw_type = 'STICK'
+
+    # apply to modifier
+    mod.object = arm_ob
+
+    mesh_vgroup_add(obj, name="Bone.A", axis=0, invert=True)
+    mesh_vgroup_add(obj, name="Bone.B", axis=0, invert=False)
+
+    return mod
+
+
+def modifier_mirror_add(obj):
+    mod = obj.modifiers.new(name=whoami(), type='MIRROR')
+    mod.show_in_editmode = True
+
+    return mod
+
+
+def modifier_solidify_add(obj, thickness=0.25):
+    mod = obj.modifiers.new(name=whoami(), type='SOLIDIFY')
+    mod.show_in_editmode = True
+
+    mod.thickness = thickness
+
+    return mod
+
+
+# -----------------------------------------------------------------------------
+# models
+
+# useful since its solid boxy shape but simple enough to debug errors
+cube_like_vertices = (
+    (1, 1, -1),
+    (1, -1, -1),
+    (-1, -1, -1),
+    (-1, 1, -1),
+    (1, 1, 1),
+    (1, -1, 1),
+    (-1, -1, 1),
+    (-1, 1, 1),
+    (0, -1, -1),
+    (1, 0, -1),
+    (0, 1, -1),
+    (-1, 0, -1),
+    (1, 0, 1),
+    (0, -1, 1),
+    (-1, 0, 1),
+    (0, 1, 1),
+    (1, -1, 0),
+    (1, 1, 0),
+    (-1, -1, 0),
+    (-1, 1, 0),
+    (0, 0, -1),
+    (0, 0, 1),
+    (1, 0, 0),
+    (0, -1, 0),
+    (-1, 0, 0),
+    (2, 0, 0),
+    (2, 0, -1),
+    (2, 1, 0),
+    (2, 1, -1),

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list