def load(self): Savable.load(self) Ui.instance().project_name = self.name print debug.curr_f(), ': Config.on_project_opening_reopen_last_level = True' if Config.instance().on_project_opening_reopen_last_level: print 'trying to reopen level named \'%s\'' % (self.level_name) if self.level_name: self.open_level(self.level_name) else: print >> sys.stderr, 'could not reopen such level.' else: print 'level auto reload skipped.'
def load(self): Savable.load(self) Ui.instance().project_name = self.name print debug.curr_f( ), ': Config.on_project_opening_reopen_last_level = True' if Config.instance().on_project_opening_reopen_last_level: print 'trying to reopen level named \'%s\'' % (self.level_name) if self.level_name: self.open_level(self.level_name) else: print >> sys.stderr, 'could not reopen such level.' else: print 'level auto reload skipped.'
def file_props(self, url): """ Takes the url to a file, and returns a dict of properties relative to it: """ if url not in self: print url print >> sys.stderr, curr_f( ), 'can only get props of contained files.' return None url = str(url[len('file://'):]) props = {} #base url props['url'] = url #full path, relative to base_path props['relpath'] = os.path.split(os.path.relpath(url, self.base_path))[0] #name of the file s = os.path.split(url)[1].split('.') props['name'], props['ext'] = '.'.join(s[:-1]), s[-1] try: #type of resource, as defined in steel (inanimate, b-shape, behavior, etc) props['resource_type'] = props['relpath'].split(os.sep)[0] except: print 'url:', url print 'props:', props raise return props
def file_props(self, url): """ Takes the url to a file, and returns a dict of properties relative to it: """ if url not in self: print url print >> sys.stderr, curr_f(), 'can only get props of contained files.' return None url = str(url[len('file://'):]) props = {} #base url props['url'] = url #full path, relative to base_path props['relpath'] = os.path.split(os.path.relpath(url, self.base_path))[0] #name of the file s = os.path.split(url)[1].split('.') props['name'], props['ext'] = '.'.join(s[:-1]), s[-1] try: #type of resource, as defined in steel (inanimate, b-shape, behavior, etc) props['resource_type'] = props['relpath'].split(os.sep)[0] except: print 'url:', url print 'props:', props raise return props
def add_resource(self, props): """ Adds a resource to the level under edition. """ #TODO: add resource sharing between levels. if self.level is None: print >> sys.stderr, debug.curr_f(), ': no current level defined yet.' return {'inanimate':Level.load_inanimate}[props['resource_type']](self.level, props)
def on_item_dropped(self, url): """ triggered when an item is dropped in the qsteelwidget. """ print 'Weld.on_item_dropped:', url #make sure all struct are present if not (self.project and self.project.level): print >> sys.stderr, 'it\'s too early to drop stuff: '\ 'create a project and a level first !' return #retrieve data if it comes from weld if url in self.resMan: props = self.resMan.file_props(url) if props is None: print >> sys.stderr, curr_f(), ': url(\'%s\') in self.resMan '\ 'but can\'t retrieve props.' % (url) return props = self.project.level.resMan.add_resource( self.resMan.base_path, props) url = props['url'] if props == {} or url not in self.project.level.resMan: print >> sys.stderr, curr_f(), 'could not retrieve file and/or '\ 'dependencies for props:', pp(props) return #instanciate it if url in self.project.level.resMan: props = self.project.level.resMan.file_props(url) dtp = self.project.level.qsteelwidget.dropTargetPosition( Config.instance().drop_target_vec) props['position'] = dtp props[ 'rotation'] = self.project.level.qsteelwidget.dropTargetRotation( ) if props['resource_type'] == 'meshes': props['meshName'] = props['name'] self.project.level.instanciate(props) s = 'dropped agent \'%s\' with id %i' % (props['name'], props['agentId']) print s Ui.instance().show_status(s) else: Ui.instance().show_status('can only drop meshes so far')
def add_resource(self, srcpath, props): """ Retrieve into base_path the file descripted by the given props, as well as each file it may depends on, according to these rules: - <filename>.mesh files depend on <filename>.material Returns the full url to the new resource, if any. Also, it makes sure all needed resource are available in ogre (if props are valid). """ if props['ext'] not in Config.instance().res_dep: print curr_f( ), ': no recorded dependencies for extension \'%s\'.' % props['ext'] return #counts the number of dependencies retrieved cnt = 0 for ext in Config.instance().res_dep[props['ext']]: dir = Config.instance().resource_ext_to_dirs[ext] filename = props['name'] + '.' + ext src = os.path.join(srcpath, dir, filename) dst = os.path.join(self.base_path, dir, filename) if self.retrieve_resource(src, dst): cnt += 1 else: print >> sys.stderr, 'could not retrieve dependency \'%s\' for props' % ext, props #did we retrieve as many dependencies as needed ? if cnt == len(Config.instance().res_dep[props['ext']]): new_props = dict(props) new_props['url'] = 'file://' + os.path.join( self.base_path, Config.instance().resource_ext_to_dirs[props['ext']], props['name'] + '.' + props['ext']) return new_props print >> sys.stderr, curr_f( ), ': could not retrieve all dependencies for props:', pp(props) return {}
def add_resource(self, props): """ Adds a resource to the level under edition. """ #TODO: add resource sharing between levels. if self.level is None: print >> sys.stderr, debug.curr_f( ), ': no current level defined yet.' return { 'inanimate': Level.load_inanimate }[props['resource_type']](self.level, props)
def on_item_dropped(self, url): """ triggered when an item is dropped in the qsteelwidget. """ print 'Weld.on_item_dropped:', url #make sure all struct are present if not(self.project and self.project.level): print >> sys.stderr, 'it\'s too early to drop stuff: '\ 'create a project and a level first !' return #retrieve data if it comes from weld if url in self.resMan: props = self.resMan.file_props(url) if props is None: print >> sys.stderr, curr_f(), ': url(\'%s\') in self.resMan '\ 'but can\'t retrieve props.' % (url) return props = self.project.level.resMan.add_resource(self.resMan.base_path, props) url = props['url'] if props == {} or url not in self.project.level.resMan: print >> sys.stderr, curr_f(), 'could not retrieve file and/or '\ 'dependencies for props:', pp(props) return #instanciate it if url in self.project.level.resMan: props = self.project.level.resMan.file_props(url) dtp = self.project.level.qsteelwidget.dropTargetPosition(Config.instance().drop_target_vec) props['position'] = dtp props['rotation'] = self.project.level.qsteelwidget.dropTargetRotation() if props['resource_type'] == 'meshes': props['meshName'] = props['name'] self.project.level.instanciate(props) s = 'dropped agent \'%s\' with id %i' % (props['name'], props['agentId']) print s Ui.instance().show_status(s) else: Ui.instance().show_status('can only drop meshes so far')
def load(self): if os.path.exists(self.savepath): file = open(self.savepath) unpickled = pickle.load(file) file.close() loaded = [] for k, v in unpickled.iteritems(): setattr(self, k, unpickled[k]) diff = set(self.savelist)-set(unpickled.keys()) if diff: s = 'Some attributes that would have been saved were\'nt '\ 'found in the loaded file:\n%s' % ', '.join(diff) print >> sys.err, curr_f(), s
def load(self): if os.path.exists(self.savepath): file = open(self.savepath) unpickled = pickle.load(file) file.close() loaded = [] for k, v in unpickled.iteritems(): setattr(self, k, unpickled[k]) diff = set(self.savelist) - set(unpickled.keys()) if diff: s = 'Some attributes that would have been saved were\'nt '\ 'found in the loaded file:\n%s' % ', '.join(diff) print >> sys.err, curr_f(), s
def add_resource(self, srcpath, props): """ Retrieve into base_path the file descripted by the given props, as well as each file it may depends on, according to these rules: - <filename>.mesh files depend on <filename>.material Returns the full url to the new resource, if any. Also, it makes sure all needed resource are available in ogre (if props are valid). """ if props['ext'] not in Config.instance().res_dep: print curr_f(), ': no recorded dependencies for extension \'%s\'.' % props['ext'] return #counts the number of dependencies retrieved cnt = 0 for ext in Config.instance().res_dep[props['ext']]: dir = Config.instance().resource_ext_to_dirs[ext] filename = props['name'] + '.' + ext src = os.path.join(srcpath, dir, filename) dst = os.path.join(self.base_path, dir, filename) if self.retrieve_resource(src, dst): cnt += 1 else: print >> sys.stderr, 'could not retrieve dependency \'%s\' for props' % ext, props #did we retrieve as many dependencies as needed ? if cnt == len(Config.instance().res_dep[props['ext']]): new_props = dict(props) new_props['url'] = 'file://' + os.path.join(self.base_path, Config.instance().resource_ext_to_dirs[props['ext']], props['name'] + '.' + props['ext']) return new_props print >> sys.stderr, curr_f(), ': could not retrieve all dependencies for props:', pp(props) return {}
def qte_changed(self, qte, model_type, model_id): """ Called when the text in a QPlainTextEdit is modified. (linkage is done in self.on_agents_selected) """ print 'qte_changed', qte, model_type, model_id # check new json is still valid s = qte.document().toPlainText() try: eval(s) except Exception, e: print >> sys.stderr, curr_f(), ':\n', s, \ '\nProperties description is not valid json:\n', e.args return
def __contains__(self, _url): """ Returns True if the pointed file can be considered a resource, that is if the given url points to a file contained in one of base_path subdirs. """ try: url = str(_url) url = url[len('file://'):] except: print type(url) print >> sys.stderr, 'in', curr_f(), 'url must be an url:', url return False if not os.path.exists(url): #print >> sys.stderr, 'in', curr_f(), 'url does not exist:', url return False relpathdir, filename = os.path.split(os.path.relpath(url, self.base_path)) if relpathdir.split(os.sep)[0] in ResourceManager.base_dirs: return True else: print 'relpathdir:', relpathdir return False
def __contains__(self, _url): """ Returns True if the pointed file can be considered a resource, that is if the given url points to a file contained in one of base_path subdirs. """ try: url = str(_url) url = url[len('file://'):] except: print type(url) print >> sys.stderr, 'in', curr_f(), 'url must be an url:', url return False if not os.path.exists(url): #print >> sys.stderr, 'in', curr_f(), 'url does not exist:', url return False relpathdir, filename = os.path.split( os.path.relpath(url, self.base_path)) if relpathdir.split(os.sep)[0] in ResourceManager.base_dirs: return True else: print 'relpathdir:', relpathdir return False
def save(self): """ Saves the object into <self.savepath>. """ toPickle = {} for varname in self.savelist: if hasattr(self, varname): toPickle[varname] = getattr(self, varname) else: s = 'Trying to save a var that does not exist: \'%s\'' % varname print >> sys.err, curr_f(), s filepath, filename = os.path.split(self.savepath) if not os.path.exists(filepath): os.makedirs(filepath) try: # backup previous save if os.path.exists(self.savepath): os.rename(self.savepath, self.savepath + '.back') # create dir if necessary dirpath, _ = os.path.split(self.savepath) if not os.path.exists(dirpath): os.makedirs(dirpath, 'rwx') # do actual saving file = open(self.savepath, 'wb') pickle.dump(toPickle, file) file.close() # remove backup if os.path.exists(self.savepath + '.back'): os.remove(self.savepath + '.back') except Exception, e: raise print >> sys.stderr, 'in Savable%s.save():' % self, e.args