コード例 #1
0
def set_stl_origin(stl_obj, current_obj_origin, required_obj_origin,
                   scene):  # pragma nocover
    """
    Move the object so the required origin is at (0, 0, 0). Then set the
    origin for the generated stl object. Origin can't be changed, so creating
    a compound of itself allows setting an origin location

    :param stl_obj: The generated stl object.
    :type stl_obj: class:`vpython.compound`
    :param current_obj_origin: Current coordinates of the origin of the model
    :type current_obj_origin: class:`vpython.vector`
    :param required_obj_origin: Required coordinates to place the origin
        at (0, 0, 0)
    :type required_obj_origin: class:`vpython.vector`
    :param scene: The scene in which to draw the object
    :type scene: class:`vpython.canvas`
    :return: Compound object of itself, with origin set respective to the joint
    :rtype: class:`vpython.compound`
    """
    # Move the object to put the origin at 0, 0, 0
    movement = required_obj_origin - current_obj_origin
    stl_obj.pos += movement

    # Set invisible to return an overwritten copy
    stl_obj.visible = False

    # Return a compound of itself with the origin at (0, 0, 0)
    return compound([stl_obj],
                    origin=vector(0, 0, 0),
                    vector=x_axis_vector,
                    canvas=scene)
コード例 #2
0
    def __set_graphic(self, structure, axis_through):
        """
        Set the graphic object depending on if one was given. If no object was
        given, create a box and return it

        :param structure: `float` or `str` representing the joint length or
            STL path to load from
        :type structure: `float`, `str`
        :param axis_through: The axis that the longest side goes through
        :type axis_through: class:`numpy.ndarray`
        :raises ValueError: Joint length must be greater than 0
        :return: Graphical object for the joint
        :rtype: class:`vpython.compound`
        """
        if isinstance(structure, float):
            length = structure
            if length <= 0.0:
                raise ValueError("Joint length must be greater than 0")
            axis = vector(axis_through[0], axis_through[1], axis_through[2])
            axis.mag = length

            box_midpoint = axis / 2
            box_tooltip = axis

            # Create a box along the +x axis, with the origin
            # (point of rotation) at (0, 0, 0)
            graphic_obj = box(
                canvas=self.__scene,
                pos=vector(box_midpoint.x, box_midpoint.y, box_midpoint.z),
                axis=axis,
                size=vector(length, 0.1, 0.1),  # L, W, H
                # up=y_axis_vector
            )

            # Set the boxes new origin
            graphic_obj = compound([graphic_obj],
                                   origin=box_tooltip,
                                   axis=axis)

            return graphic_obj
        else:
            # self.stl_offset = structure[2]
            return import_object_from_numpy_stl(structure, self.__scene)
コード例 #3
0
    def set_texture(self, colour=None, texture_link=None):
        """
        Apply the texture/colour to the object. If both are given, both are
        applied.
        Texture link can either be a link to an online image, or local file.

        WARNING: If the texture can't be loaded, the object will have n
        texture
        (appear invisible, but not set as invisible).

        WARNING: If the image has a width or height that is not a power of 2
        (that is, not 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, etc.),
        the image is stretched to the next larger width or height that is
        a power of 2.

        :param colour: List of RGB values
        :type colour: `list`, optional
        :param texture_link: Path/website to a texture image
        :type texture_link: `str`, optional
        :raises ValueError: RGB values must be normalised between 0 and 1
        """
        # Apply the texture
        if texture_link is not None:
            self.__graphic_obj.texture = {
                'file': texture_link
                # 'bumpmap', 'place', 'flipx', 'flipy', 'turn'
            }
            # Wait for the texture to load
            self.__scene.waitfor("textures")
        else:
            # Remove any texture
            self.__graphic_obj.texture = None

        # Apply the colour
        if colour is not None:
            if colour[0] > 1.0 or colour[1] > 1.0 or colour[2] > 1.0 or \
               colour[0] < 0.0 or colour[1] < 0.0 or colour[2] < 0.0:
                raise ValueError(
                    "RGB values must be normalised between 0 and 1")
            colour_vec = vector(colour[0], colour[1], colour[2])
            self.__graphic_obj.color = colour_vec
        else:
            # Set to white if none given
            self.__graphic_obj.color = color.white