Advertisement
spookymunky

SymmThings 2.0 - Mirror Instance (VBS)

Mar 25th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '-----------------------------------------------------------------------------------------------------------------'
  2. 'This script will instantiate the selected model(s) then setup mirror like expressions - inverted on scale x axis, 'position x axis, and rotation y and z axis, with the other expressions being a direct copy.
  3.  
  4. 'If instances have already been created by this script (and still have -Instance in their name) and you run this 'on the original model again, it will branch select the model and duplicate it first, then create a new instance 'of that model.
  5.  
  6. 'If you have more than one model selected then only the models that don't end in -Instance are added to the 'collection for either duplication or instancing, so if you need to run it on a -Instance model then you must 'select it on its own. On the plus side you don't have to be too cautious when selecting models if you want to 'quickly duplicate a large group of them and their instances, aslong as all the non-instances you want are 'selected it doesn't matter how many(if any) -Instances are selected.
  7.  
  8. 'This will also ignore any non-model item in the selection so you don't have to worry too much about just dragging 'a selection in the viewport, as long as a model null is part of the selection then it should work.
  9.  
  10. 'If the selection isn't a model, this script will check if it's part of a model and ask the user if they want to 'use that instead. Or if the selection isn't part of any model it will ask the user if they want to create a new 'model for it.
  11. '-----------------------------------------------------------------------------------------------------------------'
  12.  
  13. dim oSelecto, oSelection, oCol, regEx, oModel, oModelName
  14. dim oInstCheck, oModelCheck, oScriptCancel, BP, oObj, oFinalSelect
  15.  
  16. 'Define a collection(oCol) for adding viable models to
  17. Set oCol = CreateObject( "XSI.Collection" )
  18.  
  19. Set oSelection = Application.Selection
  20. oScriptCancel = "no"   
  21.  
  22. 'Check if any items are selected
  23. if oSelection.count > 0 Then
  24.  
  25.     'Check if more than one item is selected
  26.     if oSelection.count > 1 Then
  27.    
  28.         for i = 0 to (oSelection.Count - 1)
  29.        
  30.             'Set a new Regex pattern that checks if a selected Model has "-Instance" at the end of its name
  31.             set regEx = New RegExp
  32.             regEx.Pattern = "-Instance"
  33.            
  34.             'Reset oInstCheck
  35.             oInstCheck = "nothing"
  36.            
  37.             'Execute the regex on the selection
  38.             Set Matches = regEx.Execute(oSelection(i))
  39.             For Each Match in Matches
  40.                 oInstCheck = Match.Value
  41.             Next
  42.            
  43.             'If the selection doesnt end in -Instance and is a model then add it to the oCol Collection.
  44.             if oInstCheck <> "-Instance" and TypeName ( oSelection(i) ) = "Model" then
  45.            
  46.                 Logmessage oSelection(i) & " tastes like snozberries, added to oCol"
  47.                 oCol.add oSelection(i)
  48.                
  49.             else
  50.            
  51.                 Logmessage oSelection(i) & " tastes like rejection!"
  52.                
  53.             end if
  54.            
  55.         next
  56.  
  57.     else
  58.    
  59.         'If only one object is selected, make sure it is a model and add to the oCol Collection
  60.         if TypeName ( oSelection(0) ) = "Model" then   
  61.             oCol.add oSelection(0)
  62.        
  63.         else
  64.            
  65.             'If the selection isn't a model, send the user to the ModelCheck part of the script
  66.             set oSelecto = oSelection(0)
  67.             ModelCheck = "yes"
  68.  
  69.         end if
  70.    
  71.     end if
  72.    
  73. else
  74.  
  75.     'If no object is selected, start a picksession
  76.     LogMessage "Select a Model"
  77.     PickObject "Select a Model", " ", oSelecto
  78.  
  79.     if TypeName ( oSelection(0) ) = "Model" then
  80.    
  81.         oScriptCancel = "no"
  82.         oCol.add oSelecto
  83.        
  84.     else
  85.    
  86.         'If the selection isn't a model, send the user to the ModelCheck part of the script
  87.         ModelCheck = "yes"
  88.        
  89.     end if
  90.    
  91. end if
  92.  
  93. if ModelCheck = "yes" then
  94.    
  95.     if oSelecto.Model <> "Scene_Root" then
  96.  
  97.         LogMessage oSelecto.Model & " detected, could use that instead!"
  98.            
  99.         BP = XSIUIToolkit.Msgbox( oSelecto.Name & " isn't a model, but it IS part of " & oSelecto.Model & ", should I just use that ?", 4, "No Model Selected" )
  100.  
  101.             if BP = 6 then
  102.                                
  103.                 LogMessage "OK, adding " & oSelecto.Model & " to oCol for instantiation or duplication :)"
  104.                 oScriptCancel = "no"   
  105.                 oCol.Add oSelecto.Model
  106.                        
  107.             else
  108.  
  109.                 Logmessage "Script Cancelled - Next time select a Model instead of a " & TypeName( oSelecto ) & "! :)"
  110.                 oScriptCancel = "yes"
  111.                    
  112.             end if
  113.            
  114.     else
  115.                
  116.         LogMessage oSelecto & " isn't even part of a model!"
  117.            
  118.         BP = XSIUIToolkit.Msgbox( oSelecto & " isn't part of a model, should I create one now?", 4, "No Model Selected" )
  119.                    
  120.             if BP = 6 then
  121.                        
  122.                 LogMessage "Here goes, creating a model for " & oSelecto
  123.                            
  124.                 'Define a name for the new model
  125.                 oModelName = oSelecto & "-Model"
  126.                            
  127.                 DeselectAll
  128.                            
  129.                 'Create the new model using the oModelName as it's name :P
  130.                 Preferences.SetPreferenceValue "Interaction.autoinspect", false
  131.                 CreateModel , oModelName, , oModel
  132.                 Preferences.SetPreferenceValue "Interaction.autoinspect", true
  133.                            
  134.                 'Match the model's translation to the input object
  135.                 MatchTransform "B:" & oModel, oSelecto, siTrn
  136.  
  137.                 'Parent the input object to the new model
  138.                 ParentObj "B:" & oModel, oSelecto
  139.                            
  140.                 'Add the new model to oCol
  141.                 oCol.Add oModel
  142.                
  143.                 LogMessage oModel & " was created for " & oSelecto
  144.                 oScriptCancel = "no"
  145.                        
  146.             else
  147.                        
  148.                 Logmessage "Script Cancelled - Next time select a Model instead of a " & TypeName( oSelecto ) & "! :)"
  149.                 oScriptCancel = "yes"
  150.                        
  151.             end if
  152.                
  153.     end if
  154.    
  155. end if
  156.  
  157. if oCol.Count = 0 then
  158.  
  159.     BP = XSIUIToolkit.Msgbox( "No models found in collection, do you want to try selecting a model again ?", 4, "No Model Selected" )
  160.            
  161.         if BP = 6 then
  162.  
  163.             PickObject "Please select a Model", " ", oSelecto
  164.  
  165.             if TypeName( oSelecto ) = "Model" then         
  166.  
  167.                 LogMessage "That's more like it !, " & oSelecto & " was added to oCol for instantiation or duplication :)"
  168.                 oCol.Add oSelecto
  169.                 oScriptCancel = "no"               
  170.  
  171.             else
  172.  
  173.                 if oSelecto.Model <> "Scene_Root" then
  174.  
  175.                     LogMessage oSelecto.Model & " detected, could use that instead!"
  176.                    
  177.                     BP = XSIUIToolkit.Msgbox( oSelecto.Name & " isn't a model, but it IS part of " & oSelecto.Model & ", should I just use that ?", 4, "No Model Selected" )
  178.  
  179.                         if BP = 6 then
  180.                            
  181.                             LogMessage "OK, adding " & oSelecto.Model & " to oCol for instantiation or duplication :)"
  182.                             oScriptCancel = "no"   
  183.                             oCol.Add oSelecto.Model
  184.                        
  185.                         else
  186.  
  187.                             Logmessage "Script Cancelled - Next time select a Model instead of a " & TypeName( oSelecto ) & "! :)"
  188.                             oScriptCancel = "yes"
  189.                            
  190.                         end if
  191.                        
  192.                 else
  193.                
  194.                     LogMessage oSelecto & " isn't even part of a model!"
  195.                     BP = XSIUIToolkit.Msgbox( oSelecto & " isn't part of a model, should I create one now?", 4, "No Model Selected" )
  196.                        
  197.                         if BP = 6 then
  198.                        
  199.                             LogMessage "Creating a model for " & oSelecto
  200.                            
  201.                             'Define a name for the new model
  202.                             oModelName = oSelecto & "-Model"
  203.                            
  204.                             DeselectAll
  205.                            
  206.                             'Create the new model using the oModelName as it's name :P
  207.                             CreateModel , oModelName, , oModel
  208.                
  209.                             'Match the model's translation to the input object
  210.                             MatchTransform "B:" & oModel, oSelecto, siTrn
  211.  
  212.                             'Parent the input object to the new model
  213.                             ParentObj "B:" & oModel, oSelecto
  214.                            
  215.                             'Add the new model to oCol
  216.                             oCol.Add oModel
  217.                            
  218.                             LogMessage oModel & " was created for " & oSelecto
  219.                             oScriptCancel = "no"
  220.                        
  221.                         else
  222.                        
  223.                             Logmessage "Script Cancelled - Next time select a Model instead of a " & TypeName( oSelecto ) & "! :)"
  224.                             oScriptCancel = "yes"
  225.                        
  226.                         end if
  227.                
  228.                 end if
  229.                
  230.             end if
  231.        
  232.         else
  233.                    
  234.             oScriptCancel = "yes"
  235.        
  236.         end if
  237.  
  238. end if 
  239.  
  240. 'Create a collection to add new models to for selection at the end
  241. Set oFinalSelect = CreateObject( "XSI.Collection" )
  242.  
  243. if oScriptCancel = "no" then
  244.  
  245.     for i = 0 to (oCol.count-1)
  246.  
  247.         'Check if an instance of the selection exists
  248.         strPMname = oCol(i)&"-Instance"
  249.         Set oObj = Dictionary.GetObject( strPMname, false )
  250.  
  251.         if TypeName( oObj ) <> "Nothing" then
  252.  
  253.             'Branch select the selection and duplicate it
  254.             DeselectAll
  255.             AddToSelection oCol(i), "BRANCH"
  256.             oDupo = Duplicate (, , 2, 1, 1, 0, 0, 1, 0, 1, , , , , , , , , , , 0)
  257.             oDupo2 = replace (oDupo, "B:", "")
  258.                
  259.             'Instance the newly duplicated model
  260.             set oInsta = Instantiate ("B:" & oDupo, , 1, 1, 0, 1)
  261.                
  262.             'Scale Expressions
  263.             SetExpr oInsta&".kine.local.sclx", oDupo2+".kine.local.sclx*-1"
  264.             SetExpr oInsta&".kine.local.scly", oDupo2+".kine.local.scly"
  265.             SetExpr oInsta&".kine.local.sclz", oDupo2+".kine.local.sclz"
  266.          
  267.             'Rotation Expressions
  268.             SetExpr oInsta&".kine.local.rotx", oDupo2+".kine.local.rotx"
  269.             SetExpr oInsta&".kine.local.roty", oDupo2+".kine.local.roty*-1"
  270.             SetExpr oInsta&".kine.local.rotz", oDupo2+".kine.local.rotz*-1"
  271.          
  272.             'Translation Expressions
  273.             SetExpr oInsta&".kine.local.posx", oDupo2+".kine.local.posx*-1"
  274.             SetExpr oInsta&".kine.local.posy", oDupo2+".kine.local.posy"
  275.             SetExpr oInsta&".kine.local.posz", oDupo2+".kine.local.posz"
  276.                
  277.             'Rename the new instance
  278.             oNewName = replace (oInsta, "_Instance", "-Instance")
  279.             SetValue oInsta&".Name", oNewName
  280.  
  281.             SelectObj oDupo
  282.                        
  283.             LogMessage "Instance detected, " & oCol(i) & " was duplicated (" & oDupo2 & "), and given a new mirror instance (" & oNewName &")"
  284.             oFinalSelect.Add oDupo
  285.            
  286.         else
  287.            
  288.             'If there is no instance detected . . .
  289.             set oSelecto = oCol(i)
  290.        
  291.             if oScriptCancel = "no" then
  292.                
  293.                 'Instantiate each model in selection
  294.                 set oInsta = Instantiate ("B:" & oSelecto, , 1, 1, 0, 1)
  295.          
  296.                 'Scale Expressions
  297.                 SetExpr oInsta&".kine.local.sclx", oSelecto+".kine.local.sclx*-1"
  298.                 SetExpr oInsta&".kine.local.scly", oSelecto+".kine.local.scly"
  299.                 SetExpr oInsta&".kine.local.sclz", oSelecto+".kine.local.sclz"
  300.          
  301.                 'Rotation Expressions
  302.                 SetExpr oInsta&".kine.local.rotx", oSelecto+".kine.local.rotx"
  303.                 SetExpr oInsta&".kine.local.roty", oSelecto+".kine.local.roty*-1"
  304.                 SetExpr oInsta&".kine.local.rotz", oSelecto+".kine.local.rotz*-1"
  305.  
  306.                 'Translation Expressions
  307.                 SetExpr oInsta&".kine.local.posx", oSelecto+".kine.local.posx*-1"
  308.                 SetExpr oInsta&".kine.local.posy", oSelecto+".kine.local.posy"
  309.                 SetExpr oInsta&".kine.local.posz", oSelecto+".kine.local.posz"
  310.            
  311.                 'Rename the new instance
  312.                 oNewName = replace (oInsta, "_Instance", "-Instance")
  313.                 SetValue oInsta&".Name", oNewName
  314.                
  315.                 LogMessage "Created a mirror instance of " & oCol(i) & " (" & oNewName &")"
  316.                 oFinalSelect.Add oSelecto
  317.                
  318.             end if
  319.            
  320.         end if
  321.        
  322.     Next
  323.    
  324.     SelectObj oFinalSelect
  325.    
  326. end if
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement