IRESTE/blender_import.py
2022-05-23 21:22:46 +02:00

75 lines
2.3 KiB
Python

import sys, os, bpy, math
from random import randint
directory = os.path.dirname(bpy.data.filepath)
path = "./Traffic" # path to projects files
sys.path.append(path)
from CarController import CarController
from Map import Map
m = Map()
cc = CarController(m)
m.fromPath(path+"/rdpt_polytech_fixed.net.xml")
cc.fromPath(path+"/rdpt_polytech_burst_moy.rou.xml")
cc.prepareRoute()
def createLane(pts):
crv = bpy.data.curves.new("crv", "CURVE")
crv.dimensions = "3D"
spline = crv.splines.new(type = "POLY")
spline.points.add(len(pts)-1)
for p, np in zip(spline.points, pts):
p.co = list(np) + [0,1]
p.tilt = math.pi/2
spline.use_endpoint_u = True
crv.extrude = 1
#crv.bevel_depth = 2
obj = bpy.data.objects.new("test_curve", crv)
return obj
def createNet():
netCollection = bpy.data.collections.new("gen_net")
bpy.context.collection.children.link(netCollection)
for edge in m.net.getEdges():
for lane in edge.getLanes():
obj = createLane(lane.getShape())
netCollection.objects.link(obj)
coll_names = ["gen_cars","gen_net"]
coll_names = filter(lambda n: n in bpy.data.collections ,coll_names)
gen_coll = [bpy.data.collections[n] for n in coll_names]
print(gen_coll)
bpy.ops.object.delete({"selected_objects": gen_coll})
createNet()
ref_car = bpy.data.objects["Car.001"]
carsCollection = bpy.data.collections.new("gen_cars")
bpy.context.collection.children.link(carsCollection)
cars = {}
for i in range (0,6000):
cc.update()
bpy.context.scene.frame_set(round(cc.t*96))
for ind,car in enumerate(cc.cars):
obj_id = car.id#f"Car.{ind:03d}"
carObj = None
if obj_id in cars:
carObj = cars[obj_id]
else:
randCarInd = randint(1,17)
refCar = bpy.data.objects[f"Car.{randCarInd:03d}"]
carObj = refCar.copy()
carObj.name = obj_id
print(carObj.name, obj_id)
carObj.data = refCar.data.copy()
carObj.animation_data_clear()
carsCollection.objects.link(carObj)
cars[obj_id] = carObj
carObj.location = car.pos + [0]
carObj.rotation_euler = (0,0,car.dir)
carObj.keyframe_insert(data_path = "location")
carObj.keyframe_insert(data_path = "rotation_euler")