Creating a standalone simulation

import os
import sys

# if is windows environment
if  os.name == "nt":
	atoms_folder = "C:/Program Files/Toolchefs/AtomsMaya/"
else: # if is linux environment
	atoms_folder = "/opt/Toolchefs/AtomsMaya/"

os.environ["ATOMS_DATA"] = atoms_folder + "data"
sys.path.insert(0, atoms_folder + "python/2017/")
sys.path.insert(0, atoms_folder + "scripts")

if  os.name == "nt":
	os.environ["PATH"] += atoms_folder + "bin/2017;"
	os.environ["PATH"] += atoms_folder + "bin;"
	os.environ["PATH"] += atoms_folder + "python/2017;"
else:
	os.environ["LD_LIBRARY_PATH"] += atoms_folder + "lib/2017:"
	os.environ["LD_LIBRARY_PATH"] += atoms_folder + "lib:"

import AtomsMath
import json
import AtomsUtils
import AtomsCore
import Atoms
import AtomsModules

from Atoms.ui.staticdata import MODES
from Atoms.constants import EVENT_TYPES, JSON_KEYS

import Atoms
from Atoms.hostbridge.atomsnode import BaseAtomsNodeHostBridge
from Atoms.utils.setup import register_all_from_stored_json_data


class StandaloneHost(BaseAtomsNodeHostBridge):
    JSON_DATA = {}
    def __init__(self):
        super(StandaloneHost, self).__init__()
        self.simulation = Atoms.AgentsSimulation()
        Atoms.AgentsSimulations.instance().addAgentsSimulation("main", self.simulation)

    @classmethod
    def load_scene(cls, data_path):
        with open(data_path) as f:
            json_data = json.load(f)
            cls.JSON_DATA = json_data

    @classmethod
    def get_json_data(cls):
        return cls.JSON_DATA

    @classmethod
    def set_json_data(cls, data):
        data_ = {}
        if not JSON_KEYS.VERSION in data:
            data_[JSON_KEYS.VERSION] = JSON_VERSION
            data_[JSON_KEYS.DATA] = data
        else:
            data_ = data

        cls.JSON_DATA =  data_

    @staticmethod
    def get_agents_simulation():
        return Atoms.AgentsSimulations.instance().agentsSimulation("main")


data_path = os.environ["ATOMS_DATA"] + "/atomsRobot.json"
host = StandaloneHost()
host.load_scene(data_path)
register_all_from_stored_json_data(host)


simulation = host.get_agents_simulation()
modulesFactory = Atoms.BehaviourModules.instance()
agGroup = simulation.createAgentGroup()
agGroup.setMultithread(True)

agGroup.addBehaviourModule("gridLayout", "gridLayout")
agGroup.addBehaviourModule("stateMachine", "stateMachine")

gridLayout = agGroup.behaviourModule("gridLayout")
gridLayout.attributes()["agentType"].set("atomsRobot")
gridLayout.attributes()["size"].set(AtomsMath.V3d(10, 0, 10))
stateMachine = agGroup.behaviourModule("stateMachine")
stateMachine.attributes()["state"].set(1)

atomsTime = Atoms.SimulationTime.instance()
atomsTime.set(0)
atomsTime.setFps(24.0)

simulation.initSimulation()

for i in range(0, 50):
    atomsTime.set(i)
    simulation.initFrame()
    simulation.computeFrame()
    simulation.endFrame()
    print "Frame", i

simulation.endSimulation()

Copyright © 2017, Toolchefs LTD.