[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [880] trunk/py/scripts/addons/ add_mesh_ant_landscape.py: add_mesh_ant_landscape.py

Brendon Murphy meta.androcto1 at gmail.com
Sun Aug 1 04:47:06 CEST 2010


Revision: 880
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=880
Author:   meta-androcto
Date:     2010-08-01 04:47:03 +0200 (Sun, 01 Aug 2010)

Log Message:
-----------
add_mesh_ant_landscape.py
port of script by Jimmy Hazevoet
thanks to ideasman for the noise module.

Added Paths:
-----------
    trunk/py/scripts/addons/add_mesh_ant_landscape.py

Added: trunk/py/scripts/addons/add_mesh_ant_landscape.py
===================================================================
--- trunk/py/scripts/addons/add_mesh_ant_landscape.py	                        (rev 0)
+++ trunk/py/scripts/addons/add_mesh_ant_landscape.py	2010-08-01 02:47:03 UTC (rev 880)
@@ -0,0 +1,766 @@
+# ##### 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 #####
+
+bl_addon_info = {
+    'name': 'Add Mesh: ANT Landscape',
+    'author': 'Jimmy Hazevoet',
+    'version': '0.1.0 July-2010',
+    'blender': (2, 5, 3),
+    'location': 'Add Mesh menu',
+    'description': 'Adds a landscape primitive',
+    'warning': '', # used for warning icon and text in addons panel
+    'wiki_url': 'http://wiki.blender.org/index.php/Extensions:2.5/Py/' \
+        'Scripts/',
+    'tracker_url': 'https://projects.blender.org/tracker/index.php?' \
+        'func=detail&aid=23130&group_id=153&atid=469',
+    'category': 'Add Mesh'}
+
+# import modules
+import bpy
+from bpy.props import *
+from mathutils import *
+from noise import *
+from math import *
+
+###------------------------------------------------------------
+# calculates the matrix for the new object depending on user pref
+def align_matrix(context):
+    loc = TranslationMatrix(context.scene.cursor_location)
+    obj_align = context.user_preferences.edit.object_align
+    if (context.space_data.type == 'VIEW_3D'
+        and obj_align == 'VIEW'):
+        rot = context.space_data.region_3d.view_matrix.rotation_part().invert().resize4x4()
+    else:
+        rot = Matrix()
+    align_matrix = loc * rot
+    return align_matrix
+
+# Create a new mesh (object) from verts/edges/faces.
+# verts/edges/faces ... List of vertices/edges/faces for the
+#                    new mesh (as used in from_pydata).
+# name ... Name of the new mesh (& object).
+# edit ... Replace existing mesh data.
+# Note: Using "edit" will destroy/delete existing mesh data.
+def create_mesh_object(context, verts, edges, faces, name, edit, align_matrix):
+    scene = context.scene
+    obj_act = scene.objects.active
+
+    # Can't edit anything, unless we have an active obj.
+    if edit and not obj_act:
+        return None
+
+    # Create new mesh
+    mesh = bpy.data.meshes.new(name)
+
+    # Make a mesh from a list of verts/edges/faces.
+    mesh.from_pydata(verts, edges, faces)
+
+    # Update mesh geometry after adding stuff.
+    mesh.update()
+
+    # Deselect all objects.
+    bpy.ops.object.select_all(action='DESELECT')
+
+    if edit:
+        # Replace geometry of existing object
+
+        # Use the active obj and select it.
+        ob_new = obj_act
+        ob_new.select = True
+
+        if obj_act.mode == 'OBJECT':
+            # Get existing mesh datablock.
+            old_mesh = ob_new.data
+
+            # Set object data to nothing
+            ob_new.data = None
+
+            # Clear users of existing mesh datablock.
+            old_mesh.user_clear()
+
+            # Remove old mesh datablock if no users are left.
+            if (old_mesh.users == 0):
+                bpy.data.meshes.remove(old_mesh)
+
+            # Assign new mesh datablock.
+            ob_new.data = mesh
+
+    else:
+        # Create new object
+        ob_new = bpy.data.objects.new(name, mesh)
+
+        # Link new object to the given scene and select it.
+        scene.objects.link(ob_new)
+        ob_new.select = True
+
+        # Place the object at the 3D cursor location.
+        # apply viewRotaion
+        ob_new.matrix_world = align_matrix
+
+    if obj_act and obj_act.mode == 'EDIT':
+        if not edit:
+            # We are in EditMode, switch to ObjectMode.
+            bpy.ops.object.mode_set(mode='OBJECT')
+
+            # Select the active object as well.
+            obj_act.select = True
+
+            # Apply location of new object.
+            scene.update()
+
+            # Join new object into the active.
+            bpy.ops.object.join()
+
+            # Switching back to EditMode.
+            bpy.ops.object.mode_set(mode='EDIT')
+
+            ob_new = obj_act
+
+    else:
+        # We are in ObjectMode.
+        # Make the new object the active one.
+        scene.objects.active = ob_new
+
+    return ob_new
+
+# A very simple "bridge" tool.
+# Connects two equally long vertex rows with faces.
+# Returns a list of the new faces (list of  lists)
+#
+# vertIdx1 ... First vertex list (list of vertex indices).
+# vertIdx2 ... Second vertex list (list of vertex indices).
+# closed ... Creates a loop (first & last are closed).
+# flipped ... Invert the normal of the face(s).
+#
+# Note: You can set vertIdx1 to a single vertex index to create
+#    a fan/star of faces.
+# Note: If both vertex idx list are the same length they have
+#    to have at least 2 vertices.
+def createFaces(vertIdx1, vertIdx2, closed=False, flipped=False):
+    faces = []
+
+    if not vertIdx1 or not vertIdx2:
+        return None
+
+    if len(vertIdx1) < 2 and len(vertIdx2) < 2:
+        return None
+
+    fan = False
+    if (len(vertIdx1) != len(vertIdx2)):
+        if (len(vertIdx1) == 1 and len(vertIdx2) > 1):
+            fan = True
+        else:
+            return None
+
+    total = len(vertIdx2)
+
+    if closed:
+        # Bridge the start with the end.
+        if flipped:
+            face = [
+                vertIdx1[0],
+                vertIdx2[0],
+                vertIdx2[total - 1]]
+            if not fan:
+                face.append(vertIdx1[total - 1])
+            faces.append(face)
+
+        else:
+            face = [vertIdx2[0], vertIdx1[0]]
+            if not fan:
+                face.append(vertIdx1[total - 1])
+            face.append(vertIdx2[total - 1])
+            faces.append(face)
+
+    # Bridge the rest of the faces.
+    for num in range(total - 1):
+        if flipped:
+            if fan:
+                face = [vertIdx2[num], vertIdx1[0], vertIdx2[num + 1]]
+            else:
+                face = [vertIdx2[num], vertIdx1[num],
+                    vertIdx1[num + 1], vertIdx2[num + 1]]
+            faces.append(face)
+        else:
+            if fan:
+                face = [vertIdx1[0], vertIdx2[num], vertIdx2[num + 1]]
+            else:
+                face = [vertIdx1[num], vertIdx2[num],
+                    vertIdx2[num + 1], vertIdx1[num + 1]]
+            faces.append(face)
+
+    return faces
+
+###------------------------------------------------------------
+
+# some functions for marbleNoise
+def no_bias(a):
+    return a
+
+def sin_bias(a):
+    return 0.5 + 0.5 * sin(a)
+
+def tri_bias(a):
+    b = 2 * pi
+    a = 1 - 2 * abs(floor((a * (1/b))+0.5) - (a*(1/b)))
+    return a
+
+def saw_bias(a):
+    b = 2 * pi
+    n = int(a/b)
+    a -= n * b
+    if a < 0: a += b
+    return a / b
+
+def soft(a):
+    return a
+
+def sharp(a):
+    return a**0.5
+
+def sharper(a):
+    return sharp(sharp(a))
+
+def shapes(x,y,shape=0):
+    if shape == 1:
+        # ring
+        x = x*2
+        y = y*2
+        s = -cos(x**2+y**2)/(x**2+y**2+0.5)
+    elif shape == 2:
+        # swirl
+        x = x*2
+        y = y*2
+        s = ( x*sin( x*x+y*y ) + y*cos( x*x+y*y ) )
+    elif shape == 3:
+        # bumps
+        x = x*2
+        y = y*2
+        s = (sin( x*pi ) + sin( y*pi ))
+    elif shape == 4:
+        # y grad.
+        s = y*pi
+    elif shape == 5:
+        # x grad.
+        s = x*pi
+    else:
+        # marble
+        s = ((x+y)*5)
+    return s
+
+# marbleNoise
+def marble_noise(x,y, origin, size, shape, bias, sharpnes, depth, turb, basis ):
+    x = x / size
+    y = y / size
+    s = shapes(x,y,shape)
+
+    x += origin[0]
+    y += origin[1]
+    value = s + turb * turbulence_vector((x,y,0.0), depth, 0, basis )[0]
+
+    if bias == 1:
+        value = sin_bias( value )
+    elif bias == 2:
+        value = tri_bias( value )
+    elif bias == 3:
+        value = saw_bias( value )
+    else:
+        value = no_bias( value )
+
+    if sharpnes == 1:
+        value = sharp( value )
+    elif sharpnes == 2:
+        value = sharper( value )
+    else:
+        value = soft( value )
+
+    return value
+
+# Shattered_hTerrain:
+def shattered_hterrain( x,y, H, lacunarity, octaves, offset, distort, basis ):
+    d = ( turbulence_vector( ( x, y, 0.0 ), 6, 0, 0, 0.5, 2.0 )[0] * 0.5 + 0.5 )*distort*0.5
+    t0 = ( turbulence_vector( ( x+d, y+d, 0.0 ), 0, 0, 7, 0.5, 2.0 )[0] + 0.5 )
+    t2 = ( hetero_terrain(( x*2, y*2, t0*0.25 ), H, lacunarity, octaves, offset, basis ) )
+    return (( t0*t2 )+t2*0.5)*0.5
+
+# landscape_gen
+def landscape_gen(x,y,falloffsize,options=[0,1.0,1, 0,0,1.0,0,6,1.0,2.0,1.0,2.0,0,0,0, 1.0,0.0,1,0.0,1.0,0]):
+
+    # options
+    rseed    = options[0]
+    nsize    = options[1]
+    ntype      = int( options[2][0] )
+    nbasis     = int( options[3][0] )
+    vlbasis    = int( options[4][0] )
+    distortion = options[5]
+    hard       = options[6]
+    depth      = options[7]
+    dimension  = options[8]
+    lacunarity = options[9]
+    offset     = options[10]
+    gain       = options[11]
+    marblebias     = int( options[12][0] )
+    marblesharpnes = int( options[13][0] )
+    marbleshape    = int( options[14][0] )
+    invert       = options[15]
+    height       = options[16]
+    heightoffset = options[17]
+    falloff      = int( options[18][0] )
+    sealevel     = options[19]
+    platlevel    = options[20]
+    terrace      = options[21]
+
+    # origin
+    if rseed == 0:
+        origin = 0.0,0.0,0.0
+        origin_x = 0.0
+        origin_y = 0.0
+    else:
+        # randomise origin
+        seed_set( rseed )
+        origin = random_unit_vector()
+        origin_x = 0.5 - origin[0] * 1000
+        origin_y = 0.5 - origin[1] * 1000
+
+    # adjust noise size and origin

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list