Help me solve a mystery
-
Using Ruby Web Console, I created a method to generate a Herringbone pattern on a selected face.
When finished and all was working as expected, I copied the code to a plugin file. But, unlike when executed from Ruby Web Console, the code produced errors, as listed below, when executed.=begin su8 errors h_bone Error; #<NoMethodError; undefined method `explode' for nil;NilClass> C;\Users\Public\SketchUp\Plugins\h_bone.rb;39 C;\Users\Public\SketchUp\Plugins\h_bone.rb;36;in `upto' C;\Users\Public\SketchUp\Plugins\h_bone.rb;36;in `h_bone' C;\Users\Public\SketchUp\Plugins\h_bone.rb;35;in `upto' C;\Users\Public\SketchUp\Plugins\h_bone.rb;35;in `h_bone' (eval);15 su2014 errors h_bone Error; #<TypeError; reference to deleted Entities> C;/Users/Public/SketchUp/Plugins/h_bone.rb;38;in `add_instance' C;/Users/Public/SketchUp/Plugins/h_bone.rb;38;in `block (2 levels) in h_bone' C;/Users/Public/SketchUp/Plugins/h_bone.rb;36;in `upto' C;/Users/Public/SketchUp/Plugins/h_bone.rb;36;in `block in h_bone' C;/Users/Public/SketchUp/Plugins/h_bone.rb;35;in `upto' C;/Users/Public/SketchUp/Plugins/h_bone.rb;35;in `h_bone' <main>;in `<main>' -e;1;in `eval' nil =end
Thinking that some of the code cleanup I had done had something to do with it, I pasted the code between a def and end statement in a new file, loaded and executed that. But the results were the same. So the question is, why would the exact same code executed from Ruby Web Console succeed while executed from a loaded plugin fail?
-
A new clue in the mystery. If the components exist before the plugin is executed, it runs without a problem. So it is something regarding the initial creation of the components in the plugin that apparently doesn't occur when the Ruby Web Console script is executed.
-
if you simply wrap it as a string [ in the .rb file with eval ] and load that, does it work?
i.e
eval %Q(# you original code here)
also, if you post the code it's easier to test...
john
-
I would not trust my old Ruby Web Console plugin. There were name-space issues if I remember correctly.
One simple way to write and test code in SketchUp is to use your favorite editor. Then create a menu or tool button in SketchUp to load the file you are editing. The advantge of a menu is that you can create a shortcut to reload the file. It's a simple, fast, and effective workflow.
-
Apparently the only mystery is why creating components "on the fly" is only partially successful at best.
-
I could think of 2 things related to those errors. You are referencing face.normal in the add instance method. BUT using pushpull on the face after referencing to a variable may delete the original face/faceID or reference what you may call it.
Also maybe using load definitions from paths might bring more succes than add ?
-
Jolran is correct.
When you usef.pushpull()
the original face is lost.
So when you later try to use a reference to that face 'f
' it is a 'deleted-entity' - raising an error...
But since your container's entities only has the one face with that normal you can re-find the face if needed - or just set a reference to the normal [say asn=f.normal
] BEFORE doing the pushpull that deletes the face... -
In this case, I don't attempt to reference the face later. Also, if the plugin doesn't have to create the components, it ran normally.
I tried creating a group for each "box" then group.to_component. This was a better but still unreliable.
I eventually ended up just adding the "boxes" to a single group rather than making components at all. This then created another problem which was resolved in the test plugin by doing nothing more than simply moving the .add_group statement. I can't imagine why that should make a difference.
The good news is that all the gremlins have been eliminated and all is right with the world.
-
The old webconsole may have done some initializing of local varibales that your method does not have.
Such as:
model = Sketchup::active_model defns = model.definitions selset = model.selection
etc.
Look at the code for the webconsole.
-
@sdmitch said:
In this case, I don't attempt to reference the face later.
Yes you did.. you tried to create a transform using
f.normal
so that method would likely returnnil
or raise an exception.Or the
add_instance
method would returnnil
making theci
referencenil
.Obviously you expect a valid instance or group when you use the
explode
method, but the reference (that you are callingexplode
upon,) is referencingnil
.Learn to write nil tests / valid test into your code to catch these errors, ie:
ci.explode if ci
-
Is this editor will have the same behavior?
http://www.alexschreyer.net/projects/sketchup-ruby-code-editor/
the author said it was based on the Ruby Web Console
Advertisement