Properties and UI for associating a patch template object with a vertex
authorLukas Tönne <lukas.toenne@gmail.com>
Tue, 9 Dec 2014 16:06:20 +0000 (17:06 +0100)
committerLukas Tönne <lukas.toenne@gmail.com>
Tue, 9 Dec 2014 16:06:20 +0000 (17:06 +0100)
group of the ground object.

This will allow filtering samples later on and selecting between
different template groups (defined by shared vgroup).

object_physics_meadow/blob.py
object_physics_meadow/duplimesh.py
object_physics_meadow/meadow.py
object_physics_meadow/settings.py
object_physics_meadow/ui.py

index 8b65f3f..61b9a1f 100644 (file)
@@ -166,10 +166,10 @@ def make_blobs(context, gridob, groundob, samples):
     
     blob_list = []
     for v in gridob.data.vertices:
-        ok, loc, nor = project_on_ground(groundob, v.co)
+        ok, loc, nor, index = project_on_ground(groundob, v.co)
         blob_list.append((loc, []))
     
-    for loc, nor in samples:
+    for loc, nor, face_index in samples:
         # note: use only 2D coordinates for weighting, z component should be 0
         index = assign_blob(blobtree, (loc[0], loc[1], 0.0), nor)
         if index >= 0:
index f4d81ba..49a665a 100644 (file)
 import bpy
 from mathutils import *
 
+def get_vgroup_index(ob, name):
+    for v in ob.vertex_groups:
+        if v.name == name:
+            return v.index
+    return -1
+
+def get_vgroup_weight(vertex, index):
+    for elem in vertex.groups:
+        if elem.group == index:
+            return elem.weight
+    return 0.0
+
+def interp_vgroup(ob, face_index, co, vgroup):
+    vgroup_index = get_vgroup_index(ob, vgroup)
+    if vgroup_index < 0:
+        return 0.0
+    
+    mesh = ob.data
+    face = mesh.tessfaces[face_index]
+    verts = [mesh.vertices[i] for i in face.vertices]
+    weights = [get_vgroup_weight(v, vgroup_index) for v in verts]
+    return weights[0] # XXX TODO
+
 def project_on_ground(groundob, co):
     groundmat4 = groundob.matrix_world
     groundmat3 = groundmat4.to_3x3()
@@ -33,9 +56,9 @@ def project_on_ground(groundob, co):
     
     hit, nor, index = groundob.ray_cast(ray_start, ray_end)
     if index >= 0:
-        return True, groundmat4 * hit, groundmat3 * nor
+        return True, groundmat4 * hit, groundmat3 * nor, index
     else:
-        return False, co, (0.0, 0.0, 1.0)
+        return False, co, (0.0, 0.0, 1.0), -1
 
 
 def make_dupli_mesh(name, obmat, samples, scale):
index 676dd55..cb9da93 100644 (file)
@@ -53,9 +53,9 @@ def make_samples(context, gridob, groundob):
     # project samples onto the ground object
     samples = []
     for loc in loc2D:
-        ok, loc, nor = project_on_ground(groundob, loc)
+        ok, loc, nor, face_index = project_on_ground(groundob, loc)
         if ok:
-            samples.append((loc, nor))
+            samples.append((loc, nor, face_index))
     
     return samples
 
index 2833d55..21f8172 100644 (file)
@@ -25,6 +25,12 @@ from bpy.props import *
 def copy_rna_enum_items(type, prop):
     return [(item.identifier, item.name, item.description) for item in type.bl_rna.properties[prop].enum_items]
 
+def find_meadow_object(context, type):
+    scene = context.scene
+    for ob in scene.objects:
+        if ob.meadow.type == type:
+            return ob
+
 #-----------------------------------------------------------------------
 
 class MeadowAddonPreferences(AddonPreferences):
@@ -74,6 +80,12 @@ def type_update(self, context):
             if ob != self.id_data and ob.meadow.type == self.type:
                 ob.meadow.type = 'NONE'
 
+def vgroup_items(self, context):
+    groundob = find_meadow_object(context, 'GROUND')
+    if groundob:
+        return [(v.name, v.name, "", 'NONE', v.index) for v in groundob.vertex_groups]
+    return []
+
 class MeadowObjectSettings(PropertyGroup):
     type = EnumProperty(
         name="Type",
@@ -113,6 +125,19 @@ class MeadowObjectSettings(PropertyGroup):
         soft_max=10000
         )
     
+    density_vgroup_name = StringProperty(
+        name="Density Vertex Group Name",
+        description="Name of the vertex group to use for patch density",
+        default=""
+        )
+
+    # XXX enum wrapper would be more convenient, but harder to manage
+#    density_vgroup = EnumProperty(
+#        name="Density Vertex Group",
+#        description="Vertex group to use for patch density",
+#        items=vgroup_items
+#        )
+
     # internal
     blob_index = IntProperty(
         name="Blob Index",
index 05a6e2c..b6c0473 100644 (file)
@@ -23,6 +23,7 @@ from bpy.types import Operator, Panel
 from bpy.props import *
 
 from object_physics_meadow import meadow, settings as _settings, patch, blob
+from object_physics_meadow.settings import find_meadow_object
 
 class OBJECT_PT_Meadow(Panel):
     """Settings for meadow components"""
@@ -50,7 +51,16 @@ class OBJECT_PT_Meadow(Panel):
         layout.separator()
         
         if meadow.type == 'TEMPLATE':
+            row = layout.row()
+            groundob = find_meadow_object(context, 'GROUND')
+            if groundob:
+                row.prop_search(meadow, "density_vgroup_name", groundob, "vertex_groups", text="Density Vertex Group")
+            else:
+                row.active = False
+                row.prop(meadow, "density_vgroup_name", text="Density Vertex Group")
+            
             layout.prop(meadow, "use_as_dupli")
+            
             layout.operator("meadow.make_patches", icon='PARTICLE_PATH')
         
         elif meadow.type == 'BLOBGRID':
@@ -92,12 +102,6 @@ class MeadowOperatorBase():
                 self.report({'ERROR'}, "{0}".format(err))
                 return False, ""
         return True, cache_dir
-    
-    def find_meadow_object(self, context, type):
-        scene = context.scene
-        for ob in scene.objects:
-            if ob.meadow.type == type:
-                return ob
 
 
 class MakeBlobsOperator(MeadowOperatorBase, Operator):
@@ -116,11 +120,11 @@ class MakeBlobsOperator(MeadowOperatorBase, Operator):
         if not settings.blob_group(context):
             bpy.data.groups.new(settings.blob_groupname)
         
-        groundob = self.find_meadow_object(context, 'GROUND')
+        groundob = find_meadow_object(context, 'GROUND')
         if not groundob:
             self.report({'ERROR'}, "Could not find meadow Ground object")
             return {'CANCELLED'}
-        blobgridob = self.find_meadow_object(context, 'BLOBGRID')
+        blobgridob = find_meadow_object(context, 'BLOBGRID')
         if not blobgridob:
             self.report({'ERROR'}, "Could not find meadow Blob Grid object")
             return {'CANCELLED'}
@@ -145,11 +149,11 @@ class MakePatchesOperator(MeadowOperatorBase, Operator):
         if not settings.blob_group(context):
             bpy.data.groups.new(settings.blob_groupname)
         
-        groundob = self.find_meadow_object(context, 'GROUND')
+        groundob = find_meadow_object(context, 'GROUND')
         if not groundob:
             self.report({'ERROR'}, "Could not find meadow Ground object")
             return {'CANCELLED'}
-        blobgridob = self.find_meadow_object(context, 'BLOBGRID')
+        blobgridob = find_meadow_object(context, 'BLOBGRID')
         if not blobgridob:
             self.report({'ERROR'}, "Could not find meadow Blob Grid object")
             return {'CANCELLED'}