User avatar
By w i l l
#279316
Has anyone yet managed to write any interesting macros to automate animation with the plugin? I'd really like to learn how to do this and then come up with some of my own. Or any tutorials?
By JDHill
#279324
That is a pretty broad subject; probably (if you are unfamiliar) you would just want to start by reading up a bit on VBA (Visual Basic for Applications), which is the language you'd probably use to automate things in SolidWorks. There is of course a simple Sun-study in the plugin's manual (see the Scripting topic) which shows how to connect to the current scene and how to get data in and out - in designing the code-interface, I've tried to make it as easy as possible to do this. Basically, if you know the layout of the plugin's Scene Manager, it should be very easy to access things in code - the layout of scene-data is nearly 1:1 with how it is represented in the Scene Manager.

As far as any step-by-step for getting started, you could try out this small crash-course:

1. make sure you have installed the Maxwell.Script library - this is available from the same place you download the plugin installer
2. open SolidWorks, go to Tools > Macro > New
3. the VBA editor will open up, and there will be a code page like this:
Code: Select all
Dim swApp As Object
Sub main()

Set swApp = Application.SldWorks
End Sub
4. try editing the code to look like this:
Code: Select all
Dim swApp As Object
Sub main()

Set swApp = Application.SldWorks

MsgBox "hello world."

End Sub
5. that's your basic hello world example - click the 'Run Sub/UserForm' button in the VBA editor toolbar to run this code; you should just get a small message box (i.e. MsgBox) that pops up

6. next, we'll connect to the current Maxwell Scene (assuming there is a document open). Start by going to Tools > References in the VBA editor's toolbar - we are going to load the Maxwell.Script library...

7. in the 'References' dialog which comes up, scroll down to the Maxwell.Script entry, check its' checkbox, and click OK

8. in the VBA menu go to View > Object Browser, then in the Object Browser's drop-down, select 'Maxwell_Script' - this will show you the objects which are available for you to code against...this is what you'll see:

Image

9. finally, here is how some code would look which gets or sets some parameters in the current scene:
Code: Select all
Dim swApp As Object
Sub main()

Set swApp = Application.SldWorks

Dim maxwell As New Maxwell_Script.ScriptObject

If maxwell Is Nothing Then
  MsgBox "unable to create Maxwell.Script.ScriptObject."
  ' bail
  Exit Sub
End If

If Not maxwell.IsConnected Then
  MsgBox "unable to connect to the current Scene (if there even is one)."
  'bail
  Exit Sub
End If

'get some camera params
MsgBox "the current camera is named '" & maxwell.Camera.Name & "'"
MsgBox "the current camera's fStop is " & maxwell.Camera.fStop

'get/set some environment params
maxwell.Environment.EnvironmentType = "PhysicalSky"
MsgBox "the environment type is " & maxwell.Environment.EnvironmentType
maxwell.Environment.EnvironmentType = "SkyDome"
MsgBox "changed the environment type to " & maxwell.Environment.EnvironmentType

'get/set date/time params
MsgBox "the current time of day is " & maxwell.DateAndTime.TimeOfDay
maxwell.DateAndTime.TimeOfDay = 11.22
MsgBox "changed time of day to " & maxwell.DateAndTime.TimeOfDay

End Sub
That's a quick tutorial, and I apologize if you already knew some of it. If not, you can pick up a 'VBA for Dummies' book at the local bookstore which can pretty quickly teach you basic VBA syntax. VBA itself is really the same whether you're coding against SolidWorks, AutoCAD, MS Word/Excel, etc. There are also some VBA code examples in the SW documentation if you go to SolidWorks main menu > Help > API Help Topics item, then drill down into the contents to SolidWorks API Help > Examples and Projects > Visual Basic (VB) Examples - these can show you how to get your hands on objects in the SolidWorks model.
User avatar
By w i l l
#279368
Thanks very much.

I didn't know any of that. I haven't yet tested it.. i'll have a go though and probably buy something like a 'VBA for Dummies' book. I see that your examples involve environment settings/scripting. Would it be possible to write a script for a 360 degree animation of an object? I think I remember you saying that the plugin cant link to the Solidworks Motion Manager to render out per frame (as the motion manager sees it), but would it be possible to somehow render out per 'frame' through a script?

Sorry if that isn't termed correctly (I don't yet understand scripting) but I'm wondering if it would be theoretically possible to render out an animation such as a 360 degree rotation of an object through the plugin. If this can theoretically be done using a script then i'll have a go.

Alternatively do you think Solidworks in the future may make it easier for the plugin to link to Motion Manager to render out per frame? Are Solidworks likely to look into making this possible, as i've heard that they may be addressing the problem with texturing/using UV sets which can be recognised by the Maxwell plugin.
By JDHill
#279370
There is only one fundamental problem with hooking into SW animation: there is no notification you can catch to know when each frame is being shown...if you look through the SW API help, you will find that the control you are given is the basic start/stop commands, really no different than just clicking those buttons in the UI (alot of the SW API works like this).

It is surely possible to go around this entirely and code your own turntable, though it is not a super-trivial thing to do. Easiest would be to animate the camera around a target, rather than rotating an object in front fo the camera. Conceptually, you would probably want to:

- prompt for the desired number of frames
- prompt for a center & axis of rotation (or, use the current camera's target point with the <x/y/z> axis direction)
- write code that implements the rotation and exports MXS at each step, something along these lines:
Code: Select all
<zero-out camera position/target/up>
<issue Maxwell.Script's BeginAnimation command>
for each frame
  <rotate camera position <n> degrees around target/up>
  <issue Maxwell.Script's ExportMXS command>
next
<issue Maxwell.Script's EndAnimation command>
I'll see if I can play around with this myself a bit too...that's one of the reasons why asking questions on the forum is so valuable - doing a simple turntable this way hadn't really occurred to me until writing this response. I'm not sure how long it would actually take me to make it work, but I can mess around with it...
User avatar
By w i l l
#279371
So you're saying that animating the camera is potentially easier than animating moving objects? So a walkthrough animation would also be possible? (but probably with some full on amount of code).
By JDHill
#279373
While most anything is possible, it is indeed much simpler to accomplish a turntable animation using the camera because all of the 3D data involved is conceptually a part of the camera itself (i.e. position point, target point, direction vector). So from frame to frame, you just need to modify those values and export.

On the other hand, if you want to do something like a path, it will involve getting an obect (probably a sketch, or an edge?) defined by the user, then interpolating along it. If you want to animate a rotating object, you need to define where it rotates around. If it is to move in arbitrary ways, you need to define some path for it to move along, as well as a position/direction relation between some point on the object and the path curve(s). There is also the difference between .sldprt and .sldasm to take into account - moving something in a .sldprt basically means re-defining the sketch it is built from (so it really doesn't make much sense), while in a .sldasm, it involves changing the transform applied to the component. So, these other things are all much more complex in comparison to simply rotating the camera.

Since the plugin's basic job is exporting MXS files (not altering the SW model), I'm relatively unfamiliar with those areas, and it would involve a good deal of trial and error while exploring how those parts of the API work. And here we get into time-budgeting: there are many areas of the plugin that I wish to refine and which apply to the basic functionality and purpose of the plugin (it's far from perfect, after all) - those things have to take precedence over something (i.e. complex animation) which I would really consider to be a 'value-added' feature, were it to be implemented.
By JDHill
#279414
Here's a VBA macro that will do a 360° turntable animation using the Maxwell plugin to output a variable number of frames and render them in MXCL. All the lines that start with an apostrophe are comments. To use this you would go to Tools > Macro > New, choose a file name, then replace the code that is shown with what's copied below. It uses the current camera's target to spin around, and your camera should have 'Aimed at target' checked, not 'Floating'. Once you've created and saved the VBA project, you can put it in a toolbar button - see SolidWorks Help > User Interface > Commands, Menus, Toolbars > Managing Toolbars > Customize Macro Button.
Code: Select all
Option Explicit

'globals
Dim swApp As Object
Dim previousFrameCount As Integer

Sub MaxwellTurntableAnimation()
    On Error GoTo handle_error
    
    'dialog title
    Dim dialogTitle As String
    dialogTitle = "Maxwell Turntable Animation"
    
    'get SW
    Set swApp = Application.SldWorks
    If swApp Is Nothing Then
      MsgBox "Unable to connect to SolidWorks", vbOKOnly, dialogTitle
      Exit Sub
    End If
    
    'get active doc
    Dim doc As ModelDoc2
    Set doc = swApp.ActiveDoc
    If doc Is Nothing Then
      MsgBox "There is no active document.", vbOKOnly, dialogTitle
      Exit Sub
    End If
    
    'get active view
    Dim view As ModelView
    Set view = doc.ActiveView
    If view Is Nothing Then
      MsgBox "There is no active view.", vbOKOnly, dialogTitle
      Exit Sub
    End If
    
    'get view camera
    Dim cam As Camera
    Set cam = view.Camera
    If cam Is Nothing Then
      MsgBox "The active view has no SolidWorks camera - " & _
      "please add one, or choose a viewport which does.", vbOKOnly, dialogTitle
      Exit Sub
    End If
    
    'Maxwell.Script ScriptObject
    Dim maxwell As Object
    Set maxwell = CreateObject("Maxwell.Script.ScriptObject")
    
    'ScriptObject is created?
    If maxwell Is Nothing Then
      MsgBox "Unable to create Maxwell.Script.ScriptObject.", vbOKOnly, dialogTitle
      Exit Sub
    End If
    
    'ScriptObject is connected?
    If Not maxwell.IsConnected Then
      MsgBox "There is no current Maxwell Scene.", vbOKOnly, dialogTitle
      Exit Sub
    End If
    
    'declare a bunch of variables
    Dim viewportRect(0 To 3) As Long
    Dim cameraTarget As Double
    Dim cameraLatitude As Double
    Dim cameraLongitude As Double
    Dim strFrameCount As String
    Dim frameCount As Integer
    Dim frameStep As Double
    Dim initialLongitude As Double
    Dim meshesWereCached As Boolean
    Dim i As Integer
    
    'get viewport for refreshing
    viewportRect(0) = view.FrameLeft
    viewportRect(1) = view.FrameTop
    viewportRect(2) = view.FrameWidth
    viewportRect(3) = view.FrameHeight
    
    'initialize previous frameCount
    If previousFrameCount < 2 Then
      previousFrameCount = 24
    End If
    
    'get number of frames from user
get_frameCount:
    strFrameCount = InputBox("Specify the number of frames to render.", _
                             dialogTitle, previousFrameCount)
    'cancelled by user?
    If strFrameCount = "" Then Exit Sub
    
    'validate to numeric input
    If Not IsNumeric(strFrameCount) Then
      MsgBox "Please enter a number.", vbOKOnly, dialogTitle
      'try again
      GoTo get_frameCount
    End If
    
    'validate specified number of frames
    If CInt(strFrameCount) < 2 Then
      MsgBox "Please enter a number greater than one.", vbOKOnly, dialogTitle
      'try again
      GoTo get_frameCount
    End If
    
    'set framecount
    frameCount = CDbl(strFrameCount)
    
    'check for crazy number of frames
    If frameCount > 360 Then
      If Not MsgBox("That sure is alot of frames (" & _
                    frameCount & "). Do you really want " & _
                    "continue?", vbYesNo, dialogTitle) = vbYes Then
        Exit Sub
      End If
    End If
    
    'store as previous count
    previousFrameCount = frameCount
    'set step size, i.e. (2 * PI) / frameCount
    frameStep = 6.2831853 / frameCount
    'get initial position
    cam.GetPositionSpherical cameraTarget, initialLongitude, cameraLatitude
    'save current value of Cache MXS Meshes option
    meshesWereCached = maxwell.PlugInOptions.CacheMXSMeshes
    'enable Cache MXS Meshes
    maxwell.PlugInOptions.CacheMXSMeshes = True
    'start plugin animation loop
    maxwell.Rendering.BeginAnimation

    For i = 1 To frameCount
      'write MXS
      maxwell.Rendering.RenderToMxs
      'get current camera position
      cam.GetPositionSpherical cameraTarget, cameraLongitude, cameraLatitude
      'set new camera position (i.e. add step size)
      cam.SetPositionSpherical cameraTarget, cameraLongitude + frameStep, cameraLatitude
      'refresh viewport
      view.GraphicsRedraw (viewportRect)
    Next
    
    'end plugin animation loop
    maxwell.Rendering.EndAnimation
    'reset camera exactly where it was before
    cam.SetPositionSpherical cameraTarget, initialLongitude, cameraLatitude
    'restore Cache MXS Meshes option to previous value
    maxwell.PlugInOptions.CacheMXSMeshes = meshesWereCached
    
    'done
    Exit Sub
    
    'something went wrong
handle_error:     MsgBox "An unexpected error occurred."

End Sub
Last edited by JDHill on Tue Sep 02, 2008 2:39 am, edited 1 time in total.
By JDHill
#281003
Cool. I put it into the plugin for the next version:

Image

The window just floats over SW, so you can modify the camera and preview the animation until you get it set up exactly how you want.
User avatar
By w i l l
#281023
Ok thats really good.

Does Maxwell have to know the position of an object before it can render it? And the VBA script is associating this position with Maxwell at each frame in order for it to render out all of the frames?

I had an idea in my head that you might be able to run an animation using Solidworks but then code a separate script that tells Maxwell to render frames at set time intervals (not necessarily every frame within the Motion Manager - so it doesnt have to link to Motion Manager). Kind of like a screen grab using Maxwell at set time intervals. That way you can do all of the complex animation within Solidworks but just use the script to run the render at certain intervals. I suppose this wouldn't work as it needs to know or link to the position of the parts? I'm thinking in terms of having a fixed camera and animating a moving mechanism for example.
By JDHill
#281026
Yes, the plugin needs to know where the objects and the camera are at export-time...that's how the turntable script works:

- move camera
- export (nothing is moving)
- move camera
- export (nothing is moving)
- etc..

So regarding something based on a 'dumb' timer, I'm not sure if it's realistic or not. Either way it would be quite a kludge. If it's possible, then it's possible using VBA and the plugin's BeginAnimation and EndAnimation script methods, like in the turntable script above. I was looking in the new 2009 API, and it looks like they've added some event hooks for Motion Studies - if they are what I think they are (I've only had a quick look), we may now have an actual, legit way of accomplishing this.
By JDHill
#282946
No, not off-hand. I can try to work something up - if you want to play around with this, it would end up looking alot like the code posted above, except that this part:
Code: Select all
    For i = 1 To frameCount 
      'write MXS 
      maxwell.Rendering.RenderToMxs 
      'get current camera position 
      cam.GetPositionSpherical cameraTarget, cameraLongitude, cameraLatitude 
      'set new camera position (i.e. add step size) 
      cam.SetPositionSpherical cameraTarget, cameraLongitude + frameStep, cameraLatitude 
      'refresh viewport 
      view.GraphicsRedraw (viewportRect) 
    Next 
...would have code for moving the camera along its' view vector, rather than rotating it around the focal point.
User avatar
By w i l l
#282959
Well if its not too much trouble to post something that would be really helpful. I still haven't looked into scripting myself - I can kind of guess what should be changed but not exactly sure. Would be interested to see the changes anyway.
Will there be a Maxwell Render 6 ?

Let's be realistic. What's left of NL is only milk[…]