예제 #1
0
    def test_frustrum_test_blast(self):

        from maya_psyhive.tank_support.ts_frustrum_test_blast import (
            blast, remove_rigs)

        # Test frustrum test
        _ref = ref.obtain_ref(namespace='archer_TMP',
                              file_=_RIG_PATH,
                              class_=blast._BlastRigRef)
        assert isinstance(_ref, blast._BlastRigRef)
        _cam = hom.HFnCamera('persp')
        _pos = hom.HMatrix([
            0.95, 0.00, 0.31, 0.00, 0.08, 0.96, -0.26, 0.00, -0.29, 0.27, 0.92,
            0.00, -19.37, 25.40, 54.23, 1.00
        ])
        _pos.apply_to(_cam)
        assert blast._rig_in_cam(rig=_ref, cam=_cam)
        _pos = hom.HMatrix([
            0.80, -0.00, -0.60, 0.00, -0.10, 0.98, -0.13, 0.00, 0.60, 0.16,
            0.79, 0.00, -16.33, 37.34, 109.80, 1.00
        ])
        _pos.apply_to(_cam)
        assert not blast._rig_in_cam(rig=_ref, cam=_cam)

        # Test remove rigs ui
        _dialog = remove_rigs.launch([_ref], exec_=False)
        assert len(_dialog.ui.List.all_items()) == 1
        assert len(_dialog.ui.List.selectedItems()) == 1
        _dialog.close()
예제 #2
0
    def test_frustrum_test_blast(self):
        from maya_psyhive.tools.frustrum_test_blast import blast, remove_rigs

        set_dev_mode(False)

        # Test frustrum test
        _ref = ref.obtain_ref(namespace='archer_TMP',
                              file_=_RIG_PATH,
                              class_=blast._Rig)
        assert isinstance(_ref, blast._Rig)
        _cam = hom.HFnCamera('persp')
        _pos = hom.HMatrix([
            0.95, 0.00, 0.31, 0.00, 0.08, 0.96, -0.26, 0.00, -0.29, 0.27, 0.92,
            0.00, -19.37, 25.40, 54.23, 1.00
        ])
        _pos.apply_to(_cam)
        assert blast._rig_in_cam(rig=_ref, cam=_cam)
        _pos = hom.HMatrix([
            0.80, -0.00, -0.60, 0.00, -0.10, 0.98, -0.13, 0.00, 0.60, 0.16,
            0.79, 0.00, -16.33, 37.34, 109.80, 1.00
        ])
        _pos.apply_to(_cam)
        assert not blast._rig_in_cam(rig=_ref, cam=_cam)

        # Test remove rigs ui
        _dialog = remove_rigs.launch([_ref], exec_=False)
        _dialog.close()
예제 #3
0
def _rand_m():
    _vals = list(hom.HMatrix())
    for _idx in range(16):
        if _idx in [3, 7, 11, 15]:
            continue
        _vals[_idx] = random.random()
    return hom.HMatrix(_vals)
예제 #4
0
    def prepareForDraw(self, obj, cam, frame_context, data, verbose=0):
        """Retrieve data cache (create if does not exist).

        Args:
            obj (MDagPath): path to object being drawn
            cam (MDagPath): path to viewport camera
            frame_context (MFrameContext): frame context
            data (MeshXRayerData): previous data
            verbose (int): print process data

        Returns:
            (MeshXRayerData): node data
        """
        lprint('PREPARE FOR DRAW', verbose=verbose)

        _data = data
        if not isinstance(_data, MeshXRayerData):
            _data = MeshXRayerData()
            lprint(' - USING EXISTING DATA', _data, verbose=verbose)
        lprint(' - DATA', _data, verbose=verbose)

        # Read in_mesh plug
        lprint(' - OBJ', obj, verbose=verbose)
        _node = obj.node()
        _in_mesh_plug = om.MPlug(_node, MeshXRayer.in_mesh)
        lprint(' - IN MESH PLUG', _in_mesh_plug, verbose=verbose)

        _data.mesh_tris.clear()
        if _in_mesh_plug.isNull:
            return None
        if _in_mesh_plug.asMDataHandle().type() != om.MFnData.kMesh:
            return None
        _in_mesh_handle = _in_mesh_plug.asMDataHandle().asMesh()
        _in_mesh = om.MFnMesh(_in_mesh_handle)

        # Read mesh triangles
        _mesh_pts = _in_mesh.getPoints()
        for _poly_id in range(_in_mesh.numPolygons):
            _vtx_ids = _in_mesh.getPolygonVertices(_poly_id)
            for _vtx_id in _vtx_ids[:3]:
                _data.mesh_tris.append(_mesh_pts[_vtx_id])
        lprint(' - IN MESH',
               _in_mesh,
               len(_in_mesh.getPoints()),
               verbose=verbose)

        # Read col/hide_angle + draw toggles
        _col_plug = om.MPlug(_node, MeshXRayer.color)
        _data.color = om.MColor(
            [_col_plug.child(_idx).asFloat() for _idx in range(3)])
        _data.hide_angle = om.MPlug(_node, MeshXRayer.hide_angle).asFloat()
        _data.draw_control = om.MPlug(_node, MeshXRayer.draw_control).asBool()
        _data.draw_mesh = om.MPlug(_node, MeshXRayer.draw_mesh).asBool()

        _obj_pos = hom.HMatrix(obj.inclusiveMatrix()).pos()
        _cam_pos = hom.HMatrix(cam.inclusiveMatrix()).pos()
        _data.mesh_to_cam = _cam_pos - _obj_pos

        return _data
예제 #5
0
    def test(self):

        _sphere1 = hom.CMDS.polySphere()
        _sphere2 = hom.CMDS.polySphere()
        _sphere3 = hom.CMDS.polySphere()

        # Test apply euler rotation to sphere
        _rot = hom.HEulerRotation(0, 0, math.pi)
        _rot.apply_to(_sphere1)
        assert cmds.getAttr(_sphere1.rotate) == [(0, 0, 180)]

        # Test apply euler rot as mtx to sphere
        hom.HMatrix().apply_to(_sphere1)
        _rot = hom.HEulerRotation(0, math.pi, 0)
        _mtx = _rot.as_mtx()
        cmds.xform(_sphere1, matrix=_mtx)
        assert cmds.getAttr(_sphere1.rotate) == [(0, 180, 0)]
        hom.HMatrix().apply_to(_sphere1)
        _mtx.apply_to(_sphere2)
        assert cmds.getAttr(_sphere2.rotate) == [(0, 180, 0)]

        # Test apply vector + rot as mtxs to sphere
        hom.HMatrix().apply_to(_sphere1)
        _mtx1 = hom.HVector(5, 10, 20).as_mtx()
        _mtx2 = hom.HEulerRotation(0, math.pi, 0).as_mtx()
        (_mtx2 * _mtx1).apply_to(_sphere1)
        print cmds.getAttr(_sphere1.rotate)
        assert cmds.getAttr(_sphere1.rotate) == [(0, 180, 0)]
        assert cmds.getAttr(_sphere1.translate) == [(5, 10, 20)]
        (_mtx1 * _mtx2).apply_to(_sphere1)
        assert cmds.getAttr(_sphere1.rotate) == [(0, 180, 0)]
        assert cmds.getAttr(_sphere1.translate) == [(-4.999999999999997, 10.0,
                                                     -20.0)]
        (_mtx1 * _mtx1).apply_to(_sphere1)
        assert cmds.getAttr(_sphere1.rotate) == [(0, 0, 0)]
        assert cmds.getAttr(_sphere1.translate) == [(10, 20, 40)]

        # Offset matrix cookbook
        _mtx_b = _rand_m()
        _mtx_a = _rand_m()
        _offs_a_to_b = _mtx_a.inverse() * _mtx_b
        assert _mtx_a * _offs_a_to_b == _mtx_b
        _mtx_b.apply_to(_sphere1)
        _mtx_a.apply_to(_sphere2)
        (_mtx_a * _offs_a_to_b).apply_to(_sphere3)
        assert hom.get_m(_sphere3) == _mtx_b

        # Test load/save preset
        _sphere2.tx.set_val(10)
        _tmp_preset = '{}/tmp.preset'.format(tempfile.gettempdir())
        assert (_sphere1.get_p() - _sphere2.get_p()).length()
        _sphere1.save_preset(_tmp_preset)
        print _tmp_preset
        _sphere2.load_preset(_tmp_preset)
        print(_sphere1.get_p() - _sphere2.get_p()).length()
        assert not round((_sphere1.get_p() - _sphere2.get_p()).length(), 5)
예제 #6
0
    def as_mtx(self):
        """Get this vector as a transformation matrix.

        Returns:
            (HMatrix): matrix
        """
        from maya_psyhive import open_maya as hom
        _vals = list(hom.HMatrix().to_tuple())
        for _idx in range(3):
            _vals[12+_idx] = self[_idx]
        return hom.HMatrix(_vals)
예제 #7
0
    def inclusive_matrix(self):
        """Get world space matrix for this dag object.

        Returns:
            (HMatrix): matrix
        """
        from maya_psyhive import open_maya as hom
        return hom.HMatrix(self.inclusiveMatrix())
예제 #8
0
    def apply_fk_to_ik(self, pole_vect_depth=10.0, apply_=True,
                       build_tmp_geo=False, verbose=1):
        """Apply fk to ik.

        First the pole vector is calculated by extending a line from the
        elbow joint in the direction of the cross product of the limb
        vector (fk_ctrls[0] to fk3) and the limb bend.

        The ik joint is then moved to the position of the fk3 control.

        The arm/knee offset is reset on apply.

        Args:
            pole_vect_depth (float): distance of pole vector from fk_ctrls[1]
            apply_ (bool): apply the update to gimbal ctrl
            build_tmp_geo (bool): build tmp geo
            verbose (int): print process data
        """
        lprint('APPLYING FK -> IK', verbose=verbose)

        # Reset offset
        for _offs in self.ik_offs:
            cmds.setAttr(_offs, 0)

        # Calculate pole pos
        _limb_v = hom.get_p(self.fk_ctrls[2]) - hom.get_p(self.fk_ctrls[0])
        if self.limb is Limb.ARM:
            _limb_bend = -hom.get_m(self.fk_ctrls[1]).ly_().normalized()
        elif self.limb == Limb.LEG:
            _limb_bend = hom.get_m(self.fk_ctrls[1]).lx_().normalized()
        else:
            raise ValueError(self.limb)
        _pole_dir = (_limb_v ^ _limb_bend).normalized()
        _pole_p = hom.get_p(self.fk_ctrls[1]) + _pole_dir*pole_vect_depth
        _pole_p.apply_to(self.ik_pole, use_constraint=True)

        # Read fk3 mtx
        _ik_mtx = hom.get_m(self.fk_ctrls[2])
        _side_offs = hom.HMatrix()
        if self.side == Side.RIGHT:
            _side_offs = hom.HEulerRotation(math.pi, 0, 0).as_mtx()
        _ik_mtx = _side_offs * _ik_mtx
        _ik_mtx.apply_to(self.ik_)

        # Apply vals to ik ctrls
        if apply_:
            self.set_to_ik()
            lprint('SET', self.ik_, 'TO IK', verbose=verbose)

        if build_tmp_geo:
            _limb_v.build_crv(hom.get_p(self.fk_ctrls[0]), name='limb_v')
            _limb_bend.build_crv(hom.get_p(self.fk_ctrls[1]), name='limb_bend')
            _pole_dir.build_crv(hom.get_p(self.fk_ctrls[1]), name='pole_dir')
            _pole_p.build_loc(name='pole')
            _ik_mtx.build_geo(name='fk3')
            hom.get_m(self.ik_).build_geo(name='ik')
예제 #9
0
    def _casted_result_fn(*args, **kwargs):

        import maya_psyhive.open_maya as hom

        _result = func(*args, **kwargs)
        lprint('CASTING RESULT', _result, verbose=verbose)
        if isinstance(_result, float):
            return _result
        elif isinstance(_result, om.MPoint):
            return hom.HPoint(_result)
        elif isinstance(_result, om.MVector):
            return hom.HVector(_result)
        elif isinstance(_result, om.MMatrix):
            return hom.HMatrix(_result)
        raise ValueError(_result)