def buildModelFromDictionary(model): """Creates the Blender representation of the imported model, using a model dictionary. Args: model: Returns: """ # DOCU add some more docstring log("Creating Blender model...", 'INFO') log("Creating links...", 'INFO') for l in model['links']: link = model['links'][l] model['links'][l]['object'] = linkmodel.createLink(link) log("Setting parent-child relationships", 'INFO') bUtils.toggleLayer(defs.layerTypes['link'], True) for l in model['links']: parent = model['links'][l] for c in parent['children']: child = model['links'][c] child['object'].matrix_world = parent['object'].matrix_world sUtils.selectObjects([child['object'], parent['object']], True, 1) bpy.ops.object.parent_set(type='BONE_RELATIVE') log("Creating joints...", 'INFO') for j in model['joints']: joint = model['joints'][j] jointmodel.createJoint(joint) log('...finished.', 'INFO') # set transformations log("Placing links...", 'INFO') for l in model['links']: if 'parent' not in model['links'][l]: root = model['links'][l] break linkmodel.placeChildLinks(model, root) log("Assigning model name...", 'INFO') try: rootlink = sUtils.getRoot(bpy.data.objects[root['name']]) rootlink['modelname'] = model['name'] rootlink.location = (0, 0, 0) except (KeyError, NameError): log("Could not assign model name to root link.", "ERROR") try: log("Creating sensors...", 'INFO') for s in model['sensors']: sensormodel.createSensor(model['sensors'][s]) except KeyError: log("No sensors in model " + model['name'], 'INFO') try: log("Creating motors...", 'INFO') for m in model['motors']: eUtils.addDictionaryToObj( model['motors'][m], model['joints'][model['motors'][m]['joint']], category='motor') except KeyError: log("No motors in model " + model['name'], 'INFO') try: log("Creating controllers...", 'INFO') for c in model['controllers']: controllermodel.createController(model['controllers'][c]) except KeyError: log("No controllers in model " + model['name'], 'INFO') try: log("Creating groups...", 'INFO') for g in model['groups']: createGroup(model['groups'][g]) except KeyError: log("No kinematic groups in model " + model['name'], 'INFO') try: log("Creating chains...", 'INFO') for ch in model['chains']: createChain(model['chains'][ch]) except KeyError: log("No kinematic chains in model " + model['name'], 'INFO') try: log("Creating lights...", 'INFO') for l in model['lights']: lightmodel.createLight(model['lights'][l]) except KeyError: log("No lights in model " + model['name'], 'INFO') # display all objects after import for obj in bpy.data.objects: bUtils.setObjectLayersActive(obj) bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.select_all(action='SELECT') bpy.ops.view3d.view_selected() # update transformations bUtils.update()
def buildModelFromDictionary(model): """Creates the Blender representation of the imported model, using a model dictionary. """ # DOCU add some more docstring log("Creating Blender model...", 'INFO') log("Creating links...", 'INFO') for l in model['links']: link = model['links'][l] linkmodel.createLink(link) log("Creating joints...", 'INFO') for j in model['joints']: joint = model['joints'][j] jointmodel.createJoint(joint) # build tree recursively and correct translation & rotation on the fly log("Placing links...", 'INFO') for l in model['links']: if 'parent' not in model['links'][l]: root = model['links'][l] linkmodel.placeChildLinks(model, root) log("Assigning model name...", 'INFO') try: rootlink = sUtils.getRoot(bpy.data.objects[root['name']]) rootlink['modelname'] = model['name'] rootlink.location = (0, 0, 0) except KeyError: log("Could not assign model name to root link.", "ERROR") log("Placing visual and collision objects...", 'INFO') for link in model['links']: linkmodel.placeLinkSubelements(model['links'][link]) try: log("Creating sensors...", 'INFO') for s in model['sensors']: sensormodel.createSensor(model['sensors'][s]) except KeyError: log("No sensors in model " + model['name'], 'INFO') try: log("Creating motors...", 'INFO') for m in model['motors']: eUtils.addDictionaryToObj( model['motors'][m], model['joints'][model['motors'][m]['joint']], category='motor') except KeyError: log("No motors in model " + model['name'], 'INFO') try: log("Creating controllers...", 'INFO') for c in model['controllers']: controllermodel.createController(model['controllers'][c]) except KeyError: log("No controllers in model " + model['name'], 'INFO') try: log("Creating groups...", 'INFO') for g in model['groups']: createGroup(model['groups'][g]) except KeyError: log("No kinematic groups in model " + model['name'], 'INFO') try: log("Creating chains...", 'INFO') for ch in model['chains']: createChain(model['chains'][ch]) except KeyError: log("No kinematic chains in model " + model['name'], 'INFO') try: log("Creating lights...", 'INFO') for l in model['lights']: lightmodel.createLight(model['lights'][l]) except KeyError: log("No lights in model " + model['name'], 'INFO') # FIXME: this is a trick to force Blender to apply matrix_local # AAAAAARGH: THIS DOES NOT WORK! for obj in bpy.data.objects: bUtils.setObjectLayersActive(obj) bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.select_all(action='SELECT') bpy.ops.transform.translate(value=(0, 0, 0))
def buildModelFromDictionary(model): """Creates the Blender representation of the imported model, using a model dictionary. Args: model(dict): model representation of the imported model Returns: """ log("Creating Blender model...", 'INFO', prefix='\n' + '-' * 25 + '\n') log(" Initializing materials... ({} total)".format(len(model['materials'])), 'INFO') for mat in model['materials']: matmodel.createMaterial(model['materials'][mat], logging=True, adjust=True) # ['name'], tuple(mat['color'][0:3]), (1, 1, 1), mat['color'][-1]) newobjects = [] log(" Creating links... ({} total)".format(len(model['links'])), 'INFO') for lnk in model['links']: link = model['links'][lnk] model['links'][lnk]['object'] = linkmodel.createLink(link) newobjects.append(model['links'][lnk]['object']) newobjects.extend(model['links'][lnk]['object'].children) log("Setting parent-child relationships", 'INFO', prefix='\n') bUtils.toggleLayer('link', True) for lnk in model['links']: parent = model['links'][lnk] log("Children for link " + parent['name'] + ":\n" + '\n'.join(parent['children']), 'DEBUG') for chi in parent['children']: child = model['links'][chi] child['object'].matrix_world = parent['object'].matrix_world eUtils.parentObjectsTo(child['object'], parent['object']) # set transformations log("Transforming links... ({} total)".format(len(model['links'])), 'INFO', prefix='\n') for lnk in model['links']: if 'parent' not in model['links'][lnk]: root = model['links'][lnk] break linkmodel.setLinkTransformations(model, root) log("Creating joints... ({} total)".format(len(model['joints'])), 'INFO', prefix='\n') for j in model['joints']: joint = model['joints'][j] jointmodel.createJoint(joint, links=model['links']) log("Assigning model name: {}".format(model['name']), 'INFO') rootlink = sUtils.getRoot(bpy.data.objects[root['object'].name]) if 'name' not in model: log("Model name not specified in URDF. Make sure to define it thereafter.", 'WARNING') else: rootlink['model/name'] = model['name'] rootlink.location = (0, 0, 0) # TODO make sure this works log("Creating sensors...", 'INFO') if 'sensors' in model and model['sensors']: for sen in model['sensors']: sensormodel.createSensor(model['sensors'][sen], model['sensors'][sen]['parent']) else: log(" No sensors in model.", 'INFO') # TODO make sure this works log("Creating motors...", 'INFO') if 'motors' in model and model['motors']: for motor in model['motors']: eUtils.setProperties( model['joints'][model['motors'][motor]['joint']], model['motors'][motor], category='motor', ) else: log(" No motors in model.", 'INFO') # TODO make sure this works log("Creating groups...", 'INFO') if 'groups' in model and model['groups']: for group in model['groups']: createGroup(model['groups'][group]) else: log(" No kinematic groups in model.", 'INFO') # TODO make sure this works log("Creating chains...", 'INFO') if 'chains' in model and model['chains']: for ch in model['chains']: createChain(model['chains'][ch]) else: log(" No kinematic chains in model.", 'INFO') # TODO make sure this works log("Creating lights...", 'INFO') if 'lights' in model and model['lights']: for light in model['lights']: lightmodel.createLight(model['lights'][light]) else: log(" No lights in model.", 'INFO') # display new objects after import sUtils.selectObjects(newobjects, clear=True, active=0) eUtils.sortObjectsToLayers(newobjects) for obj in newobjects: bUtils.setObjectLayersActive(obj, extendlayers=True) bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.view3d.view_selected() # update the scene bUtils.update()