Advertisement
spookymunky

SymmThings 2.01 - Shrink CL Cluster (VBS)

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