forked from johnroper100/CrowdMaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcm_compileBrain.py
93 lines (82 loc) · 3.26 KB
/
cm_compileBrain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Copyright 2017 CrowdMaster Developer Team
#
# ##### BEGIN GPL LICENSE BLOCK ######
# This file is part of CrowdMaster.
#
# CrowdMaster 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 3 of the License, or
# (at your option) any later version.
#
# CrowdMaster 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 CrowdMaster. If not, see <http://www.gnu.org/licenses/>.
# ##### END GPL LICENSE BLOCK #####
import bpy
from .cm_brainClasses import Brain
from .cm_nodeFunctions import logictypes, statetypes
preferences = bpy.context.user_preferences.addons[__package__].preferences
def getInputs(inp):
result = []
for link in inp.links:
fr = link.from_node
if fr.bl_idname == "NodeReroute":
result += getInputs(fr.inputs[0])
else:
result += [fr.name]
return result
def getMultiInputs(inputs):
result = []
for inp in inputs:
for link in inp.links:
fr = link.from_node
if fr.bl_idname == "NodeReroute":
result += getInputs(fr.inputs[0])
else:
result += [fr.name]
return result
def getOutputs(out):
result = []
for link in out.links:
fr = link.to_node
if fr.bl_idname == "NodeReroute":
result += getOutputs(fr.outputs[0])
else:
result += [fr.name]
return result
def compileBrain(nodeGroup, sim, userid, freezeAnimation):
"""Compile the brain that defines how and agent moves and is animated"""
result = Brain(sim, userid, freezeAnimation)
preferences = bpy.context.user_preferences.addons[__package__].preferences
"""create the connections from the node"""
for node in nodeGroup.nodes:
if node.bl_idname in logictypes:
# node.name - The identifier
# node.bl_idname - The type
item = logictypes[node.bl_idname](result, node)
node.getSettings(item)
if node.bl_idname == "PriorityNode":
item.inputs = getMultiInputs(node.inputs)
else:
item.inputs = getInputs(node.inputs["Input"])
item.dependantOn = getOutputs(node.outputs["Dependant"])
if not node.outputs["Output"].is_linked:
result.outputs.append(node.name)
result.neurons[node.name] = item
elif node.bl_idname in statetypes:
item = statetypes[node.bl_idname](result, node, node.name)
node.getSettings(item)
item.outputs = getOutputs(node.outputs["To"])
if node.bl_idname == "StartState":
result.setStartState(node.name)
else:
item.valueInputs = getInputs(node.inputs["Value"])
item.inputs = getInputs(node.inputs["From"])
if len(item.valueInputs) != 0:
result.outputs.append(node.name)
result.neurons[node.name] = item
return result