0

I have a ModelVisual3D. I would like to animate its position in a Viewport3D. I have gotten the code to work with a BoxVisual3D (from HelixToolkit), but the code doesn't work for my ModelVisual3D. The ModelVisual3D should translate along the X axis, but instead it just sits still.

I have essentially copied the working code for my BoxVisual3D to my ModelVisual3D. For now, I am just doing the OffsetX property, but later on I will need to have multiple properties on multiple ModelVisual3Ds animated at once, hence the storyboard.

this is the code that doesn't work:

Transform3DGroup modelTransformGroup = (Transform3DGroup)_myModel.Transform;
TranslateTransform3D curTransform = (TranslateTransform3D)modelTransformGroup.Children[1];
var moveX = new DoubleAnimation(0, 1000, TimeSpan.FromSeconds(5));
Storyboard.SetTarget(moveX, curTransform);
Storyboard.SetTargetProperty(moveX, new PropertyPath(TranslateTransform3D.OffsetXProperty));
var sb = new Storyboard();
sb.Children.Add(moveX);
sb.Begin();

Here is some code that works, but won't be sufficient since I need to synchronize many animations at once with the storyboard:

Transform3DGroup modelTransformGroup = (Transform3DGroup)_myModel.Transform;
TranslateTransform3D curTransform = (TranslateTransform3D)modelTransformGroup.Children[1];
var moveX = new DoubleAnimation(0, 1000, TimeSpan.FromSeconds(5));
curTransform.BeginAnimation(TranslateTransform3D.OffsetXProperty, moveX);

Thanks for your help!

Edit: I wanted to add that _myModel.Transform is set as a Transform3DGroup earlier in the code and that the first child is a RotationTransform3D and the second child is a TranslateTransform3D.

Micahstuh
  • 443
  • 1
  • 5
  • 13

2 Answers2

0

I found the answer in this post which also pointed to this post. Turns out you have to register a name for your transformation and set the target name instead of the target. This is because transformations are not part of FrameworkElement.

Below is my working set of code.

Transform3DGroup modelTransformGroup = (Transform3DGroup)_myModel.Transform;
TranslateTransform3D curTransform = (TranslateTransform3D)modelTransformGroup.Children[1];
var moveX = new DoubleAnimation(0, 1000, TimeSpan.FromSeconds(5));
RegisterName("Translate", translate);
Storyboard.SetTargetName(moveX, "Translate");
Storyboard.SetTargetProperty(moveX, new PropertyPath(TranslateTransform3D.OffsetXProperty));
var sb = new Storyboard();
sb.Children.Add(moveX);
sb.Begin(this);

I have tested a couple different model types. It seems this will work for any type of object that inherits Visual3D.

Micahstuh
  • 443
  • 1
  • 5
  • 13
0

I am sorry to ask a question here. I really want to know why the codes occur some errors, like: Unable to type into "System. Windows. Media. Media3D. MatrixTransform3D" objects cast to type "System. Windows. Media. Media3D. Transform3DGroup.

Transform3DGroup modelTransformGroup = (Transform3DGroup)_myModel.Transform;
TranslateTransform3D curTransform (TranslateTransform3D)modelTransformGroup.Children[1];

  • 1
    Current answer doesn't help with an OP's problem. It's worth to create a separate question for you issue. – Oxoron Jun 12 '22 at 16:32