Exemplo n.º 1
0
    def pickPoints(self):
        self.geo = self.geometry()
        self.hide()

        try:
            mxs.unregisterRedrawViewsCallback(self.debugView)
        except:
            pass

        try:
            self.pos1 = mxs.pickPoint(
                snap=mxs.readvalue(mxs.StringStream('#3D')))
            self.pos2 = mxs.pickPoint(
                snap=mxs.readvalue(mxs.StringStream('#3D')))

            self.moveVector = self.pos2 - self.pos1

            vector = self.moveVector

            X = mxs.normalize(vector)
            Z = mxs.point3(0, 0, 1)
            Y = mxs.normalize(mxs.cross(Z, X))
            Z = mxs.normalize(mxs.cross(X, Y))

            self.transform = mxs.matrix3(Z, Y, X, mxs.point3(0, 0, 0))

            mxs.WorkingPivot.SetTM(self.transform)
            mxs.execute("max tti")

            mxs.registerRedrawViewsCallback(self.debugView)
            mxs.execute("max move")
            mxs.execute("toolMode.coordsys #working_pivot")
            self.show()
            self.setGeometry(self.geo)

        except:
            print("Point Picking Cancelled")
def volume_based_transform(obj):
    """
    creates a transform matrix by running a SVD operation on the verts that make up the mesh

    :param type obj: a max node...
    :returns: a transform matrix
    :rtype: mxs.matrix3
    """
    mesh = mxs.snapshotAsMesh(obj)  # let's work in world space :D
    print('collecting verts')
    verts = [[mesh.verts[i].pos.x, mesh.verts[i].pos.y, mesh.verts[i].pos.z] for i in range(mesh.verts.count)]

    print('making convexhull')
    hull = ConvexHull(verts)
    center = hull.points.mean(axis=0)

    print('centering points ch points')
    hull_verts = [hull.points[vert] - center for vert in hull.vertices]

    print('working out svd')
    pcloud = matrix(hull_verts)
    _, _, vh = svd(pcloud)

    print('building matrix')
    xaxis, yaxis, zaxis = vh.tolist()

    xaxis = mxs.point3(*map(float, xaxis))
    yaxis = mxs.point3(*map(float, yaxis))
    zaxis = mxs.point3(*map(float, zaxis))

    transform = mxs.matrix3(mxs.normalize(xaxis),
                            mxs.normalize(yaxis),
                            mxs.normalize(zaxis),
                            mxs.point3(*map(float, center)))

    return transform
Exemplo n.º 3
0
def get_pole_vector_position(bone_a,
                             bone_b,
                             bone_c,
                             pole_vector_distance=1.0,
                             create_locator=False):
    """
    Calculates the correct pole vector position
    NOTE: Given nodes must be coplanar (must be on the same plane)
    :param bone_a:
    :param bone_b:
    :param bone_c:
    :param pole_vector_distance:
    :param create_locator:
    :return:
    """

    bone_a_pos = rt.Point3(*bone_a.position)
    bone_b_pos = rt.Point3(*bone_b.position)
    bone_c_pos = rt.Point3(*bone_c.position)

    # get initial vectors
    ab_vec = bone_b_pos - bone_a_pos
    ac_vec = bone_c_pos - bone_a_pos

    # calculate projection length
    ab_ac_dot = rt.dot(ab_vec, ac_vec)
    proj = float(ab_ac_dot) / float(rt.length(ac_vec))

    # calculate project vector by normalizing ac_vec and multiplying
    # that normalized vector by the projection length
    ac_vec_normalized = rt.normalize(ac_vec)
    proj_vec = ac_vec_normalized * proj

    # get the vector from the projected vector to the bone_b position
    b_proj_vec = bone_b_pos - (bone_a_pos + proj_vec)

    # calculate final pole vector position
    b_proj_vec *= pole_vector_distance
    final_pos = b_proj_vec + bone_b_pos

    if create_locator:
        test_point_helper = node_utils.create_point_helper(
            size=15.0, is_center_marker=False, is_box=True)
        rt.setProperty(test_point_helper, 'position', final_pos)
        return test_point_helper

    return final_pos
Exemplo n.º 4
0
def execute():

    # Define the target object
    my_target = mxs.selection[0]

    # Reset the pivot
    my_target.pivot = my_target.center

    # Define the grid object
    my_grid = mxs.grid()

    # Define the vector
    vector = my_target.transform.position - mxs.inverse(
        mxs.viewport.getTM()).position

    # Define the length of the target
    distance = (mxs.length(my_target.min - my_target.max) / 2)

    # Normalize + UP vector (Z)
    X = mxs.normalize(vector)
    Z = mxs.point3(0, 0, 1)
    Y = mxs.normalize(mxs.cross(Z, X))
    Z = mxs.normalize(mxs.cross(X, Y))

    # Define a new transform based on vector
    my_transform = mxs.matrix3(Z, Y, X, my_target.transform.position)

    # Edit the position of the transform
    new_position = my_transform.position - (X * distance)
    my_transform = mxs.matrix3(Z, Y, X, new_position)

    # Assign the transform to the grid
    my_grid.transform = my_transform

    # Activate the grid
    mxs.activegrid = my_grid

    # Define spline method
    def setMode(name):
        name.curveType = 1

    try:
        # Set iso view
        mxs.execute("max vpt iso user")

        # Define user draw curves
        curves = mxs.startObjectCreation(mxs.FreehandSpline,
                                         returnNewNodes=True,
                                         newNodeCallback=setMode)

        # Define modifiers
        noise_mod = mxs.Noisemodifier()
        quad_mod = mxs.Quadify_Mesh()
        extrude_mod = mxs.Extrude()

        # Change the parameters
        extrude_mod.amount = distance * 2
        quad_mod.quadsize = 2
        noise_mod.scale = 10
        noise_mod.fractal = True
        noise_mod.strength = mxs.point3(2, 10, 2)

        # Add the modifiers
        mxs.addmodifier(curves, extrude_mod)
        mxs.addmodifier(curves, quad_mod)
        mxs.addmodifier(curves, noise_mod)

        # Define cutter splines
        mxs.ProCutter.CreateCutter(curves, 1, True, True, False, True, True)

        # Define stock object
        mxs.ProCutter.AddStocks(curves[0], my_target, 1, 1)

        # Set perspective view
        mxs.execute("max vpt persp user")

        # Deactivate and delete the grid
        mxs.activegrid = None
        mxs.delete(my_grid)

    except:

        # Set perspective view
        mxs.execute("max vpt persp user")

        # Deactivate and delete the grid
        mxs.activegrid = None
        mxs.delete(my_grid)

        print("cancled")