Example #1
0
        public void ResetCamera()
        {
            //<PerspectiveCamera UpDirection="0,1,0" Position="1,1,1" LookDirection="-1,-1,-1" FieldOfView="45" />
            MDagPath cameraPath;

            try {
                // Try with a Maya host first
                cameraPath = M3dView.active3dView.Camera;
            } catch {
                // We are in standalone mode (WPF application)
                MSelectionList list = new MSelectionList();
                list.add("persp");
                cameraPath = new MDagPath();
                list.getDagPath(0, cameraPath);
            }

            MFnCamera fnCamera = new MFnCamera(cameraPath);
            MPoint    eyePoint = fnCamera.eyePoint(MSpace.Space.kWorld);
            MPoint    centerOfInterestPoint = fnCamera.centerOfInterestPoint(MSpace.Space.kWorld);
            MVector   direction             = centerOfInterestPoint.minus(eyePoint);
            MVector   upDirection           = fnCamera.upDirection(MSpace.Space.kWorld);

            camera.Position      = new Point3D(eyePoint.x, eyePoint.y, eyePoint.z);
            camera.LookDirection = new Vector3D(direction.x, direction.y, direction.z);
            MAngle fieldOfView = new MAngle(fnCamera.verticalFieldOfView);              //verticalFieldOfView / horizontalFieldOfView

            camera.FieldOfView       = fieldOfView.asDegrees;
            camera.UpDirection       = new Vector3D(upDirection.x, upDirection.y, upDirection.z);
            camera.NearPlaneDistance = fnCamera.nearClippingPlane;
            camera.FarPlaneDistance  = fnCamera.farClippingPlane;
            camera.Transform         = new Transform3DGroup();
            (camera.Transform as Transform3DGroup).Children.Add(new TranslateTransform3D(new Vector3D()));
        }
Example #2
0
        public void ResetLights()
        {
            //<AmbientLight Color="White" />
            //<DirectionalLight Color="White" Direction="-1,-1,-1" />
            //<PointLight Color="White" ConstantAttenuation="1" LinearAttenuation="1" Position="0,0,0" QuadraticAttenuation="1" Range="0" />
            //<SpotLight Color="White" ConstantAttenuation="1" Direction="-1,-1,-1" InnerConeAngle="10" LinearAttenuation="1" OuterConeAngle="10" Position="0,0,0" QuadraticAttenuation="1" Range="0" />
            lights.Children.Clear();

            MItDag dagIterator = new MItDag(MItDag.TraversalType.kDepthFirst, MFn.Type.kLight);

            for ( ; !dagIterator.isDone; dagIterator.next())
            {
                MDagPath lightPath = new MDagPath();
                dagIterator.getPath(lightPath);

                MFnLight light     = new MFnLight(lightPath);
                bool     isAmbient = light.lightAmbient;
                MColor   mcolor    = light.color;
                Color    color     = Color.FromScRgb(1.0f, mcolor.r, mcolor.g, mcolor.b);
                if (isAmbient)
                {
                    AmbientLight ambient = new AmbientLight(color);
                    lights.Children.Add(ambient);
                    continue;
                }

                MFloatVector lightDirection = light.lightDirection(0, MSpace.Space.kWorld);
                Vector3D     direction      = new Vector3D(lightDirection.x, lightDirection.y, lightDirection.z);
                bool         isDiffuse      = light.lightDiffuse;
                try {
                    MFnDirectionalLight dirLight    = new MFnDirectionalLight(lightPath);
                    DirectionalLight    directional = new DirectionalLight(color, direction);
                    lights.Children.Add(directional);
                    continue;
                } catch {
                }

                MObject               transformNode = lightPath.transform;
                MFnDagNode            transform     = new MFnDagNode(transformNode);
                MTransformationMatrix matrix        = new MTransformationMatrix(transform.transformationMatrix);
                double []             threeDoubles  = new double [3];
                int rOrder = 0;                 //MTransformationMatrix.RotationOrder rOrder ;
                matrix.getRotation(threeDoubles, out rOrder, MSpace.Space.kWorld);
                matrix.getScale(threeDoubles, MSpace.Space.kWorld);
                MVector pos      = matrix.getTranslation(MSpace.Space.kWorld);
                Point3D position = new Point3D(pos.x, pos.y, pos.z);
                try {
                    MFnPointLight pointLight = new MFnPointLight(lightPath);
                    PointLight    point      = new PointLight(color, position);
                    //point.ConstantAttenuation =pointLight. ; // LinearAttenuation / QuadraticAttenuation
                    //point.Range =pointLight.rayDepthLimit ;
                    lights.Children.Add(point);
                    continue;
                } catch {
                }

                try {
                    MFnSpotLight spotLight      = new MFnSpotLight(lightPath);
                    MAngle       InnerConeAngle = new MAngle(spotLight.coneAngle);
                    MAngle       OuterConeAngle = new MAngle(spotLight.penumbraAngle);
                    SpotLight    spot           = new SpotLight(color, position, direction, OuterConeAngle.asDegrees, InnerConeAngle.asDegrees);
                    spot.ConstantAttenuation = spotLight.dropOff;                     // LinearAttenuation / QuadraticAttenuation
                    //spot.Range =spotLight.rayDepthLimit ;
                    lights.Children.Add(spot);
                    continue;
                } catch {
                }
            }
        }
Example #3
0
        public void ResetLights()
        {
            //<AmbientLight Color="White" />
            //<DirectionalLight Color="White" Direction="-1,-1,-1" />
            //<PointLight Color="White" ConstantAttenuation="1" LinearAttenuation="1" Position="0,0,0" QuadraticAttenuation="1" Range="0" />
            //<SpotLight Color="White" ConstantAttenuation="1" Direction="-1,-1,-1" InnerConeAngle="10" LinearAttenuation="1" OuterConeAngle="10" Position="0,0,0" QuadraticAttenuation="1" Range="0" />
            lights.Children.Clear () ;

            MItDag dagIterator =new MItDag (MItDag.TraversalType.kDepthFirst, MFn.Type.kLight) ;
            for ( ; !dagIterator.isDone ; dagIterator.next () ) {
                MDagPath lightPath =new MDagPath () ;
                dagIterator.getPath (lightPath) ;

                MFnLight light =new MFnLight (lightPath) ;
                bool isAmbient =light.lightAmbient ;
                MColor mcolor =light.color ;
                Color color =Color.FromScRgb (1.0f, mcolor.r, mcolor.g, mcolor.b) ;
                if ( isAmbient ) {
                    AmbientLight ambient =new AmbientLight (color) ;
                    lights.Children.Add (ambient) ;
                    continue ;
                }

                MFloatVector lightDirection =light.lightDirection (0, MSpace.Space.kWorld) ;
                Vector3D direction =new Vector3D (lightDirection.x, lightDirection.y, lightDirection.z) ;
                bool isDiffuse =light.lightDiffuse ;
                try {
                    MFnDirectionalLight dirLight =new MFnDirectionalLight (lightPath) ;
                    DirectionalLight directional =new DirectionalLight (color, direction) ;
                    lights.Children.Add (directional) ;
                    continue ;
                } catch {
                }

                MObject transformNode =lightPath.transform ;
                MFnDagNode transform =new MFnDagNode (transformNode) ;
                MTransformationMatrix matrix =new MTransformationMatrix (transform.transformationMatrix) ;
                double [] threeDoubles =new double [3] ;
                int rOrder =0 ; //MTransformationMatrix.RotationOrder rOrder ;
                matrix.getRotation (threeDoubles, out rOrder, MSpace.Space.kWorld) ;
                matrix.getScale (threeDoubles, MSpace.Space.kWorld) ;
                MVector pos =matrix.getTranslation (MSpace.Space.kWorld) ;
                Point3D position =new Point3D (pos.x, pos.y, pos.z) ;
                try {
                    MFnPointLight pointLight =new MFnPointLight (lightPath) ;
                    PointLight point =new PointLight (color, position) ;
                    //point.ConstantAttenuation =pointLight. ; // LinearAttenuation / QuadraticAttenuation
                    //point.Range =pointLight.rayDepthLimit ;
                    lights.Children.Add (point) ;
                    continue ;
                } catch {
                }

                try {
                    MFnSpotLight spotLight =new MFnSpotLight (lightPath) ;
                    MAngle InnerConeAngle =new MAngle (spotLight.coneAngle) ;
                    MAngle OuterConeAngle =new MAngle (spotLight.penumbraAngle) ;
                    SpotLight spot =new SpotLight (color, position, direction, OuterConeAngle.asDegrees, InnerConeAngle.asDegrees) ;
                    spot.ConstantAttenuation =spotLight.dropOff ; // LinearAttenuation / QuadraticAttenuation
                    //spot.Range =spotLight.rayDepthLimit ;
                    lights.Children.Add (spot) ;
                    continue ;
                } catch {
                }
            }
        }
Example #4
0
        //
        //    Description:
        //        Converts the tangent angles from Maya 3.0 to Maya2.* formats.
        //
        protected void convertAnglesAndWeights3To2(MFnAnimCurve.AnimCurveType type,
            bool isWeighted, ref MAngle angle, ref double weight)
        {
            double oldAngle = angle.asRadians;
            double newAngle = oldAngle;

            //	Calculate the scale values for the conversion.
            //
            double xScale = 1.0;
            double yScale = 1.0;

            MTime tOne = new MTime(1.0, MTime.Unit.kSeconds);
            if (type == MFnAnimCurve.AnimCurveType.kAnimCurveTT ||
                type == MFnAnimCurve.AnimCurveType.kAnimCurveTL ||
                type == MFnAnimCurve.AnimCurveType.kAnimCurveTA ||
                type == MFnAnimCurve.AnimCurveType.kAnimCurveTU) {

                xScale = tOne.asUnits(MTime.uiUnit);
            }

            switch (type)
            {
                case MFnAnimCurve.AnimCurveType.kAnimCurveTT:
                case MFnAnimCurve.AnimCurveType.kAnimCurveUT:
                    yScale = tOne.asUnits(MTime.uiUnit);
                    break;
                case MFnAnimCurve.AnimCurveType.kAnimCurveTL:
                case MFnAnimCurve.AnimCurveType.kAnimCurveUL:
                    {
                        MDistance dOne = new MDistance(1.0, MDistance.internalUnit);
                        yScale = dOne.asUnits(linearUnit);
                    }
                    break;
                case MFnAnimCurve.AnimCurveType.kAnimCurveTA:
                case MFnAnimCurve.AnimCurveType.kAnimCurveUA:
                    {
                        MAngle aOne = new MAngle(1.0, MAngle.internalUnit);
                        yScale = aOne.asUnits(angularUnit);
                    }
                    break;
                case MFnAnimCurve.AnimCurveType.kAnimCurveTU:
                case MFnAnimCurve.AnimCurveType.kAnimCurveUU:
                default:
                    break;
            }

            double tanAngle = Math.Tan(oldAngle);
            newAngle = Math.Atan((xScale*tanAngle)/yScale);

            if (isWeighted)
            {
                double sinAngle = Math.Sin(oldAngle);
                double cosAngle = Math.Cos(oldAngle);

                double denominator = (yScale*yScale*sinAngle*sinAngle) +
                                        (xScale*xScale*cosAngle*cosAngle);
                weight = Math.Sqrt(weight/denominator);
            }

            MAngle finalAngle = new MAngle(newAngle, MAngle.Unit.kRadians);
            angle = finalAngle;
        }
Example #5
0
        public void ResetCamera()
        {
            //<PerspectiveCamera UpDirection="0,1,0" Position="1,1,1" LookDirection="-1,-1,-1" FieldOfView="45" />
            MDagPath cameraPath ;
            try {
                // Try with a Maya host first
                cameraPath =M3dView.active3dView.Camera ;
            } catch {
                // We are in standalone mode (WPF application)
                MSelectionList list =new MSelectionList () ;
                list.add ("persp") ;
                cameraPath =new MDagPath () ;
                list.getDagPath (0, cameraPath) ;
            }

            MFnCamera fnCamera =new MFnCamera (cameraPath) ;
            MPoint eyePoint =fnCamera.eyePoint (MSpace.Space.kWorld) ;
            MPoint centerOfInterestPoint =fnCamera.centerOfInterestPoint (MSpace.Space.kWorld) ;
            MVector direction =centerOfInterestPoint.minus (eyePoint) ;
            MVector upDirection =fnCamera.upDirection (MSpace.Space.kWorld) ;

            camera.Position =new Point3D (eyePoint.x, eyePoint.y, eyePoint.z) ;
            camera.LookDirection =new Vector3D (direction.x, direction.y, direction.z) ;
            MAngle fieldOfView =new MAngle (fnCamera.verticalFieldOfView) ; //verticalFieldOfView / horizontalFieldOfView
            camera.FieldOfView =fieldOfView.asDegrees ;
            camera.UpDirection =new Vector3D (upDirection.x, upDirection.y, upDirection.z) ;
            camera.NearPlaneDistance =fnCamera.nearClippingPlane ;
            camera.FarPlaneDistance =fnCamera.farClippingPlane ;
            camera.Transform =new Transform3DGroup () ;
            (camera.Transform as Transform3DGroup).Children.Add (new TranslateTransform3D (new Vector3D ())) ;
        }
Example #6
0
        //    Description:
        //        Write out the anim curve from the clipboard item into the
        //        stream. The actual anim curve data is written out.
        //
        //        This method returns true if the write was successful.
        //
        protected void writeAnimCurve(ref StreamWriter clip, 
            MObject animCurveObj,
            MFnAnimCurve.AnimCurveType type,
            bool verboseUnits = false)
        {
            if (clip == null)
            {
                throw new ArgumentNullException( "clip" );
            }

            if (null == animCurveObj || animCurveObj.isNull)
            {
                throw new ArgumentNullException( "animCurveObj" );
            }

            MFnAnimCurve animCurve = new MFnAnimCurve(animCurveObj);

            clip.Write(kAnimData + kSpaceChar + kBraceLeftChar + Environment.NewLine);

            clip.Write(kTwoSpace + kInputString + kSpaceChar +
                    boolInputTypeAsWord(animCurve.isUnitlessInput) +
                    kSemiColonChar + Environment.NewLine);

            clip.Write( kTwoSpace + kOutputString + kSpaceChar +
                    outputTypeAsWord(type) + kSemiColonChar + Environment.NewLine);

            clip.Write(kTwoSpace + kWeightedString + kSpaceChar +
                    Convert.ToString(animCurve.isWeighted ? 1 : 0) + kSemiColonChar +Environment.NewLine);

            //	These units default to the units in the header of the file.
            //
            if (verboseUnits)
            {
                clip.Write(kTwoSpace + kInputUnitString + kSpaceChar);
                if (animCurve.isTimeInput)
                {
                    string unitName = "";
                    animUnitNames.setToShortName(timeUnit, ref unitName);
                    clip.Write(unitName);
                }
                else
                {
                    //	The anim curve has unitless input.
                    //
                    clip.Write(animUnitNames.kUnitlessString);
                }

                clip.Write(kSemiColonChar + Environment.NewLine);

                clip.Write(kTwoSpace + kOutputUnitString + kSpaceChar);
            }

            double conversion = 1.0;
            switch (type)
            {
                case MFnAnimCurve.AnimCurveType.kAnimCurveTA:
                case MFnAnimCurve.AnimCurveType.kAnimCurveUA:
                    {
                        string unitName = "";
                        animUnitNames.setToShortName(angularUnit, ref unitName);
                        if (verboseUnits) clip.Write(unitName);
                        {
                            MAngle angle = new MAngle(1.0);
                            conversion = angle.asUnits(angularUnit);
                        }
                        break;
                    }
                case MFnAnimCurve.AnimCurveType.kAnimCurveTL:
                case MFnAnimCurve.AnimCurveType.kAnimCurveUL:
                    {
                        string unitName = "";
                        animUnitNames.setToShortName(linearUnit, ref unitName);
                        if (verboseUnits) clip.Write(unitName);
                        {
                            MDistance distance = new MDistance(1.0);
                            conversion = distance.asUnits(linearUnit);
                        }
                        break;
                    }
                case MFnAnimCurve.AnimCurveType.kAnimCurveTT:
                case MFnAnimCurve.AnimCurveType.kAnimCurveUT:
                    {
                        string unitName = "";
                        animUnitNames.setToShortName(timeUnit, ref unitName);
                        if (verboseUnits) clip.Write(unitName);
                        break;
                    }
                default:
                    if (verboseUnits) clip.Write(animUnitNames.kUnitlessString);
                    break;
            }

            if (verboseUnits) clip.Write(kSemiColonChar +Environment.NewLine);

            if (verboseUnits)
            {
                string angleUnitName = "";
                animUnitNames.setToShortName(angularUnit, ref angleUnitName);
                clip.Write(kTwoSpace + kTanAngleUnitString +
                        kSpaceChar + angleUnitName + kSemiColonChar + Environment.NewLine);
            }

            clip.Write(kTwoSpace + kPreInfinityString + kSpaceChar +
                    infinityTypeAsWord(animCurve.preInfinityType) +
                    kSemiColonChar + Environment.NewLine);

            clip.Write(kTwoSpace + kPostInfinityString + kSpaceChar +
                    infinityTypeAsWord(animCurve.postInfinityType) +
                    kSemiColonChar + Environment.NewLine);

            clip.Write(kTwoSpace + kKeysString + kSpaceChar + kBraceLeftChar + Environment.NewLine);

            // And then write out each keyframe
            //
            uint numKeys = animCurve.numKeyframes;
            for (uint i = 0; i < numKeys; i++)
            {
                clip.Write(kTwoSpace + kTwoSpace);
                if (animCurve.isUnitlessInput)
                {
                    clip.Write(animCurve.unitlessInput(i));
                }
                else
                {
                    clip.Write(animCurve.time(i).value);
                }

                // clamp tiny values so that it isn't so small it can't be read in
                //
                double animValue = (conversion*animCurve.value(i));
                if (isEquivalent(animValue,0.0))
                    animValue = 0.0;
                clip.Write(kSpaceChar + Convert.ToString(animValue));

                clip.Write(kSpaceChar + tangentTypeAsWord(animCurve.inTangentType(i)) );
                clip.Write(kSpaceChar + tangentTypeAsWord(animCurve.outTangentType(i)) );

                clip.Write( kSpaceChar + Convert.ToString(animCurve.tangentsLocked(i) ? 1 : 0));
                clip.Write(kSpaceChar + Convert.ToString(animCurve.weightsLocked(i) ? 1 : 0));
                clip.Write(kSpaceChar + Convert.ToString(animCurve.isBreakdown(i) ? 1 : 0));

                if (animCurve.inTangentType(i) == MFnAnimCurve.TangentType.kTangentFixed)
                {
                    MAngle angle = new MAngle();
                    double weight = 0.0;
                    animCurve.getTangent(i, angle, ref weight, true);

                    clip.Write(kSpaceChar + Convert.ToString(angle.asUnits(angularUnit)));
                    clip.Write(kSpaceChar + Convert.ToString(weight));
                }
                if (animCurve.outTangentType(i) == MFnAnimCurve.TangentType.kTangentFixed)
                {
                    MAngle angle = new MAngle();
                    double weight = 0.0;
                    animCurve.getTangent(i, angle, ref weight, false);

                    clip.Write(kSpaceChar +  Convert.ToString(angle.asUnits(angularUnit)));
                    clip.Write(kSpaceChar + Convert.ToString(weight));
                }

                clip.Write(kSemiColonChar + Environment.NewLine);
            }
            clip.Write(kTwoSpace + kBraceRightChar + Environment.NewLine);

            clip.Write(kBraceRightChar + Environment.NewLine);
        }
Example #7
0
        //
        //    Description:
        //        Converts the tangent angles from Maya 2.* to Maya3.0+ formats.
        //
        protected void convertAnglesAndWeights2To3(MFnAnimCurve.AnimCurveType type,
            bool isWeighted, ref MAngle angle, ref double weight)
        {
            double oldAngle = angle.asRadians;

            double newAngle = oldAngle;
            double newWeight = weight;

            //	Calculate the scale values for the conversion.
            //
            double xScale = 1.0;
            double yScale = 1.0;

            MTime tOne = new MTime(1.0, MTime.Unit.kSeconds);
            if (type == MFnAnimCurve.AnimCurveType.kAnimCurveTT ||
                type == MFnAnimCurve.AnimCurveType.kAnimCurveTL ||
                type == MFnAnimCurve.AnimCurveType.kAnimCurveTA ||
                type == MFnAnimCurve.AnimCurveType.kAnimCurveTU) {

                xScale = tOne.asUnits(MTime.uiUnit);
            }

            switch (type)
            {
                case MFnAnimCurve.AnimCurveType.kAnimCurveTT:
                case MFnAnimCurve.AnimCurveType.kAnimCurveUT:
                    yScale = tOne.asUnits(MTime.uiUnit);
                    break;
                case MFnAnimCurve.AnimCurveType.kAnimCurveTL:
                case MFnAnimCurve.AnimCurveType.kAnimCurveUL:
                    {
                        MDistance dOne = new MDistance(1.0, MDistance.internalUnit);
                        yScale = dOne.asUnits(linearUnit);
                    }
                    break;
                case MFnAnimCurve.AnimCurveType.kAnimCurveTA:
                case MFnAnimCurve.AnimCurveType.kAnimCurveUA:
                    {
                        MAngle aOne = new MAngle(1.0, MAngle.internalUnit);
                        yScale = aOne.asUnits(angularUnit);
                    }
                    break;
                case MFnAnimCurve.AnimCurveType.kAnimCurveTU:
                case MFnAnimCurve.AnimCurveType.kAnimCurveUU:
                default:
                    break;
            }

            const double quarter = Math.PI/2;
            if (isEquivalent(oldAngle, 0.0) ||
                isEquivalent(oldAngle, quarter) ||
                isEquivalent(oldAngle, -quarter)) {

                newAngle = oldAngle;

                if (isWeighted) {
                    newWeight = yScale*oldAngle;
                }
            } else {
                double tanAngle = Math.Tan(oldAngle);
                newAngle = Math.Atan((yScale*tanAngle)/xScale);

                if (isWeighted) {
                    double cosAngle = Math.Cos(oldAngle);
                    double cosSq = cosAngle*cosAngle;

                    double wSq = (weight*weight) *
                            (((xScale*xScale - yScale*yScale)*cosSq) + (yScale*yScale));

                    newWeight = Math.Sqrt(wSq);
                }
            }

            weight = newWeight;

            MAngle finalAngle = new MAngle(newAngle, MAngle.Unit.kRadians);
            angle = finalAngle;
        }
Example #8
0
 //
 //    Description:
 //        Sets the string with the short text name of the angle unit.
 //
 public static void setToShortName( MAngle.Unit unit, ref string name)
 {
     switch(unit) {
         case MAngle.Unit.kDegrees:
             name = kDegString;
             break;
         case MAngle.Unit.kRadians:
             name = kRadString;
             break;
         case MAngle.Unit.kAngMinutes:
             name = kMinString;
             break;
         case MAngle.Unit.kAngSeconds:
             name = kSecString;
             break;
         default:
             name = kUnknownAngularString;
             break;
     }
 }
Example #9
0
        //
        //    Description:
        //        The angle unit is set based on the passed string. If the string
        //        is not recognized, the angle unit is set to MAngle::kInvalid.
        //
        /* static */
        public static bool setFromName(string str, ref MAngle.Unit unit)
        {
            bool state = true;

            if(string.Compare(str,kDegString) == 0 || string.Compare(str,kDegLString) == 0)
                unit = MAngle.Unit.kDegrees;
            else if( string.Compare(str,kRadString) == 0 || string.Compare(str,kRadLString) == 0)
                unit = MAngle.Unit.kRadians;
            else if( string.Compare(str,kMinString) == 0 || string.Compare(str,kMinLString) == 0 )
                unit = MAngle.Unit.kAngMinutes;
            else if (string.Compare(str, kSecString) == 0 || string.Compare(str, kSecLString) == 0)
                unit = MAngle.Unit.kAngSeconds;
               else
            {
                //	This is not a recognized angular unit.
                //
                unit = MAngle.Unit.kInvalid;
                // Use format to place variable string into message
                string msgFmt = MStringResource.getString(RegisterMStringResources.kInvalidAngleUnits);
                string msg = string.Format(msgFmt, str);
                MGlobal.displayError(msg);
                state = false;
            }

            return state;
        }
Example #10
0
        //    Description:
        //        Read a block of the stream that should contain anim curve
        //        data in the format determined by the animData keyword.
        //
        protected bool readAnimCurve(ref StreamReaderExt clipFile, ref MAnimCurveClipboardItem item)
        {
            MFnAnimCurve animCurve = new MFnAnimCurve();
            MObject animCurveObj = new MObject();

            //	Anim curve defaults.
            //
            animBase.AnimBaseType input = wordAsInputType(kWordTypeTime);
            animBase.AnimBaseType output = wordAsOutputType(kWordTypeLinear);
            MFnAnimCurve.InfinityType preInf = wordAsInfinityType(kWordConstant);
            MFnAnimCurve.InfinityType postInf = wordAsInfinityType(kWordConstant);

            string inputUnitName = "";
            animUnitNames.setToShortName(timeUnit, ref inputUnitName);
            string outputUnitName = "";
            MAngle.Unit tanAngleUnit = angularUnit;
            bool isWeighted = false;

            string dataType = "";

            while (!clipFile.EndOfStream)
            {
                advance(ref clipFile);

                dataType = asWord(ref clipFile);

                if (string.Compare(dataType, kInputString) == 0) {
                    input = wordAsInputType(asWord(ref clipFile));
                } else if (string.Compare(dataType, kOutputString) == 0) {
                    output = wordAsOutputType(asWord(ref clipFile));
                } else if (string.Compare(dataType, kWeightedString) == 0) {
                    isWeighted = (asDouble(ref clipFile) == 1.0);
                } else if (string.Compare(dataType, kPreInfinityString) == 0) {
                    preInf = wordAsInfinityType(asWord(ref clipFile));
                } else if (string.Compare(dataType, kPostInfinityString) == 0) {
                    postInf = wordAsInfinityType(asWord(ref clipFile));
                } else if (string.Compare(dataType, kInputUnitString) == 0) {
                    inputUnitName = asWord(ref clipFile);
                } else if (string.Compare(dataType, kOutputUnitString) == 0) {
                    outputUnitName = asWord(ref clipFile);
                } else if (string.Compare(dataType, kTanAngleUnitString) == 0) {
                    string tUnit = asWord(ref clipFile);
                    if (!animUnitNames.setFromName(tUnit, ref tanAngleUnit)) {
                        string unitName="";
                        tanAngleUnit = angularUnit;
                        animUnitNames.setToShortName(tanAngleUnit, ref unitName);

                        // Use format to place variable string into message
                        string msgFmt = MStringResource.getString(RegisterMStringResources.kSettingTanAngleUnit);
                        string msg = string.Format(msgFmt, unitName);
                        MGlobal.displayError(msg);
                    }
                } else if (string.Compare(dataType, kKeysString) == 0) {
                    //	Ignore the rest of this line.
                    //
                    clipFile.ReadLine();
                    break;
                } else if (string.Compare(dataType, "{") == 0) {
                    //	Skippping the '{' character. Just ignore it.
                    //
                    continue;
                } else {
                    //	An unrecogized keyword was found.
                    //
                    string warnStr = (dataType);
                    // Use format to place variable string into message
                    string msgFmt = MStringResource.getString(RegisterMStringResources.kUnknownKeyword);
                    string msg = string.Format(msgFmt, warnStr);
                    MGlobal.displayError(msg);
                    continue;
                }
            }

            // Read the animCurve
            //
            MFnAnimCurve.AnimCurveType type = typeAsAnimCurveType(input, output);
            try
            {
                animCurveObj = animCurve.create(type,null);
            }
            catch (System.Exception )
            {
                string msg = MStringResource.getString(RegisterMStringResources.kCouldNotCreateAnim);
                MGlobal.displayError(msg);
                return false;
            }

            animCurve.setIsWeighted(isWeighted);
            animCurve.setPreInfinityType(preInf);
            animCurve.setPostInfinityType(postInf);

            //	Set the appropriate units.
            //
            MTime.Unit inputTimeUnit = MTime.Unit.kInvalid;
            if (input == AnimBaseType.kAnimBaseTime)
            {
                if (!animUnitNames.setFromName(inputUnitName, ref inputTimeUnit))
                {
                    string unitName = "";
                    inputTimeUnit = timeUnit;
                    animUnitNames.setToShortName(inputTimeUnit, ref unitName);
                    // Use format to place variable string into message
                    string msgFmt = MStringResource.getString(RegisterMStringResources.kSettingToUnit);
                    string msg = string.Format(msgFmt, kInputUnitString, unitName);
                    MGlobal.displayWarning(msg);
                }
            }

            MTime.Unit outputTimeUnit= MTime.Unit.kInvalid;
            if (output == AnimBaseType.kAnimBaseTime)
            {
                if (!animUnitNames.setFromName(outputUnitName, ref outputTimeUnit))
                {
                    string unitName = "";
                    outputTimeUnit = timeUnit;
                    animUnitNames.setToShortName(outputTimeUnit, ref unitName);
                    // Use format to place variable string into message
                    string msgFmt = MStringResource.getString(RegisterMStringResources.kSettingToUnit);
                    string msg = string.Format(msgFmt, kOutputUnitString, unitName);
                    MGlobal.displayWarning(msg);
                }
            }

            uint index = 0;
            double conversion = 1.0;
            if (output == AnimBaseType.kAnimBaseLinear)
            {
                MDistance.Unit unit = MDistance.Unit.kInvalid;
                if (outputUnitName.Length != 0)
                {
                    if (!animUnitNames.setFromName(outputUnitName, ref unit))
                    {
                        string unitName = "";
                        unit = linearUnit;
                        animUnitNames.setToShortName(unit, ref unitName);
                        // Use format to place variable string into message
                        string msgFmt = MStringResource.getString(RegisterMStringResources.kSettingToUnit);
                        string msg = string.Format(msgFmt, kOutputUnitString, unitName);
                        MGlobal.displayWarning(msg);
                    }
                }
                else
                    unit = linearUnit;

                if (unit != MDistance.Unit.kCentimeters)
                {
                    MDistance one = new MDistance(1.0, unit);
                    conversion = one.asCentimeters;
                }
            }
            else if (output == AnimBaseType.kAnimBaseAngular)
            {
                MAngle.Unit unit = MAngle.Unit.kInvalid;
                if (outputUnitName.Length != 0)
                {
                    if (!animUnitNames.setFromName(outputUnitName, ref unit))
                    {
                        string unitName = "";
                        unit = angularUnit;
                        animUnitNames.setToShortName(unit, ref unitName);
                        // Use format to place variable string into message
                        string msgFmt = MStringResource.getString(RegisterMStringResources.kSettingToUnit);
                        string msg = string.Format(msgFmt, kOutputUnitString, unitName);
                        MGlobal.displayWarning(msg);
                    }
                }
                else
                    unit = angularUnit;

                if (unit != MAngle.Unit.kRadians)
                {
                    MAngle one = new MAngle(1.0, unit);
                    conversion = one.asRadians;
                }
            }

            // Now read each keyframe
            //
            advance(ref clipFile);
            char c = (char)clipFile.Peek();
            while (!clipFile.EndOfStream && c != kBraceRightChar)
            {
                double t = asDouble(ref clipFile);
                double val = asDouble(ref clipFile);

                MFnAnimCurve.TangentType tanIn = wordAsTangentType(asWord(ref clipFile));
                MFnAnimCurve.TangentType tanOut = wordAsTangentType(asWord(ref clipFile));

                switch (type)
                {
                    case MFnAnimCurve.AnimCurveType.kAnimCurveTT:
                        index = animCurve.addKey(	new MTime(val, inputTimeUnit),
                                                    new MTime(val, outputTimeUnit),
                                                    tanIn, tanOut, null);
                        break;
                    case MFnAnimCurve.AnimCurveType.kAnimCurveTL:
                    case MFnAnimCurve.AnimCurveType.kAnimCurveTA:
                    case MFnAnimCurve.AnimCurveType.kAnimCurveTU:
                        index = animCurve.addKey(	new MTime(t, inputTimeUnit),
                                                    val*conversion, tanIn, tanOut,
                                                    null);
                        break;
                    case MFnAnimCurve.AnimCurveType.kAnimCurveUL:
                    case MFnAnimCurve.AnimCurveType.kAnimCurveUA:
                    case MFnAnimCurve.AnimCurveType.kAnimCurveUU:
                        index = animCurve.addKey(	t, val*conversion,
                                                    tanIn, tanOut,
                                                    null);
                        break;
                    case MFnAnimCurve.AnimCurveType.kAnimCurveUT:
                        index = animCurve.addKey(	t, new MTime(val, outputTimeUnit),
                                                    tanIn, tanOut,
                                                    null);
                        break;
                    default:
                        string msg = MStringResource.getString(RegisterMStringResources.kUnknownNode);
                        MGlobal.displayError(msg);
                        return false;
                }

                //	Tangent locking needs to be called after the weights and
                //	angles are set for the fixed tangents.
                //
                bool tLocked = asDouble(ref clipFile) == 1.0;
                bool swLocked = asDouble(ref clipFile) == 1.0;
                bool isBreakdown = false;
                if (animVersion >= kVersionNonWeightedAndBreakdowns)
                {
                    isBreakdown = (asDouble(ref clipFile) == 1.0);
                }

                //	Only fixed tangents need additional information.
                //
                if (tanIn == MFnAnimCurve.TangentType.kTangentFixed)
                {
                    MAngle inAngle = new MAngle(asDouble(ref clipFile), tanAngleUnit);
                    double inWeight = asDouble(ref clipFile);

                    //	If this is from a pre-Maya3.0 file, the tangent angles will
                    //	need to be converted.
                    //
                    if (convertAnglesFromV2To3) {
                        convertAnglesAndWeights2To3(type,isWeighted,ref inAngle,ref inWeight);
                    } else if (convertAnglesFromV3To2) {
                        convertAnglesAndWeights3To2(type,isWeighted,ref inAngle,ref inWeight);
                    }

                    //  By default, the tangents are locked. When the tangents
                    //	are locked, setting the angle and weight of a fixed in
                    //	tangent may change the tangent type of the out tangent.
                    //
                    animCurve.setTangentsLocked(index, false);
                    animCurve.setTangent(index, inAngle, inWeight, true);
                }

                //	Only fixed tangents need additional information.
                //
                if (tanOut == MFnAnimCurve.TangentType.kTangentFixed)
                {
                    MAngle outAngle = new MAngle(asDouble(ref clipFile), tanAngleUnit);
                    double outWeight = asDouble(ref clipFile);

                    //	If this is from a pre-Maya3.0 file, the tangent angles will
                    //	need to be converted.
                    //
                    if (convertAnglesFromV2To3) {
                        convertAnglesAndWeights2To3(type,isWeighted,ref outAngle,ref outWeight);
                    } else if (convertAnglesFromV3To2) {
                        convertAnglesAndWeights3To2(type,isWeighted,ref outAngle,ref outWeight);
                    }

                    //  By default, the tangents are locked. When the tangents
                    //	are locked, setting the angle and weight of a fixed out
                    //	tangent may change the tangent type of the in tangent.
                    //
                    animCurve.setTangentsLocked(index, false);
                    animCurve.setTangent(index, outAngle, outWeight, false);
                }

                //	To prevent tangent types from unexpectedly changing, tangent
                //	locking should be the last operation. See the above comments
                //	about fixed tangent types for more information.
                //
                animCurve.setWeightsLocked(index, swLocked);
                animCurve.setTangentsLocked(index, tLocked);
                animCurve.setIsBreakdown (index, isBreakdown);

                //	There should be no additional data on this line. Go to the
                //	next line of data.
                //
                clipFile.ReadLine();

                //	Skip any comments.
                //
                advance(ref clipFile);
                c = (char)clipFile.Peek();
            }

            //	Ignore the brace that marks the end of the keys block.
            //
            if (c == kBraceRightChar) {
                clipFile.ReadLine();
            }

            //	Ignore the brace that marks the end of the animData block.
            //
            advance(ref clipFile);
            if ( (char)clipFile.Peek() == kBraceRightChar) {
                clipFile.ReadLine();
            }
            else {
                //	Something is wrong.
                //
                string msg = MStringResource.getString(RegisterMStringResources.kMissingBrace);
                MGlobal.displayError(msg);
            }

            //	Do not set the clipboard with an empty clipboard item.
            //
            if (!animCurveObj.isNull) {
                item.animCurve = animCurveObj;
            }

            //	Delete the copy of the anim curve.
            //
            MGlobal.deleteNode(animCurveObj);

            return true;
        }
Example #11
0
		public static void setToShortName( MAngle.Unit unit, ref string name)
		//
		//	Description:
		//		Sets the string with the short text name of the angle unit.
		//
		{
			switch(unit) {
				case MAngle.Unit.kDegrees:
					name = kDegString;
					break;
				case MAngle.Unit.kRadians:
					name = kRadString;
					break;
				case MAngle.Unit.kAngMinutes:
					name = kMinString;
					break;
				case MAngle.Unit.kAngSeconds:
					name = kSecString;
					break;
				default:
					name = kUnknownAngularString;
					break;
			}
		}
 /// <summary>
 /// Get the motor counts given the length
 /// </summary>
 /// <param name="ml"></param>
 /// <returns></returns>
 public virtual double ToMotorCounts(MAngle mA)
 {
     return(Math.Round(mA.ToDegrees * MotorCountsPerDeg));
 }