private BabylonMatrix ConvertMayaToBabylonMatrix(MMatrix mMatrix) { var transformationMatrix = new MTransformationMatrix(mMatrix); // Retreive TRS vectors from matrix float[] position = transformationMatrix.getTranslation(); float[] rotationQuaternion = transformationMatrix.getRotationQuaternion(); float[] scaling = transformationMatrix.getScale(); // Switch coordinate system at object level position[2] *= -1; rotationQuaternion[0] *= -1; rotationQuaternion[1] *= -1; // Apply unit conversion factor to meter position[0] *= scaleFactorToMeters; position[1] *= scaleFactorToMeters; position[2] *= scaleFactorToMeters; // The composed matrix return(BabylonMatrix.Compose(new BabylonVector3(scaling[0], scaling[1], scaling[2]), // scaling new BabylonQuaternion(rotationQuaternion[0], rotationQuaternion[1], rotationQuaternion[2], rotationQuaternion[3]), // rotation new BabylonVector3(position[0], position[1], position[2]) // position )); }
private void GetTransform(MFnTransform mFnTransform, ref float[] position, ref float[] rotationQuaternion, ref float[] rotation, ref BabylonVector3.EulerRotationOrder rotationOrder, ref float[] scaling) { var transformationMatrix = new MTransformationMatrix(mFnTransform.transformationMatrix); var mayaRotationOrder = 0; MGlobal.executeCommand($"getAttr {mFnTransform.fullPathName}.rotateOrder", out mayaRotationOrder); rotationOrder = Tools.ConvertMayaRotationOrder((MEulerRotation.RotationOrder)mayaRotationOrder); position = transformationMatrix.getTranslation(); rotationQuaternion = transformationMatrix.getRotationQuaternion(); rotation = transformationMatrix.getRotation(); scaling = transformationMatrix.getScale(); // Switch coordinate system at object level position[2] *= -1; rotationQuaternion[0] *= -1; rotationQuaternion[1] *= -1; rotation[0] *= -1; rotation[1] *= -1; rotationOrder = Tools.InvertRotationOrder(rotationOrder); // Apply unit conversion factor to meter position[0] *= scaleFactorToMeters; position[1] *= scaleFactorToMeters; position[2] *= scaleFactorToMeters; }
void printTransformData(MDagPath dagPath, bool quiet) { MObject transformNode = null; try { transformNode = dagPath.transform; } catch (System.Exception) { // This node has no transform - i.e., it's the world node // return; } MFnDagNode transform = null; try { transform = new MFnDagNode(transformNode); } catch (System.Exception) { return; } MTransformationMatrix matrix = new MTransformationMatrix(transform.transformationMatrix); if (!quiet) { MGlobal.displayInfo(" translation: " + MVectorToString(matrix.translation(MSpace.Space.kWorld)) + Environment.NewLine); } double[] threeDoubles = new double[3]; int rOrder = 0; matrix.getRotation(threeDoubles, out rOrder, MSpace.Space.kWorld); if (!quiet) { MGlobal.displayInfo(string.Format(" rotation: [%f, %f, %f]\n", threeDoubles[0], threeDoubles[1], threeDoubles[2])); } matrix.getScale(threeDoubles, MSpace.Space.kWorld); if (!quiet) { MGlobal.displayInfo(string.Format(" scale: [%f, %f, %f]\n", threeDoubles[0], threeDoubles[1], threeDoubles[2])); } }
private void GetTransform(MFnTransform mFnTransform, ref float[] position, ref float[] rotationQuaternion, ref float[] rotation, ref float[] scaling) { var transformationMatrix = new MTransformationMatrix(mFnTransform.transformationMatrix); position = transformationMatrix.getTranslation(); rotationQuaternion = transformationMatrix.getRotationQuaternion(); rotation = transformationMatrix.getRotation(); scaling = transformationMatrix.getScale(); // Switch coordinate system at object level position[2] *= -1; rotationQuaternion[0] *= -1; rotationQuaternion[1] *= -1; rotation[0] *= -1; rotation[1] *= -1; }
private void ExportTransform(BabylonAbstractMesh babylonAbstractMesh, MFnDagNode mFnDagNode) { // Position / rotation / scaling var transformationMatrix = new MTransformationMatrix(mFnDagNode.transformationMatrix); babylonAbstractMesh.position = transformationMatrix.getTranslation(); if (_exportQuaternionsInsteadOfEulers) { babylonAbstractMesh.rotationQuaternion = transformationMatrix.getRotationQuaternion(); } else { babylonAbstractMesh.rotation = transformationMatrix.getRotation(); } babylonAbstractMesh.scaling = transformationMatrix.getScale(); }
private List <BabylonAnimation> GetAnimationsFrameByFrame(MFnTransform mFnTransform) { int start = Loader.GetMinTime(); int end = Loader.GetMaxTime(); // Animations List <BabylonAnimation> animations = new List <BabylonAnimation>(); string[] babylonAnimationProperties = new string[] { "scaling", "rotationQuaternion", "position", "visibility" }; Dictionary <string, List <BabylonAnimationKey> > keysPerProperty = new Dictionary <string, List <BabylonAnimationKey> >(); keysPerProperty.Add("scaling", new List <BabylonAnimationKey>()); keysPerProperty.Add("rotationQuaternion", new List <BabylonAnimationKey>()); keysPerProperty.Add("position", new List <BabylonAnimationKey>()); keysPerProperty.Add("visibility", new List <BabylonAnimationKey>()); // get keys for (int currentFrame = start; currentFrame <= end; currentFrame++) { // get transformation matrix at this frame MDoubleArray mDoubleMatrix = new MDoubleArray(); MGlobal.executeCommand($"getAttr -t {currentFrame} {mFnTransform.fullPathName}.matrix", mDoubleMatrix); mDoubleMatrix.get(out float[] localMatrix); MMatrix matrix = new MMatrix(localMatrix); var transformationMatrix = new MTransformationMatrix(matrix); // Retreive TRS vectors from matrix var position = transformationMatrix.getTranslation(); var rotationQuaternion = transformationMatrix.getRotationQuaternion(); var scaling = transformationMatrix.getScale(); // Switch coordinate system at object level position[2] *= -1; rotationQuaternion[0] *= -1; rotationQuaternion[1] *= -1; // create animation key for each property for (int indexAnimation = 0; indexAnimation < babylonAnimationProperties.Length; indexAnimation++) { string babylonAnimationProperty = babylonAnimationProperties[indexAnimation]; BabylonAnimationKey key = new BabylonAnimationKey(); key.frame = currentFrame; switch (indexAnimation) { case 0: // scaling key.values = scaling.ToArray(); break; case 1: // rotationQuaternion key.values = rotationQuaternion.ToArray(); break; case 2: // position key.values = position.ToArray(); break; case 3: // visibility key.values = new float[] { Loader.GetVisibility(mFnTransform.fullPathName, currentFrame) }; break; } keysPerProperty[babylonAnimationProperty].Add(key); } } // create animation for each property for (int indexAnimation = 0; indexAnimation < babylonAnimationProperties.Length; indexAnimation++) { string babylonAnimationProperty = babylonAnimationProperties[indexAnimation]; List <BabylonAnimationKey> keys = keysPerProperty[babylonAnimationProperty]; var keysFull = new List <BabylonAnimationKey>(keys); // Optimization OptimizeAnimations(keys, true); // Ensure animation has at least 2 frames if (IsAnimationKeysRelevant(keys)) { int dataType = 0; // "scaling", "rotationQuaternion", "position", "visibility" if (indexAnimation == 0 || indexAnimation == 2) // scaling and position { dataType = (int)BabylonAnimation.DataType.Vector3; } else if (indexAnimation == 1) // rotationQuaternion { dataType = (int)BabylonAnimation.DataType.Quaternion; } else // visibility { dataType = (int)BabylonAnimation.DataType.Float; } // Create BabylonAnimation animations.Add(new BabylonAnimation() { dataType = dataType, name = babylonAnimationProperty + " animation", framePerSecond = Loader.GetFPS(), loopBehavior = (int)BabylonAnimation.LoopBehavior.Cycle, property = babylonAnimationProperty, keys = keys.ToArray(), keysFull = keysFull }); } } return(animations); }
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 { } } }
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 { } } }
private List <BabylonAnimation> GetAnimationsFrameByFrame(MFnTransform mFnTransform) { int start = GetMinTime()[0]; int end = GetMaxTime()[0]; // Animations List <BabylonAnimation> animations = new List <BabylonAnimation>(); string[] babylonAnimationProperties = new string[] { "scaling", "rotationQuaternion", "position" }; Dictionary <string, List <BabylonAnimationKey> > keysPerProperty = new Dictionary <string, List <BabylonAnimationKey> >(); keysPerProperty.Add("scaling", new List <BabylonAnimationKey>()); keysPerProperty.Add("rotationQuaternion", new List <BabylonAnimationKey>()); keysPerProperty.Add("position", new List <BabylonAnimationKey>()); // get keys for (int currentFrame = start; currentFrame <= end; currentFrame++) { // get transformation matrix at this frame MDoubleArray mDoubleMatrix = new MDoubleArray(); MGlobal.executeCommand($"getAttr -t {currentFrame} {mFnTransform.fullPathName}.matrix", mDoubleMatrix); mDoubleMatrix.get(out float[] localMatrix); MMatrix matrix = new MMatrix(localMatrix); var transformationMatrix = new MTransformationMatrix(matrix); // Retreive TRS vectors from matrix var position = transformationMatrix.getTranslation(); var rotationQuaternion = transformationMatrix.getRotationQuaternion(); var scaling = transformationMatrix.getScale(); // Switch coordinate system at object level position[2] *= -1; rotationQuaternion[0] *= -1; rotationQuaternion[1] *= -1; // create animation key for each property for (int indexAnimation = 0; indexAnimation < babylonAnimationProperties.Length; indexAnimation++) { string babylonAnimationProperty = babylonAnimationProperties[indexAnimation]; BabylonAnimationKey key = new BabylonAnimationKey(); key.frame = currentFrame; switch (indexAnimation) { case 0: // scaling key.values = scaling.ToArray(); break; case 1: // rotationQuaternion key.values = rotationQuaternion.ToArray(); break; case 2: // position key.values = position.ToArray(); break; } keysPerProperty[babylonAnimationProperty].Add(key); } } // create animation for each property for (int indexAnimation = 0; indexAnimation < babylonAnimationProperties.Length; indexAnimation++) { string babylonAnimationProperty = babylonAnimationProperties[indexAnimation]; List <BabylonAnimationKey> keys = keysPerProperty[babylonAnimationProperty]; // Optimization OptimizeAnimations(keys, true); // Ensure animation has at least 2 frames if (keys.Count > 1) { var animationPresent = true; // Ensure animation has at least 2 non equal frames if (keys.Count == 2) { if (keys[0].values.IsEqualTo(keys[1].values)) { animationPresent = false; } } if (animationPresent) { // Create BabylonAnimation animations.Add(new BabylonAnimation() { dataType = indexAnimation == 1 ? (int)BabylonAnimation.DataType.Quaternion : (int)BabylonAnimation.DataType.Vector3, name = babylonAnimationProperty + " animation", framePerSecond = GetFPS(), loopBehavior = (int)BabylonAnimation.LoopBehavior.Cycle, property = babylonAnimationProperty, keys = keys.ToArray() }); } } } return(animations); }
/// <summary> /// Default space is transform /// </summary> /// <param name="mTransformationMatrix"></param> /// <returns></returns> public static float[] getScale(this MTransformationMatrix mTransformationMatrix) { double[] scale = new double[3]; mTransformationMatrix.getScale(scale, MSpace.Space.kTransform); return(Array.ConvertAll(scale, item => (float)item)); }
void printTransformData(MDagPath dagPath, bool quiet) { MObject transformNode = null; try { transformNode = dagPath.transform; } catch (System.Exception) { // This node has no transform - i.e., it's the world node // return; } MFnDagNode transform = null; try { transform = new MFnDagNode(transformNode); } catch (System.Exception ) { return; } MTransformationMatrix matrix = new MTransformationMatrix(transform.transformationMatrix); if (!quiet) { MGlobal.displayInfo(" translation: " + MVectorToString(matrix.translation(MSpace.Space.kWorld)) + Environment.NewLine); } double[] threeDoubles = new double[3]; int rOrder = 0; matrix.getRotation (threeDoubles, out rOrder, MSpace.Space.kWorld); if (!quiet) { MGlobal.displayInfo(string.Format(" rotation: [%f, %f, %f]\n", threeDoubles[0], threeDoubles[1], threeDoubles[2])); } matrix.getScale (threeDoubles, MSpace.Space.kWorld); if (!quiet) { MGlobal.displayInfo(string.Format(" scale: [%f, %f, %f]\n", threeDoubles[0], threeDoubles[1], threeDoubles[2])); } }