[Plugin] Add Named Scene
-
Hi all,I'm a newbie in ruby, although being here several years...
I made a little plugin that lets you give a name upon adding a new scene~Name : Named_Scene.rb
Date : v1.0 11/12/6
Description : To Let you rename it upon adding a new scene
Menu Item : View -> Add Named Scene
%(#a2a2a2)[Still have a problem, I want that: when clicking the menu or hot key, give you a small inputbox, and write the name, and create, you can also not change the default name, and add the new scene as normal~ The default name of the new scene can increase each time, I made the "Scene_Num" to do it but everytime it is "scene 1".
Who can teach me how to? ]
Thanks Thomthom~! I've updated the attachment below!
Now it works like this :
Hit the hotkey or click the menu item, give you an inputbox, you can choose to give the new a unique name, Or leave it to its default and create like normal: Scene 1- Scene 2- Scene 3-...
The first one define the new tab's name according to the number of existing pages:Finally make the second one that do exactly the same thing as the normal scene tool, scene 1 - scene 2 - scene 3 - scene 4 - ... ignoring the number of existing pages, %(#BF0000)[will not have any duplicate names! ]
& Use theFile;;basename(__FILE__)
to ignore its own file name.
# mirale999 first release, with Thomthom's help. # Permission to use, copy, modify, and distribute this software for # any purpose and without fee is hereby granted # Freely use # Name ; Named_Scene.rb # Date ; v1.0 11/12/6 # Description ; To Let you rename it upon adding a new scene # Menu Item ; View -> Add Named Scene # It works like this ; # Hit the hotkey or click the menu item, give you an inputbox, you can # choose to give the new a unique name, Or leave it to its default and # create like normal; Scene 1- Scene 2- Scene 3-... #---------------------------------------------------------------------------- require 'sketchup.rb' module Named_Scene # Add menu items. unless file_loaded? File;;basename(__FILE__) add_separator_to_menu("View") UI.menu("View").add_item("Add Named Scene") { self.add_namedScene } end @@scene_i = 1 def self.add_namedScene model = Sketchup.active_model pages = model.pages # Ask user for page name. list = [] prompts = ["Scene Name;"] defaults = ["Scene"+" "+"#{@@scene_i}" ] input = UI.inputbox( prompts, defaults, list, "Add Named Scene" ) # Check if the user cancelled. return if input==false # Get data from result array scene_name = input @@scene_i = @@scene_i + 1 status = pages.add( scene_name.to_s ) end end #----------------------------------------------------------------------------- file_loaded File;;basename(__FILE__) #-----------------------------------------------------------------------------
Hi all, after several years this small plugin has a bug because of the new version of ruby, and mariocha adds LanguageHandler in this version.The new modified version is here:
Named_Scene2.rbz -
@mirale999 said:
Still have a problem, I want that: when clicking the menu or hot key, give you a small inputbox, and write the name, and create, you can also not change the default name, and add the new scene as normal~ The default name of the new scene can increase each time, I made the "Scene_Num" to do it but everytime it is "scene 1".
` 'Scene1'.next
Scene2`
-
Ah - looking at your code now:
<span class="syntaxdefault"><br /></span><span class="syntaxcomment"># mirale999 first release<br /># Permission to use, copy, modify, and distribute this software for <br /># any purpose and without fee is hereby granted<br /># Freely use<br /># Name ; Named_Scene.rb<br /># Date ; v1.0 11/12/6<br /># Description ; To Let you rename it upon adding a new scene<br /># Menu Item ; View -> Add Named Scene<br />#----------------------------------------------------------------------------<br /><br /></span><span class="syntaxkeyword">require </span><span class="syntaxstring">'sketchup.rb'<br /> <br /></span><span class="syntaxdefault">Scene_Num</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">0<br /><br /><br />module Named_Scene<br /> <br /> unless file_loaded</span><span class="syntaxkeyword">?(</span><span class="syntaxstring">'Named_Scene.rb'</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxcomment"># Add menu items.<br /> </span><span class="syntaxdefault">add_separator_to_menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"View"</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"View"</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Add Named Scene"</span><span class="syntaxkeyword">) { </span><span class="syntaxdefault">self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">Add_NamedScene </span><span class="syntaxkeyword">}<br /> </span><span class="syntaxdefault">end<br /> <br /> <br /> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">Add_NamedScene<br /> model </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br /> pages </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pages<br /><br /> </span><span class="syntaxcomment"># Ask user for page name.<br /> </span><span class="syntaxkeyword">list=[]<br /> </span><span class="syntaxdefault">prompts</span><span class="syntaxkeyword">=[</span><span class="syntaxstring">"Scene Name;"</span><span class="syntaxkeyword">]<br /> </span><span class="syntaxdefault">defaults</span><span class="syntaxkeyword">=[(</span><span class="syntaxstring">"Scene"</span><span class="syntaxkeyword">+ </span><span class="syntaxdefault">Scene_Num</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s</span><span class="syntaxkeyword">) ]<br /> </span><span class="syntaxdefault">input</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">inputbox</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">prompts</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">defaults</span><span class="syntaxkeyword">, list, </span><span class="syntaxstring">"Add Named Scene" </span><span class="syntaxkeyword">)<br /> <br /> </span><span class="syntaxcomment"># Check if the user cancelled.<br /> </span><span class="syntaxkeyword">return if </span><span class="syntaxdefault">input</span><span class="syntaxkeyword">==</span><span class="syntaxdefault">false<br /> <br /> </span><span class="syntaxcomment"># Get data from result array<br /> </span><span class="syntaxdefault">scene_name </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">input<br /><br /><br /> status </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">pages</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add scene_name</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s<br /> end<br /> <br /> Scene_Num</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Scene_Num </span><span class="syntaxkeyword">+ </span><span class="syntaxdefault">1<br />end<br /><br /></span><span class="syntaxcomment">#-----------------------------------------------------------------------------<br /></span><span class="syntaxdefault">file_loaded</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Named_Scene.rb'</span><span class="syntaxkeyword">)<br /></span><span class="syntaxcomment">#-----------------------------------------------------------------------------<br /> </span><span class="syntaxdefault"></span>
Your
Scene
is a constant. But you are using it as a variable.You need to look into the difference between variables and constants. Initial capital letter means a constant in Ruby.
Here's an amended version:
<span class="syntaxdefault"><br /></span><span class="syntaxcomment"># mirale999 first release<br /># Permission to use, copy, modify, and distribute this software for <br /># any purpose and without fee is hereby granted<br /># Freely use<br /># Name ; Named_Scene.rb<br /># Date ; v1.0 11/12/6<br /># Description ; To Let you rename it upon adding a new scene<br /># Menu Item ; View -> Add Named Scene<br />#----------------------------------------------------------------------------<br /><br /></span><span class="syntaxkeyword">require </span><span class="syntaxstring">'sketchup.rb'<br /><br /><br /></span><span class="syntaxdefault">module Named_Scene<br /> <br /> unless file_loaded</span><span class="syntaxkeyword">?(</span><span class="syntaxstring">'Named_Scene.rb'</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxcomment"># Add menu items.<br /> </span><span class="syntaxdefault">add_separator_to_menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"View"</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"View"</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Add Named Scene"</span><span class="syntaxkeyword">) { </span><span class="syntaxdefault">self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_namedScene </span><span class="syntaxkeyword">}<br /> </span><span class="syntaxdefault">end<br /><br /> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_namedScene<br /> model </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br /> pages </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pages<br /><br /> </span><span class="syntaxcomment"># Ask user for page name.<br /> </span><span class="syntaxkeyword">list = []<br /> </span><span class="syntaxdefault">prompts </span><span class="syntaxkeyword">= [</span><span class="syntaxstring">"Scene Name;"</span><span class="syntaxkeyword">]<br /> </span><span class="syntaxdefault">defaults </span><span class="syntaxkeyword">= [(</span><span class="syntaxstring">"Scene#{pages.count}"</span><span class="syntaxkeyword">) ]<br /> </span><span class="syntaxdefault">input </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">inputbox</span><span class="syntaxkeyword">( </span><span class="syntaxdefault">prompts</span><span class="syntaxkeyword">, </span><span class="syntaxdefault">defaults</span><span class="syntaxkeyword">, list, </span><span class="syntaxstring">"Add Named Scene" </span><span class="syntaxkeyword">)<br /><br /> </span><span class="syntaxcomment"># Check if the user cancelled.<br /> </span><span class="syntaxkeyword">return if </span><span class="syntaxdefault">input</span><span class="syntaxkeyword">==</span><span class="syntaxdefault">false<br /><br /> </span><span class="syntaxcomment"># Get data from result array<br /> </span><span class="syntaxdefault">scene_name </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">input<br /><br /> status </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">pages</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add</span><span class="syntaxkeyword">( </span><span class="syntaxdefault">scene_name </span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">end<br /> <br />end<br /><br /></span><span class="syntaxcomment">#-----------------------------------------------------------------------------<br /></span><span class="syntaxdefault">file_loaded</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Named_Scene.rb'</span><span class="syntaxkeyword">)<br /></span><span class="syntaxcomment">#-----------------------------------------------------------------------------<br /> </span><span class="syntaxdefault"></span>
I make the default name of the scene be made out of the number of existing pages in the current model. If you do it your way, with instance variables instead of constants, then it would just keep on counting between models.
I also cleaned up your indentation. Always keep your source code clean and organized, good indentation makes it easier to read - which is essential to debug and maintain your code.
-
@thomthom said:
Ah - looking at your code now:
Your
Scene
is a constant. But you are using it as a variable.You need to look into the difference between variables and constants. Initial capital letter means a constant in Ruby.
Here's an amended version:
I make the default name of the scene be made out of the number of existing pages in the current model. If you do it your way, with instance variables instead of constants, then it would just keep on counting between models.
I also cleaned up your indentation. Always keep your source code clean and organized, good indentation makes it easier to read - which is essential to debug and maintain your code.
Thanks Thomas~!! I use your code and now it's a correct version!
And the Scene name is the very same as the normal one~ -
But.... if you delete a scene or two you'll end up with duplicate names. So if you want to avoid that, then you need to make some extra checks.
-
@thomthom said:
But.... if you delete a scene or two you'll end up with duplicate names. So if you want to avoid that, then you need to make some extra checks.
Done!
-
Hum.. This plugin is a very good idea. Often wished this would be the native way to do.
But for me it the bracket and quote signs are unwanted?
See scene 555 attached.
-
@mariocha said:
Hum.. This plugin is a very good idea. Often wished this would be the native way to do.
But for me it the bracket and quote signs are unwanted?
See scene 555 attached.this is because the plugin has been written with a previous version of ruby
instead of
# Get data from result array scene_name = input
put
# Get data from result array scene_name = input[0]
-
Good Job on your plugin, We are often creating scenes, and it is a pain to not be able name it once created. When creating a scene, the scene name it add includes [""], It would be really helpful to me if it could be modified, so that the scene name is only what you enter into the field, maybe I am doing something wrong, please advise if you would
thanks
Chris -
@chrisconcise said:
Good Job on your plugin, We are often creating scenes, and it is a pain to not be able name it once created. When creating a scene, the scene name it add includes [""], It would be really helpful to me if it could be modified, so that the scene name is only what you enter into the field, maybe I am doing something wrong, please advise if you would
thanks
ChrisI need exactly this.
Advertisement