Advertisement
spookymunky

SymmThings 2.0 - Shrink CL Cluster (VBS)

Mar 25th, 2013
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '-----------------------------------------------------------------------------------------------------------------'
  2. 'To use this script you must first create a point / edge cluster of all the components along the center that you 'want included in the shrinkwrap. The cluster should be named CL, but if it isn't this script wil do a few (very) 'simple checks for clusters it could use. Probably the best way is to have the cluster you want to use selected 'when you run this, and it will ask if you want to rename it to CL, otherwise it will look for a cluster named 'Edge, then a cluster named Point and ask if you want to use those hehe, clicking no to one check will just let 'the script go on to the next, at the end it will check if there is any CL cluster, if not it will cancel out.
  3.  
  4. 'If the object(s) haven't already got a shrink center grid, click ok when it asks if you want to create one.
  5.  
  6. 'With both of these things in place it will then create a new Shrink Wrap op on the CL cluster and move it into 'the animation stack.
  7.  
  8. 'If there is already a Shrink Wrap op in place the script will ask you if you want to freeze the object, if you 'select no, it will count how many Shrink Wraps exist and then ask if you want to delete the highest one found, 'click yes if you have only one Shrink Wrap op in either the shape modelling or animation stacks, or click no if 'the existing shrink wrap(s) are all in the modelling stack. Since xsi renames operators depending where they are 'in the stack, and I have yet to find a way of determining where they are.. sigh, this is the best I could come up 'with for now :P
  9. '-----------------------------------------------------------------------------------------------------------------'
  10.  
  11. dim oCol, oSelection, oSelecto, ScriptCancel, BP, POINTstr, EDGEstr, CLstr, oPointObj, oEdgeObj, oCLObj
  12. dim oShrinkCenter, oShrinkObj, oModelo, oSelName, oShrinky, SWstr, oObj, ShrinkWrapCount, StopLoop
  13. dim JustIncaseAbort, regEx, Match, Matches, oSubdCtrlCheck
  14.  
  15. 'Set up a regExp for later use
  16. set regEx = New RegExp
  17.  
  18. 'Define a collection(oCol) for adding objects to
  19. Set oCol = CreateObject( "XSI.Collection" )
  20.  
  21. 'Check if an object is selected
  22. if Application.Selection.count > 1 Then
  23.  
  24.     'Add the application selection to oCol
  25.     Set oSelection = Application.Selection
  26.    
  27.     for i = 0 to (oSelection.Count - 1)
  28.    
  29.         'Set a new Regex pattern that finds any letters after - at the end of its name
  30.         regEx.Pattern = "-[a-zA-Z]+$"
  31.            
  32.         'Reset oSubdCtrlCheck
  33.         oSubdCtrlCheck = "Nothing"
  34.            
  35.         'Execute the regex on the selection
  36.         Set Matches = regEx.Execute(oSelection(i))
  37.         For Each Match in Matches
  38.             oSubdCtrlCheck = Match.Value
  39.         Next
  40.            
  41.         'If the selection doesnt end in -SubdCtrl then add it to the oCol Collection.
  42.         if oSubdCtrlCheck <> "-SubdCtrl" then
  43.            
  44.             Logmessage oSelection(i) & " has a funny accent, added to oCol"
  45.             oCol.add oSelection(i)
  46.                
  47.         else
  48.            
  49.             Logmessage oSelection(i) & " looked at me funny, Rejected!"
  50.                
  51.         end if
  52.        
  53.     next   
  54.  
  55. elseIf Application.Selection.count = 1 Then
  56.  
  57.     oCol.add Application.Selection(0)
  58.        
  59. else   
  60.  
  61.     'If no object is selected, start a picksession
  62.     LogMessage "Select The Control Mesh (-Modelling)"
  63.     PickObject "Select The Control Mesh (-Modelling)", " ", oSelecto
  64.  
  65.     'Add Picksession object to oCol
  66.     oCol.add oSelecto
  67.    
  68. end if
  69.  
  70. for i = 0 to (oCol.count-1)
  71.    
  72.     set oSelecto = oCol(i)
  73.  
  74.     'Check if the cluster is selected instead of the mesh, saves having to re-select the mesh after creating the CL cluster
  75.     if TypeName( oSelecto ) = "Cluster" then
  76.         oSelecto = oSelecto.Parent3DObject
  77.     end if
  78.  
  79.     'Make sure the selection is a Polymesh, if it isn't then give the user one last try to select one
  80.     set oSelecto = Dictionary.GetObject( oSelecto, false )
  81.    
  82.     if oSelecto.Type = "polymsh" then
  83.    
  84.         ScriptCancel = "no"
  85.        
  86.     else
  87.    
  88.         LogMessage oSelecto & " is a " & oSelecto.Type & ",not a polymsh"
  89.            
  90.         BP = XSIUIToolkit.Msgbox( oSelecto & " is not a polymsh, do you want to select something else?", 4, "No Polygon Mesh Selected" )
  91.        
  92.             if BP = 6 then
  93.            
  94.                 PickObject "Please select a polygon mesh", " ", oSelecto
  95.                
  96.                 if oSelecto.Type = "polymsh" then  
  97.        
  98.                     ScriptCancel = "no"    
  99.        
  100.                 else               
  101.                        
  102.                     XSIUIToolkit.Msgbox "Bah, I give up.. " & oSelecto & " is a " & oSelecto.Type & " !", 1, "No Polygon Mesh Selected"
  103.                     Logmessage "Script Cancelled - Next time select a Polygon Mesh instead of a " & oSelecto.Type & "! :)"
  104.  
  105.                     ScriptCancel = "yes"
  106.  
  107.                 end if
  108.                
  109.             else
  110.                    
  111.                 ScriptCancel = "yes"
  112.  
  113.             end if
  114.            
  115.     end if
  116.  
  117.     POINTstr = oSelecto & ".polymsh.cls.Point"
  118.     EDGEstr = oSelecto & ".polymsh.cls.edge.clslist.Edge"
  119.     CLstr = oSelecto & ".polymsh.cls.CL"
  120.     set oPointObj = Dictionary.GetObject( POINTstr, false )
  121.     set oEdgeObj = Dictionary.GetObject( EDGEstr, false )
  122.     set oCLObj = Dictionary.GetObject( CLstr, false )
  123.  
  124.     'Check if CL / point / edge cluster exists
  125.     if ScriptCancel = "no" and TypeName( oCLObj ) = "Nothing" and TypeName( oEdgeObj ) = "Nothing" and TypeName( oPointObj ) = "Nothing" and TypeName( ocol(i) ) <> "Cluster" then
  126.        
  127.         XSIUIToolkit.Msgbox "You need to create an edge / point cluster of the center loop for " & oSelecto & " and name it CL", 1, "No CL Cluster Found"
  128.         Logmessage "You need to create an edge / point cluster of the center loop for " & oSelecto & " and name it CL"
  129.                    
  130.         ScriptCancel = "yes"
  131.  
  132.     end if
  133.    
  134.     if ScriptCancel = "no" and TypeName( oCLObj ) = "Nothing" and TypeName( oCol(i) ) = "Cluster" then
  135.         Logmessage "Selection is a cluster"
  136.         BP = XSIUIToolkit.Msgbox( oSelecto & " has no CL cluster but you had a cluster selected when you ran this script, do you want to rename it to CL ? (" & oCol(i) & ")", 4, "Edge Cluster Detected" )
  137.        
  138.             if BP = 6 then
  139.            
  140.                 SetValue oCol(i) & ".Name", "CL"
  141.                 logmessage "Selected cluster renamed to CL"
  142.                 set oCLObj = Dictionary.GetObject( CLstr, false )
  143.            
  144.             end if
  145.            
  146.     end if
  147.    
  148.     'If no CL cluster exists but an Edge cluster exists, ask if user wants to rename it to CL
  149.     if ScriptCancel = "no" and TypeName( oCLObj ) = "Nothing" and TypeName ( oEdgeObj ) = "Cluster" and oCol(i).Name <> "Edge" then
  150.         Logmessage "Edge Cluster Detected"
  151.  
  152.         BP = XSIUIToolkit.Msgbox( oSelecto & " has no CL cluster but an Edge cluster was detected, do you want to rename it to CL ?", 4, "Edge Cluster Detected" )
  153.  
  154.             if BP = 6 then
  155.            
  156.                 SetValue oSelecto & ".polymsh.cls.edge.clslist.Edge.Name", "CL"
  157.                 logmessage "Edge cluster renamed to CL"
  158.                 set oCLObj = Dictionary.GetObject( CLstr, false )
  159.            
  160.             end if
  161.                
  162.     end if
  163.  
  164.     'If no CL cluster exists but a Point cluster exists, ask if user wants to rename it to CL
  165.     if ScriptCancel = "no" and TypeName( oCLObj ) = "Nothing" and TypeName ( oPointObj ) = "Cluster" and oCol(i).Name <> "Point" then
  166.         LogMessage "Point Cluster Detected"
  167.        
  168.         BP = XSIUIToolkit.Msgbox( oSelecto & " has no CL cluster but a Point cluster was detected, do you want to rename it to CL ?", 4, "Point Cluster Detected" )
  169.            
  170.             if BP = 6 then
  171.            
  172.                 SetValue oSelecto & ".polymsh.cls.Point.Name", "CL"
  173.                 logmessage "Point cluster renamed to CL"
  174.                 set oCLObj = Dictionary.GetObject( CLstr, false )
  175.                
  176.             end if
  177.                
  178.     end if
  179.    
  180.     if ScriptCancel = "no" and TypeName( oCLObj ) = "Nothing" then
  181.        
  182.         ScriptCancel = "yes"
  183.         LogMessage oSelecto & " was skipped since it has no CL cluster"
  184.    
  185.     end if
  186.  
  187.     'Check if a ShrinkCenter object exists, if not ask the user if they want to create one
  188.     oShrinkCenter = oSelecto&"-ShrinkCenter"
  189.     set oShrinkObj = Dictionary.GetObject( oShrinkCenter, false )
  190.    
  191.     if ScriptCancel = "no" and TypeName( oShrinkObj ) = "Nothing" then
  192.    
  193.         LogMessage "No ShrinkCenter Object Found"  
  194.        
  195.         BP = XSIUIToolkit.Msgbox( "No ShrinkCenter found for " & oSelecto & ", would you like to create one now?", 4, "No ShrinkCenter Object" )
  196.            
  197.             if BP = 6 then
  198.            
  199.                 LogMessage "Creating a Shrinky Centery Gridy thingy!"
  200.            
  201.                 'Remove Model name from oSelecto
  202.                 oModelo = oSelecto.Model
  203.                 oSelName = replace( oSelecto, oModelo&".", "")
  204.                
  205.                 'Create a new grid to be used as a shrinkwrap target
  206.                 Preferences.SetPreferenceValue "Interaction.autoinspect", false
  207.                 set oShrinky = CreatePrim ("Grid", "MeshSurface", oSelName & "-ShrinkCenter")
  208.                 Preferences.SetPreferenceValue "Interaction.autoinspect", true
  209.                
  210.                 'Pose constrain the shrink center to original object
  211.                 ApplyCns "Pose", oShrinky, oSelecto
  212.                
  213.                 'Adjust the shrink center's pose constrain scale to 50x on all axis, and -90deg on z rotation axis
  214.                 SetValue oShrinky & ".kine.posecns.sclx", 50
  215.                 SetValue oShrinky & ".kine.posecns.scly", 50
  216.                 SetValue oShrinky & ".kine.posecns.sclz", 50
  217.                 SetValue oShrinky & ".kine.posecns.rotz", -90
  218.  
  219.                 'Disable all of the shrink center's visibility options
  220.                 SetValue oShrinky & ".visibility.shdwcast", False
  221.                 SetValue oShrinky & ".visibility.shdwrecv", False
  222.                 SetValue oShrinky & ".visibility.primray", False
  223.                 SetValue oShrinky & ".visibility.scndray", False
  224.                 SetValue oShrinky & ".visibility.trnscast", False
  225.                 SetValue oShrinky & ".visibility.trnsvis", False
  226.                 SetValue oShrinky & ".visibility.causvis", False
  227.                 SetValue oShrinky & ".visibility.globcast", False
  228.                 SetValue oShrinky & ".visibility.globvis", False
  229.                 SetValue oShrinky & ".visibility.viewvis", False
  230.                 SetValue oShrinky & ".visibility.rendvis", False
  231.                
  232.                 ParentObj "B:" & oSelecto, oShrinky
  233.                
  234.                 LogMessage "Created " & oShrinky
  235.                
  236.             else
  237.            
  238.                 ScriptCancel = "yes"
  239.                 LogMessage "Script Cancelled since no ShrinkCenter object exists"
  240.                
  241.             end if
  242.  
  243.     end if
  244.    
  245.     'Check if a ShrinkWrap op already exists
  246.     SWstr = oSelecto & ".polymsh.shrinkwrap"
  247.     set oMCobj = Dictionary.GetObject( SWstr, false )
  248.    
  249.     if TypeName( oMCobj ) <> "Nothing" and ScriptCancel = "no" then
  250.    
  251.         BP = XSIUIToolkit.Msgbox( "Do you want to freeze " & oSelecto & " ? : If there is more than one ShrinkWrap op in either the Shape Modelling or Animation stacks then this won't work (clicking no will determine how many Shrink Wrap ops there are, then ask if you want to delete the highest one)", 3, "Shrink Wrap Ops Detected, Freeze " & oselecto & "?" )
  252.        
  253.             if BP = 6 then
  254.            
  255.                 LogMessage "Freezing Object"
  256.                 FreezeObj oSelecto
  257.                
  258.             elseIf BP = 7 then
  259.    
  260.                 LogMessage "ShrinkWrap operator already in place, determining what number the new one will be"
  261.            
  262.                 'Reset some variables
  263.                 JustIncaseAbort = 0
  264.                 ShrinkWrapCount = 0
  265.                 StopLoop = "no"
  266.                
  267.                 '-----------------------------Loop Start-----------------------------'
  268.                
  269.                 do
  270.                
  271.                 'Add 1 onto the counter that will be used to see how many shrink wrap ops exist
  272.                 ShrinkWrapCount = ShrinkWrapCount + 1
  273.                
  274.                 'Check to see if a shrink wrap op with the count number exists e.g. shrinkwrap[15]
  275.                 SWstr = oSelecto & ".polymsh.shrinkwrap["&ShrinkWrapCount&"]"
  276.                 set oMCobj = Dictionary.GetObject( SWstr, false )
  277.                
  278.                 if TypeName( oMCobj ) = "Nothing" then
  279.                     StopLoop = "yes"
  280.                 end if
  281.                
  282.                 'Increment an abort counter to stop an infinite loop
  283.                 JustIncaseAbort = JustIncaseAbort + 1
  284.                
  285.                 Loop Until StopLoop = "yes" or JustIncaseAbort = 1000
  286.                
  287.                 '-----------------------------Loop Finish-----------------------------'
  288.                
  289.                 'If the abort number has been reached then cancel the script
  290.                 if JustIncaseAbort = 10000 then
  291.                     LogMessage "Either something has gone horribly wrong or you have more than 1000 Shrink Wrap ops ?? in which case it might be time to freeze the modelling stack :)"
  292.                     ScriptCancel = "yes"
  293.                 end if
  294.                
  295.                 LogMessage oSelecto & " had " & ShrinkWrapCount & " shrinkwraps"
  296.                
  297.                 if ScriptCancel = "no" then
  298.                
  299.                     BP = XSIUIToolkit.Msgbox( ShrinkWrapCount & " Shrink Wrap ops were found, is there one (singular) in either the Shape Modelling or Animation stack ? ( clicking yes will delete Shrink Wrap[" & ShrinkWrapCount - 1 &"] )", 3, "Move Component Ops Detected" )
  300.                    
  301.                         if BP = 6 then
  302.                        
  303.                             ShrinkWrapCount = ShrinkWrapCount - 1
  304.                            
  305.                             DeleteObj ".shrinkwrap[" & ShrinkWrapCount & "]"
  306.                            
  307.                             SWstr = oSelecto & ".polymsh.shrinkwrap[" & ShrinkWrapCount & "]"
  308.                            
  309.                             LogMessage oSelecto & ".polymsh.shrinkwrap[" & ShrinkWrapCount & "] was deleted"
  310.                            
  311.                         elseIf BP = 2 then
  312.                        
  313.                             LogMessage "Cancelling Script"
  314.                             ScriptCancel = "yes"
  315.                        
  316.                         end if
  317.                
  318.                 end if
  319.                
  320.             else
  321.            
  322.                 LogMessage "Cancelling Script"
  323.                 ScriptCancel = "yes"
  324.            
  325.             end if
  326.        
  327.     else
  328.        
  329.         SWstr = oSelecto & ".polymsh.shrinkwrap"
  330.        
  331.     end if
  332.  
  333.     if ScriptCancel = "no" then
  334.  
  335.         'Apply a shrinkwrap op on the CL cluster, using the ShrinkCenter object as the target
  336.         ApplyOp "ShrinkWrap", oSelecto&".polymsh.cls.CL;" & oShrinkCenter&";", 3, siPersistentOperation, , 0
  337.  
  338.         'Move the shrinkwrap op to the animation stack
  339.         MoveOperatorAfter oSelecto & ".polymsh", SWstr, oSelecto & ".polymsh.shapemarker"
  340.  
  341.         'Set the shrinkwrap mode to closest surface
  342.         SetValue SWstr & ".proj", 5
  343.  
  344.         LogMessage oSelecto&".polymsh.cls.CL" & " was shrinkwrapped to " & oShrinkCenter
  345.  
  346.     end if
  347.    
  348. Next
  349.  
  350. SelectObj oCol
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement