Beispiel #1
0
def _get_xxx_in_blend(filepath, kind):
    if not bpy:
        return []
    objects = []
    try:
        with bpy.data.libraries.load(filepath) as (src, _):
            try:
                objects = [obj for obj in getattr(src, kind)]
            except UnicodeDecodeError as detail:
                logger.error("Unable to open file '%s'. Exception: %s" % \
                             (filepath, detail))
    except IOError as detail:
        logger.error(detail)
        raise MorseBuilderNoComponentError("Component not found")
    return objects
Beispiel #2
0
    def __init__(self, category='', name='', make_morseable=True):
        """ Initialize a MORSE component

        :param category: The category of the component (folder in
            MORSE_COMPONENTS)
        :param name: The name of the component (file in
            MORSE_COMPONENTS/category/name.blend) If ends with '.blend',
            append the objects from the Blender file.
        :param make_morseable: If the component has no property for the
            simulation, append default Morse ones. See self.morseable()
        """
        AbstractComponent.__init__(self, name=name)
        if name.endswith('.blend'):
            filepath = os.path.abspath(name)  # external blend file
        else:
            filepath = os.path.join(MORSE_COMPONENTS, category,
                                    name + '.blend')

        try:
            with bpy.data.libraries.load(filepath) as (src, _):
                try:
                    objlist = [{'name': obj} for obj in src.objects]
                except UnicodeDecodeError as detail:
                    logger.error("Unable to open file '%s'. Exception: %s" % \
                                 (filepath, detail))
        except IOError as detail:
            logger.error(detail)
            raise MorseBuilderNoComponentError("Component not found")

        bpy.ops.object.select_all(action='DESELECT')
        bpy.ops.wm.link_append(directory=filepath + '/Object/',
                               link=False,
                               autoselect=True,
                               files=objlist)
        # here we use the fact that after appending, Blender select the objects
        # and the root (parent) object first ( [0] )
        self._blendobj = bpy.context.selected_objects[0]
        self._category = category
        if make_morseable and category in ['sensors', 'actuators', 'robots'] \
                and not self.is_morseable():
            self.morseable()