[Bf-extensions-cvs] [095aa06f] master: restore mesh_easy_lattice: T66947

meta-androcto noreply at git.blender.org
Mon Dec 9 05:04:56 CET 2019


Commit: 095aa06fbedb49b6c42bfaa7a27ff4877c6f608e
Author: meta-androcto
Date:   Mon Dec 9 15:04:40 2019 +1100
Branches: master
https://developer.blender.org/rBAC095aa06fbedb49b6c42bfaa7a27ff4877c6f608e

restore mesh_easy_lattice: T66947

===================================================================

A	mesh_easy_lattice.py

===================================================================

diff --git a/mesh_easy_lattice.py b/mesh_easy_lattice.py
new file mode 100644
index 00000000..d7c744eb
--- /dev/null
+++ b/mesh_easy_lattice.py
@@ -0,0 +1,735 @@
+# ##### 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_info = {
+            "name": " Easy Lattice",
+            "author": "Kursad Karatas / Mechanical Mustache Labs",
+            "version": ( 1, 0, 1 ),
+            "blender": ( 2, 80,0 ),
+            "location": "View3D > EZ Lattice",
+            "description": "Create a lattice for shape editing",
+            "warning": "",
+            "wiki_url": "",
+            "tracker_url": "",
+            "category": "Mesh"}
+
+import bpy
+import copy
+
+import bmesh
+from bpy.app.handlers import persistent
+from bpy.props import (EnumProperty, FloatProperty, FloatVectorProperty,
+                       IntProperty, StringProperty, BoolProperty)
+from bpy.types import Operator
+from bpy_extras.object_utils import AddObjectHelper, object_data_add
+from mathutils import Matrix, Vector
+import mathutils
+import numpy as np
+
+
+LAT_TYPES= ( ( 'KEY_LINEAR', 'KEY_LINEAR', '' ), ( 'KEY_CARDINAL', 'KEY_CARDINAL', '' ), ( 'KEY_BSPLINE', 'KEY_BSPLINE', '' ) )
+
+OP_TYPES= ( ( 'NEW', 'NEW', '' ), ( 'APPLY', 'APPLY', '' ), ( 'CLEAR', 'CLEAR', '' ) )
+
+def defineSceneProps():
+    bpy.types.Scene.ezlattice_object = StringProperty(name="Object2Operate",
+                                        description="Object to be operated on",
+                                        default="")
+
+    bpy.types.Scene.ezlattice_objects = StringProperty(name="Objects2Operate",
+                                        description="Objects to be operated on",
+                                        default="")
+
+    bpy.types.Scene.ezlattice_mode = StringProperty(name="CurrentMode",
+                                        default="")
+
+    bpy.types.Scene.ezlattice_lattice = StringProperty(name="LatticeObName",
+                                        default="")
+
+    bpy.types.Scene.ezlattice_flag = BoolProperty(name="LatticeFlag", default=False)
+
+def defineObjectProps():
+
+    bpy.types.Object.ezlattice_flag = BoolProperty(name="LatticeFlag", default=False)
+
+    bpy.types.Object.ezlattice_controller = StringProperty(name="LatticeController", default="")
+
+    bpy.types.Object.ezlattice_modifier = StringProperty(name="latticeModifier", default="")
+
+def objectMode():
+
+    if isEditMode():
+        bpy.ops.object.mode_set(mode="OBJECT")
+
+    else:
+        return True
+
+    return
+
+def isEditMode():
+    """Check to see we are in edit  mode
+    """
+
+    try:
+        if bpy.context.object.mode == "EDIT":
+            return True
+
+        else:
+            return False
+
+    except:
+        print("No active mesh object")
+
+
+
+def isObjectMode():
+
+    if bpy.context.object.mode == "OBJECT":
+        return True
+
+    else:
+        return False
+
+
+def setMode(mode=None):
+
+    if mode:
+        bpy.ops.object.mode_set(mode=mode)
+
+def curMode():
+
+    return bpy.context.object.mode
+
+def editMode():
+
+    if not isEditMode():
+        bpy.ops.object.mode_set(mode="EDIT")
+
+    else:
+        return True
+
+    return
+
+
+def setSelectActiveObject(context, obj):
+
+    if context.mode == 'OBJECT':
+
+        bpy.ops.object.select_all(action='DESELECT')
+        context.view_layer.objects.active = obj
+        obj.select_set(True)
+
+
+def getObject(name=None):
+    try:
+        ob=[o for o in bpy.data.objects if o.name == name][0]
+        return ob
+
+    except:
+        return None
+
+
+
+def buildTrnScl_WorldMat( obj ):
+    # This function builds a real world matrix that encodes translation and scale and it leaves out the rotation matrix
+    # The rotation is applied at obejct level if there is any
+    loc,rot,scl=obj.matrix_world.decompose()
+    mat_trans = mathutils.Matrix.Translation( loc)
+
+    mat_scale = mathutils.Matrix.Scale( scl[0], 4, ( 1, 0, 0 ) )
+    mat_scale @= mathutils.Matrix.Scale( scl[1], 4, ( 0, 1, 0 ) )
+    mat_scale @= mathutils.Matrix.Scale( scl[2], 4, ( 0, 0, 1 ) )
+
+    mat_final = mat_trans @ mat_scale
+
+    return mat_final
+
+def getSelectedVerts(context):
+    """
+    https://devtalk.blender.org/t/foreach-get-for-selected-vertex-indices/7712/6
+
+    v_sel = np.empty(len(me.vertices), dtype=bool)
+    me.vertices.foreach_get('select', v_sel)
+
+    sel_idx, = np.where(v_sel)
+    unsel_idx, = np.where(np.invert(v_sel))
+
+    """
+
+    obj = context.active_object
+    m=obj.matrix_world
+
+    count=len(obj.data.vertices)
+    shape = (count, 3)
+
+
+    if obj.type=='MESH':
+        me = obj.data
+        bm = bmesh.from_edit_mesh(me)
+
+        verts=[m at v.co for v in bm.verts if v.select]
+
+    return np.array(verts, dtype=np.float32)
+
+
+def getSelectedVertsNumPy(context):
+    obj = context.active_object
+
+    if obj.type=='MESH':
+
+        obj.update_from_editmode()
+
+        #BMESH
+        me = obj.data
+        bm = bmesh.from_edit_mesh(me)
+
+        verts_selected=np.array([v.select for v in bm.verts])
+
+        count=len(obj.data.vertices)
+        shape = (count, 3)
+
+        co = np.empty(count*3, dtype=np.float32)
+
+        obj.data.vertices.foreach_get("co",co)
+
+
+        co.shape=shape
+
+        return co[verts_selected]
+
+
+def findSelectedVertsBBoxNumPy(context):
+
+
+    verts=getSelectedVerts(context)
+
+    x_min = verts[:,0].min()
+    y_min = verts[:,1].min()
+    z_min = verts[:,2].min()
+
+    x_max = verts[:,0].max()
+    y_max = verts[:,1].max()
+    z_max = verts[:,2].max()
+
+
+    x_avg = verts[:,0].mean()
+    y_avg = verts[:,1].mean()
+    z_avg = verts[:,2].mean()
+
+    middle=Vector( ( (x_min+x_max)/2,
+                    (y_min+y_max)/2,
+                    (z_min+z_max)/2 )
+                )
+
+    bbox= [ np.array([x_max,y_max,z_max], dtype=np.float32),
+            np.array([x_min, y_min, z_min],  dtype=np.float32),
+            np.array([x_avg, y_avg, z_avg],  dtype=np.float32),
+            np.array(middle)
+            ]
+
+    return bbox
+
+
+def addSelected2VertGrp():
+
+    C=bpy.context
+
+    grp=C.active_object.vertex_groups.new(name=".templatticegrp")
+    bpy.ops.object.vertex_group_assign()
+    bpy.ops.object.vertex_group_set_active( group = grp.name )
+
+def removetempVertGrp():
+
+    C=bpy.context
+
+    grp=[g for g in C.active_object.vertex_groups if ".templatticegrp" in g.name]
+
+    if grp:
+        for g in grp:
+            bpy.context.object.vertex_groups.active_index = g.index
+            bpy.ops.object.vertex_group_remove(all=False, all_unlocked=False)
+
+
+def cleanupLatticeObjects(context):
+
+    cur_obj=context.active_object
+
+    try:
+        lats=[l for l in bpy.data.objects if ".latticetemp" in l.name]
+
+        if lats:
+            for l in lats:
+                setSelectActiveObject(context, l)
+                bpy.data.objects.remove(l)
+
+                bpy.ops.ed.undo_push()
+
+        setSelectActiveObject(context, cur_obj)
+
+    except:
+        print("no cleanup")
+
+
+
+
+def cleanupLatticeModifier(context):
+
+    scn=context.scene
+
+    obj_operated_name=scn.ezlattice_object
+    obj_operated=getObject(obj_operated_name)
+
+    curmode=curMode()
+
+    temp_mod=None
+
+    obj=None
+
+    if obj_operated:
+
+        if context.active_object.type=='LATTICE':
+            setMode('OBJECT')
+            setSelectActiveObject(context, obj_operated )
+
+
+        temp_mod=[m for m in obj_operated.modifiers if ".LatticeModTemp" in m.name]
+
+        obj=obj_operated
+
+    else:
+        temp_mod=[m for m in context.object.modifiers if ".LatticeModTemp" in m.name]
+        obj=context.object
+
+    if temp_mod:
+        for m in temp_mod:
+            bpy.ops.object.modifier_remove(modifier=m.name)
+
+        setMode(curmode)
+
+        return True
+
+    return False
+
+def cleanupApplyPre(context):
+
+    scn=context.scene
+
+    obj_operated_name=scn.ezlattice_object
+
+    obj_operated=getObject(obj_operated_name)
+
+    cur_mode=curMode()
+
+
+    if obj_operated:
+
+        if context.active_object.type=='LATTICE':
+            setMode('OBJECT')
+            setSelectActiveObject(context, obj_operated )
+
+
+        temp_mod=[m for m in obj_operated.modifiers if ".LatticeModTemp" in m.name]
+
+
+        lats=[l for l in bpy.data.objects if ".latticetemp" in l.name]
+
+        cur_obj=context.active_object
+
+        curmode=curMode()
+
+
+        if isEditMode():
+            objectMode()
+
+        if temp_mod:
+            for m in temp_mod:
+                if m.object:
+                    bpy.ops.object.modifier_apply(apply_as='DATA', modifier=m.name)
+
+                else:
+                    bpy.ops.object.modifier_remove(modifier=m.name)
+
+        if lats:
+            for l in lats:
+
+                bpy.data.objects.remove(l)
+
+                bpy.ops.ed.undo_push()
+
+        setSelectActiveObject(context, cur_obj)
+
+        setMode(curmode)
+        bpy.ops.object.mode_set(mode=curmode)
+
+
+
+def createLatticeObject(context, loc=Vector((0,0,0)), scale=Vector((1,1,1)),
+                        name=".latticetemp", divisions=[], interp="KEY_BSPLINE"):
+
+    C=bpy.context
+    lat_name=name+"_"+C.object.name
+
+    lat = bpy.data.lattices.new( lat_name )
+    ob = bpy.data.objects.new( lat_name, lat )
+    ob.data.use_outside=True
+    ob.data.points_u=divisions[0]
+    ob.data.points_v=divisions[1]
+    ob

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list