def fix_bones_influences(self, mesh, old_skeleton): active_bones = [] for section in range(0, mesh.skeletal_mesh_sections_num()): vertices = mesh.skeletal_mesh_get_soft_vertices(0, section) ue.log_warning(len(vertices)) new_vertices = [] old_bone_map = mesh.skeletal_mesh_get_bone_map(0, section) new_bone_map = [] for vertex in vertices: bone_ids = list(vertex.influence_bones) for index, bone_id in enumerate(bone_ids): if vertex.influence_weights[index] > 0: bone_ids[index] = self.get_updated_bone_index(old_skeleton, mesh.Skeleton, old_bone_map, new_bone_map, bone_id) if new_bone_map[bone_ids[index]] not in active_bones: active_bones.append(new_bone_map[bone_ids[index]]) vertex.influence_bones = bone_ids new_vertices.append(vertex) # assign new vertices mesh.skeletal_mesh_set_soft_vertices(new_vertices, 0, section) # add the new bone mapping mesh.skeletal_mesh_set_bone_map(new_bone_map, 0, section) # specify which bones are active and required (ensure root is added to required bones) mesh.skeletal_mesh_set_active_bone_indices(active_bones) # mark all the bones as required (eventually you can be more selective) mesh.skeletal_mesh_set_required_bones(list(range(0, mesh.Skeleton.skeleton_bones_get_num())))
def read(self, size): """Read bytes from the stream. Used to record audio.""" buf, overflow = self._system_audio_stream.read(size) if overflow: ue.log_warning('SoundDeviceStream read overflow (' + size + ', ' + str(len(buf)) + ')') return bytes(buf)
def onJsonInput(self, jsonInput): #build the result object result = {'prediction':-1} #If we try to predict before training is complete if not hasattr(self, 'model'): ue.log_warning("Warning! No 'model' found, prediction invalid. Did training complete?") return result #prepare the input, reshape 784 array to a 1x28x28 array x_raw = jsonInput['pixels'] x = np.reshape(x_raw, (1, 28, 28)) predictions = self.model.predict(x) #ue.log(predictions) # #run the input through our network using stored model and graph # with self.graph.as_default(): # output = self.model.predict(x) #convert output array to max value prediction index (0-10) #index, value = max(enumerate(output[0]), key=operator.itemgetter(1)) index, value = max(enumerate(predictions[0]), key=operator.itemgetter(1)) #Optionally log the output so you can see the weights for each value and final prediction ue.log('Predictions array: ' + str(predictions) + ',\nPrediction: ' + str(index)) result['prediction'] = index return result
def refresh_class_references(): """Called to load blueprint class references. Can be run at anytime to ensure class references are valid.""" # pylint: disable=global-statement # Global statement warning disabled in this function as this is a work around for delayed loading of blueprint classes try: global bp_RoomComponent bp_RoomComponent = ue.find_class('BP_RoomComponent_C') except Exception: ue.log_warning( "Unable to load class BP_RoomComponent_C. This happens during cooking. If this is happening during gameplay something has gone wrong." )
def __init__(self, fp, sample_rate, sample_width): self._fp = fp try: self._wavep = wave.open(self._fp, 'r') except wave.Error as e: ue.log_warning('error opening WAV file: ' + str(e) + ' falling back to RAW format') self._fp.seek(0) self._wavep = None self._sample_rate = sample_rate self._sample_width = sample_width self._sleep_until = 0
def PyFactoryCreateFile(self, uclass: Class, parent: Object, name: str, filename: str) -> Object: # load the collada file dae = Collada(filename) ue.log_warning(dae) self.do_import = False self.open_collada_wizard() if not self.do_import: return None # create a new UStaticMesh with the specified name and parent static_mesh = StaticMesh(name, parent) # prepare a new model with the specified build settings source_model = StaticMeshSourceModel(BuildSettings=MeshBuildSettings(bRecomputeNormals=False, bRecomputeTangents=True, bUseMikkTSpace=True, bBuildAdjacencyBuffer=True, bRemoveDegenerates=True)) # extract vertices, uvs and normals from the da file (numpy.ravel will flatten the arrays to simple array of floats) triset = dae.geometries[0].primitives[0] self.vertices = numpy.ravel(triset.vertex[triset.vertex_index]) # take the first uv channel (there could be multiple channels, like the one for lightmapping) self.uvs = numpy.ravel(triset.texcoordset[0][triset.texcoord_indexset[0]]) self.normals = numpy.ravel(triset.normal[triset.normal_index]) # fix mesh data self.FixMeshData() # create a new mesh, FRawMesh is an ptopmized wrapper exposed by the python plugin. read: no reflection involved mesh = FRawMesh() # assign vertices mesh.set_vertex_positions(self.vertices) # uvs are required mesh.set_wedge_tex_coords(self.uvs) # normals are optionals mesh.set_wedge_tangent_z(self.normals) # assign indices (not optimized, just return the list of triangles * 3...) mesh.set_wedge_indices(numpy.arange(0, len(triset) * 3)) # assign the FRawMesh to the LOD0 (the model we created before) mesh.save_to_static_mesh_source_model(source_model) # assign LOD0 to the SataticMesh and build it static_mesh.SourceModels = [source_model] static_mesh.static_mesh_build() static_mesh.static_mesh_create_body_setup() static_mesh.StaticMaterials = [StaticMaterial(MaterialInterface=self.ImportOptions.DefaultMaterial, MaterialSlotName='Main')] return static_mesh
def Setup(self, num_states, num_actions, batch_size): self._model = Model(num_states, num_actions, batch_size) self._memory = Memory(100000) self._session = tf.compat.v1.Session() self._session.run(self._model._var_init) MAX_EPSILON = 0.9 MIN_EPSILON = 0.01 LAMBDA = 0.001 GAMMA = 0.2 self._gamerunner = GameRunner(self._session, self._model, self._memory, MAX_EPSILON, MIN_EPSILON, LAMBDA, GAMMA) ue.log_warning("Setup Ended")
def step(message): """ Performs a single step in the game (could be several ticks) given some action/axis mappings. The number of ticks to perform can be specified through `num_ticks` (default=4). The fake amount of time (dt) that each tick will use can be specified through `delta_time` (default=1/60s). """ playing_world = util.get_playing_world() if not playing_world: return {"status": "error", "message": "No playing world!"} delta_time = message.get("delta_time", 1.0/60.0) # the force-set delta time (dt) for each tick num_ticks = message.get("num_ticks", 4) # the number of ticks to work through (all with the given action/axis mappings valid) controller = playing_world.get_player_controller() ue.log("step command: delta_time={} num_ticks={}".format(delta_time, num_ticks)) # DEBUG #pydevd.settrace("localhost", port=20023, stdoutToServer=True, stderrToServer=True) # DEBUG # END: DEBUG if "axes" in message: for axis in message["axes"]: # ue.log("-> axis {}={} (key={})".format(key_name, axis[1], Key(KeyName=key_name))) controller.input_axis(Key(KeyName=axis[0]), axis[1], delta_time) if "actions" in message: for action in message["actions"]: # ue.log("-> action {}={}".format(action_name, action[1])) controller.input_key(Key(KeyName=action[0]), EInputEvent.IE_Pressed if action[1] else EInputEvent.IE_Released) # unpause the game and then perform n ticks with the given inputs (actions and axes) for i in range(num_ticks): was_unpaused = GameplayStatics.SetGamePaused(playing_world, False) if not was_unpaused: ue.log_warning("Un-pausing game for next step was not successful!") playing_world.world_tick(delta_time, True) # After the first tick, reset all action mappings to False again # (otherwise sending True in two succinct steps would not(!) repeat the action). if i == 0 and "actions" in message: for action in message["actions"]: controller.input_key(Key(KeyName=action[0]), EInputEvent.IE_Released) # pause again was_paused = GameplayStatics.SetGamePaused(playing_world, True) if not was_paused: ue.log_warning("Re-pausing game after step was not successful!") return util.compile_obs_dict()
def get_unreal_master_material(self, material_name: str) -> MaterialInterface: """Gets an unreal material for use. If it's not already loaded, it will load it automatically and cache it for next time""" if material_name in self.loadedParentMaterials: return self.loadedParentMaterials[material_name] materialFullPath = "/Game/Rainbow/MasterMaterials/{matname}.{matname}".format( matname=material_name) loadedMaterial = None try: loadedMaterial = ue.load_object(MaterialInterface, materialFullPath) except Exception: ue.log_warning( f"Failed to load material: {materialFullPath} for materialDefinition: {material_name}" ) raise self.loadedParentMaterials[material_name] = loadedMaterial return loadedMaterial
def step(self, dist, next_state, reward, done): if done: next_state = None self._memory.add_sample((self._state, self._action, reward, next_state)) self._replay() self._steps += 1 self._eps = self._min_eps + (self._max_eps - self._min_eps) * math.exp(-self._decay * self._steps) self._state = next_state self._tot_reward += reward if done: ue.log_warning("Goal! - " + str(self._tot_reward)) self._reward_store.append(self._tot_reward) self._max_dist_store.append(dist) return True return False
def __init__(self): ue.log_warning('Hello i am __init__') self.timer = 1.0
import unreal_engine as ue import sys ue.log_warning('UnrealEnginePython build check: {0}/UE{1}.{2}'.format(sys.version, ue.ENGINE_MAJOR_VERSION, ue.ENGINE_MINOR_VERSION))
def cancel_operation(): global canceled ue.log_warning('slow operation canceled') canceled = True
def you_pressed_K(self): ue.log_warning('you pressed K')
def RunBegin(self, state): state = np.reshape(state, len(state)) self._gamerunner.resetState(state) ue.log_warning("RunBegin - " + str(len(state)))
def onJsonInput(self, jsonInput): #build the result object result = { 'word0': -1, } #If we try to predict before training is complete if not hasattr(self, 'model'): ue.log_warning( "Warning! No 'model' found, prediction invalid. Did training complete?" ) return result #prepare the input #these are float values passed through the json from unreal to python actionindex = jsonInput['actionindex'] word0 = jsonInput['word0'] word1 = jsonInput['word1'] word2 = jsonInput['word2'] word3 = jsonInput['word3'] word4 = jsonInput['word4'] word5 = jsonInput['word5'] word6 = jsonInput['word6'] word7 = jsonInput['word7'] word8 = jsonInput['word8'] word9 = jsonInput['word9'] inputarray = [ word0, word1, word2, word3, word4, word5, word6, word7, word8, word9 ] ue.log(repr(inputarray)) self.callEvent(str(repr(inputarray)), {}, True) inputarray_word2vec = np.zeros((1, 10, 300)) ue.log("Print Word2Vec Input:") self.callEvent(str(repr(inputarray)), {}, True) for index in range(10): try: inputarray_word2vec[0][index] = word2vecmodel[ inputarray[index]] ue.log(repr(inputarray_word2vec[0][index])) ue.log("valid word") except: #inputarray_word2vec[0][index] = np.random.uniform(low=-1, high=1, size=300) ue.log("invalid word") #self.callEvent("word2vec failed, entered non-words", {}, True) index += 1 inputarray_word2vec = np.asarray(inputarray_word2vec) ue.log("Print CNN Input:") ue.log(repr(inputarray_word2vec)) #run the input through our network using stored model and graph with self.graph.as_default(): ue.log("Start Predict") output = self.model.predict(inputarray_word2vec) ue.log(repr(output)) self.callEvent("SetTempID", actionindex, True) self.callEvent("JsonOutput", output.tolist(), True) ue.log("Finished Predict")
def begin_play(self): ue.log_warning('Hello i am begin_play')
def print_delta_time(dt): ue.log_warning("dt={}".format(dt)) return True
def PyFactoryCreateFile(self, uclass: Class, parent: Object, name: str, filename: str) -> Object: # load the collada file dae = Collada(filename) ue.log_warning(dae) self.do_import = False self.open_collada_wizard() if not self.do_import: return None # create a new UStaticMesh with the specified name and parent static_mesh = StaticMesh(name, parent) # prepare a new model with the specified build settings source_model = StaticMeshSourceModel( BuildSettings=MeshBuildSettings(bRecomputeNormals=False, bRecomputeTangents=True, bUseMikkTSpace=True, bBuildAdjacencyBuffer=True, bRemoveDegenerates=True)) # extract vertices, uvs and normals from the da file (numpy.ravel will flatten the arrays to simple array of floats) triset = dae.geometries[0].primitives[0] self.vertices = numpy.ravel(triset.vertex[triset.vertex_index]) # take the first uv channel (there could be multiple channels, like the one for lightmapping) self.uvs = numpy.ravel( triset.texcoordset[0][triset.texcoord_indexset[0]]) self.normals = numpy.ravel(triset.normal[triset.normal_index]) # fix mesh data self.FixMeshData() # create a new mesh, FRawMesh is an ptopmized wrapper exposed by the python plugin. read: no reflection involved mesh = FRawMesh() # assign vertices mesh.set_vertex_positions(self.vertices) # uvs are required mesh.set_wedge_tex_coords(self.uvs) # normals are optionals mesh.set_wedge_tangent_z(self.normals) # assign indices (not optimized, just return the list of triangles * 3...) mesh.set_wedge_indices(numpy.arange(0, len(triset) * 3)) # assign the FRawMesh to the LOD0 (the model we created before) mesh.save_to_static_mesh_source_model(source_model) # assign LOD0 to the SataticMesh and build it static_mesh.SourceModels = [source_model] static_mesh.static_mesh_build() static_mesh.static_mesh_create_body_setup() static_mesh.StaticMaterials = [ StaticMaterial( MaterialInterface=self.ImportOptions.DefaultMaterial, MaterialSlotName='Main') ] return static_mesh