Created
June 9, 2017 14:38
-
-
Save helxsz/325c765e94f8b750b4a5086334cbf0a7 to your computer and use it in GitHub Desktop.
use blender to create the parking lots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #coding=utf-8 | |
| import sys | |
| import bpy | |
| import math | |
| import random | |
| import colorsys | |
| import os | |
| import mathutils | |
| import numpy as np | |
| from random import randint | |
| sys.path.append("/usr/local/lib/python2.7/dist-packages") | |
| print (sys.path) | |
| class Parking(object): | |
| sunlight_from = [2] | |
| sunlight_to = [2] | |
| blur_from = 0.0 | |
| blur_to = 0.0 | |
| dof = False | |
| saturation_factor = 1.0 | |
| value_factor = 1.0 | |
| # ranges | |
| SUNLIGHT_VALUES = [0.01, 0.5, 1.0, 1.5, 2.0, 2.5] | |
| #define car space top position | |
| Car_Space_Position_Top=[] | |
| #func one set Camera paramter,with different rota | |
| Cam_rota=[[[10,0,0],[-9,-13,22]], | |
| [[20,0,0],[-9,-17,21]], | |
| [[30,0,0],[-9,-21,19]], | |
| [[40,0,0],[-9,-23,16]], | |
| [[50,0,0],[-9,-26,13]], | |
| #[[60,0,60],[10,-19,10]], | |
| #[[60,0,75],[10,-19,10]], | |
| [[60,0,75],[10,-14,10]], | |
| #[60,0,60]参数表示[pitch,yaw,roll],[10,-19,10]表示[x,y,z]坐标,改变摄像头的参数从这六个参数修改即可。 | |
| [[70,0,0],[-9,-31,6]], | |
| [[80,0,0],[-9,-31,6]] | |
| ] | |
| def __init__(self,output_dir): | |
| self.output_dir = output_dir | |
| self.configureBlender() | |
| self.configureDataset() | |
| self.createParkingCars() | |
| self.setParkingSpots() | |
| self.setCameraPos() | |
| bpy.app.handlers.render_complete.append(self.render_complete) | |
| def startRenderingIteration(self,car_num): | |
| for i in range(0, car_num): | |
| self.sampleCarLocations(8) | |
| self.saveLocalImage(i) | |
| bpy.ops.render.render(write_still = True) | |
| def configureBlender(self): | |
| # rendering constants | |
| RENDER_RES_X = 640 | |
| RENDER_RES_Y = 480 | |
| FRAME_NO = 10000 | |
| bpy.context.scene.frame_current = FRAME_NO | |
| bpy.context.scene.render.resolution_x = RENDER_RES_X | |
| bpy.context.scene.render.resolution_y = RENDER_RES_Y | |
| bpy.context.scene.render.use_border = False | |
| self.nodes = bpy.context.scene.node_tree.nodes | |
| self.links = bpy.context.scene.node_tree.links | |
| self.nodes['File Output'].base_path = self.output_dir | |
| def configureDataset(self): | |
| if not os.path.exists(self.output_dir): | |
| os.makedirs(self.output_dir) | |
| if not os.path.exists(self.output_dir + '/images'): | |
| os.makedirs(self.output_dir + '/images') | |
| if not os.path.exists(self.output_dir + '/annotations'): | |
| os.makedirs(self.output_dir + '/annotations') | |
| if not os.path.exists(self.output_dir + '/prepare'): | |
| os.makedirs(self.output_dir + '/prepare') | |
| ########################################################################### | |
| def createParkingCars(self): | |
| self.getCarTypesFromBlender() | |
| self.getSceneObjects("car") | |
| print(self.Car_classes) | |
| self.createRandomCar([1,2,0]) | |
| self.createRandomCar([1,2,0]) | |
| print("----------------- created new object") | |
| self.getSceneObjects("car") | |
| def hide_all_cars(myCar_classes): | |
| for car in myCar_classes: | |
| bpy.data.objects[car].hide_render = True; | |
| def getCarTypesFromBlender(self): | |
| self.Car_classes = [] | |
| for obj in bpy.data.objects: #bpy.context.selected_objects | |
| if(obj.name.startswith("car")): | |
| self.Car_classes.append(obj.name) | |
| def showObjects(self): | |
| for obj in bpy.data.objects: #bpy.context.selected_objects | |
| print(obj.name,obj.select) | |
| def getSceneObjects(self,object_class): | |
| objects = [] | |
| for obj in bpy.data.objects: #bpy.context.selected_objects | |
| #print(obj.name,obj.select) | |
| if(obj.name.startswith(object_class)): | |
| objects.append(obj.name) | |
| #print(object_class ," object :", len(objects)) | |
| return objects | |
| def createRandomCar(self, location): | |
| name = np.random.choice(self.Car_classes,1) | |
| print('createRandomCar',name) | |
| self.createCarWithName(name[0],location) | |
| def createCarWithName(self,name , location): | |
| obj = bpy.data.objects[name] | |
| mesh = obj.data | |
| new_obj = bpy.data.objects.new(name, mesh) | |
| bpy.context.scene.objects.link(new_obj) | |
| vec = mathutils.Vector((location[0],location[1], location[2])) | |
| #inv = bpy.data.objects[name].matrix_world.copy() | |
| #inv.invert() | |
| #vec_rot = vec * inv | |
| #new_obj.location = vec | |
| def setCarLocation(self, car_name, location): | |
| print( 'arrangeCarLocation', car_name , location ) | |
| obj = bpy.data.objects[car_name] | |
| vec = mathutils.Vector((location[0], location[1], 0)) | |
| inv = obj.matrix_world.copy() | |
| inv.invert() | |
| vec_rot = vec * inv | |
| obj.location =vec #vec_rot | |
| ############################################################################################ | |
| def sampleCarLocations(self,n): | |
| print("car_samples") | |
| Space_Position = np.array(self.Car_Space_Position_Top) | |
| Space_Position = np.array(Space_Position).reshape(-1, 3) | |
| print(Space_Position) | |
| random_indices = np.arange(0, Space_Position.shape[0]) | |
| np.random.shuffle(random_indices) | |
| empty_index = np.where(Space_Position[:,2]==0) | |
| print( empty_index[0][:n] ) | |
| print( Space_Position[empty_index[0][:n]] ) | |
| cars = self.getSceneObjects("car") | |
| #print(cars) | |
| car_samples = np.random.choice(cars, n, replace=False) | |
| print(car_samples) | |
| for i in range(n): | |
| #print( car_samples[i], Space_Position[empty_index[0][i]] ) | |
| car_name = car_samples[i] | |
| car_position = Space_Position[empty_index[0][i]] | |
| self.setCarLocation(car_name, car_position) | |
| ############################################################################################# | |
| def setParkingSpots(self): | |
| self.Car_Space_Position_Top=[ | |
| #[[-62,29.23,1],[-49.73,29.23,1],[-31.82,29.23,1],[-14.2,29.23,1],[3.51,29.23,1],[21.21,29.23,1],[39.04,29.23,1],[57.1,29.23,1]], | |
| #[[-62,11.28,1],[-49.73,11.28,0],[-31.82,11.28,0],[-14.2,11.28,0],[3.51,11.28,0],[21.21,11.28,0] ,[39.04,11.28,0],[57.1,11.8,0]], | |
| #[[-62,-5.54,1],[-49.73,-5.54,0],[-31.82,-5.54,0] ,[-14.2,-5.54,0],[3.51,-5.54,0],[21.21,-5.54,0],[39.04,-5.54,0],[57.1,-5.54,0]], | |
| #[[-62,-23.1,1],[-49.73,-23.1,0],[-31.82,-23.1,0],[-14.2,-23.1,0],[3.51,-23.1,0],[21.21,-23.1,0],[39.04,-23.1,0] ,[57.1,-23.1,0]] | |
| [[-3.24, -11.64, 0],[-5.98, -11.64, 0],[-8.72, -11.64, 0],[-11.46, -11.64, 0],[-14.2, -11.64, 0]], | |
| [[-3.239,-5.54, 0],[-5.97,-5.54, 0],[-8.71, -5.54, 0],[-11.4, -5.54, 0], [-14.2 ,-5.54, 0]] | |
| ] | |
| ################################################################################################ | |
| def setCameraPos(self): | |
| bpy.data.objects['Camera'].location = self.Cam_rota[5][1] | |
| bpy.data.objects['Camera'].rotation_euler = [math.radians(self.Cam_rota[5][0][0]+random.random()*0.4), math.radians(0), math.radians(self.Cam_rota[5][0][2]) ] | |
| def saveLocalImage(self, i): | |
| filepath = self.output_dir + '/images/' + str(i + 1) + '.png' | |
| bpy.context.scene.render.filepath = filepath | |
| self.nodes['File Output'].file_slots[0].path = '/images2/'+str(i + 1) | |
| def render_complete(self, scene): | |
| print("render complete") | |
| ''''' | |
| batches = scene.frame_ranges | |
| i = batches.active_index | |
| if i < len(batches) - 1: | |
| batch = batches(i+1) | |
| # set up scene | |
| bpy.ops.render.render('INVOKE_DEFAUT', animation=True) | |
| else: | |
| #clear this hander | |
| bpy.app.handers.render_complete.remove(render_complete) | |
| ''''' | |
| if __name__=="__main__": | |
| parking = Parking('/home/gnss/si/Blender_test/parkinglot/Blender_render/({},{})'.format('one', 'two')) | |
| parking.startRenderingIteration(10) | |
| ''''' | |
| annot_file = open(output_dir + '/annotations.txt', 'w+') | |
| annot_file.write('') | |
| annot_file.close() | |
| ''''' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment