Skip to content

Commit

Permalink
Ground positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter-Noble committed Sep 16, 2016
1 parent a126e63 commit 7a8f459
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
22 changes: 22 additions & 0 deletions cm_generation/cm_genNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,25 @@ def getSettings(self):
return {"obstacleGroup": self.obstacleGroup,
"margin": self.margin}

class GroundNode(CrowdMasterAGenTreeNode):
'''The ground node'''
bl_idname = 'GroundNodeType'
bl_label = 'Ground'
bl_icon = 'SOUND'

groundMesh = StringProperty(name="Ground")

def init(self, context):
self.inputs.new("TemplateSocketType", "Template")
self.inputs[0].link_limit = 1

self.outputs.new("TemplateSocketType", "Template")

def draw_buttons(self, context, layout):
layout.prop_search(self, "groundMesh", context.scene, "objects")

def getSettings(self):
return {"groundMesh": self.groundMesh}

class NoteNode(CrowdMasterAGenTreeNode):
"""For keeping the graph well organised"""
Expand Down Expand Up @@ -533,6 +552,7 @@ def poll(cls, context):
NodeItem("TargetPositionNodeType", label="Target"),
NodeItem("ObstacleNodeType"),
NodeItem("OffsetNodeType"),
NodeItem("GroundNodeType"),
]),
CrowdMasterAGenCategories("other", "Other", items=[
NodeItem("GenerateNodeType"),
Expand Down Expand Up @@ -566,6 +586,7 @@ def register():
bpy.utils.register_class(FormationPositionNode)
bpy.utils.register_class(TargetPositionNode)
bpy.utils.register_class(ObstacleNode)
bpy.utils.register_class(GroundNode)

bpy.utils.register_class(NoteNode)

Expand Down Expand Up @@ -594,6 +615,7 @@ def unregister():
bpy.utils.unregister_class(FormationPositionNode)
bpy.utils.unregister_class(TargetPositionNode)
bpy.utils.unregister_class(ObstacleNode)
bpy.utils.unregister_class(GroundNode)

bpy.utils.unregister_class(NoteNode)

Expand Down
42 changes: 41 additions & 1 deletion cm_generation/cm_templates.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import bpy
import mathutils
BVHTree = mathutils.bvhtree.BVHTree

from collections import OrderedDict

Expand Down Expand Up @@ -442,6 +443,44 @@ def check(self):
return True


class TemplateGROUND(Template):
"""Adjust the position of requests onto a ground mesh"""
def __init__(self, inputs, settings, bpyName):
Template.__init__(self, inputs, settings, bpyName)
self.bvhtree = None

def build(self, pos, rot, scale, tags, cm_group):
sce = bpy.context.scene
gnd = sce.objects[self.settings["groundMesh"]]
if self.bvhtree is None:
self.bvhtree = BVHTree.FromObject(gnd, sce)
point = pos - gnd.location
hitA, normA, indA, distA = self.bvhtree.ray_cast(point, (0, 0, -1))
hitB, normB, indB, distB = self.bvhtree.ray_cast(point, (0, 0, 1))
if hitA and hitB:
if distA <= distB:
hitA += gnd.location
self.inputs["Template"].build(hitA, rot, scale, tags, cm_group)
else:
hitB += gnd.location
self.inputs["Template"].build(hitB, rot, scale, tags, cm_group)
elif hitA:
hitA += gnd.location
self.inputs["Template"].build(hitA, rot, scale, tags, cm_group)
elif hitB:
hitB += gnd.location
self.inputs["Template"].build(hitB, rot, scale, tags, cm_group)

def check(self):
if self.settings["groundMesh"] not in bpy.context.scene.objects:
return False
if not isinstance(self.inputs["Template"], Template):
return False
if isinstance(self.inputs["Template"], GeoTemplate):
return False
return True


class TemplateSETTAG(Template):
"""Set a tag for an agent to start with"""
def build(self, pos, rot, scale, tags, cm_group):
Expand Down Expand Up @@ -470,5 +509,6 @@ def check(self):
("RandomPositionNodeType", TemplateRANDOMPOSITIONING),
("FormationPositionNodeType", TemplateFORMATION),
("TargetPositionNodeType", TemplateTARGET),
("ObstacleNodeType", TemplateOBSTACLE)
("ObstacleNodeType", TemplateOBSTACLE),
("GroundNodeType", TemplateGROUND)
])

0 comments on commit 7a8f459

Please sign in to comment.