Hello,
I try to import .dae file generated by Sketchup via ruby extension but i get error “Import failed”
My error is not due to my dae files, (i export them from Sketchup). It’s specific to my ruby code extension i think.
Good to know…
…and do you have a question?
Can you provide a sample model and some simple code that reproduce this? Otherwise it’s very hard to guess at what might be going on.
Also, what SketchUp version and platform are you seeing this on?
model=Sketchup.active_model
model.start_operation('DAE import', true)
dae="one_stair.dae"
imp = model.import(dae, false)
Sketchup.send_action('selectSelectionTool:')
defn=model.definitions[-1]
inst=model.active_entities.add_instance(defn, ORIGIN)
defn.name=File.basename(dae, ".*")
defn.description="Imported from DAE"
defn.entities.clear!
Here is my code
My OS: Windows
Sketchup Version: 2021
How to import .dae files on sketchup via ruby’s code?
Try not to use send actions in automated code. (It is mainly for toolbar buttons.)
To activate the SelectionTool, instead use:
Sketchup.active_model.select_tool(nil)
REF:
I do not have this file in the current directory path of my active hard drive, so when I run your code, I also get the “Import failed” messagebox.
In Ruby, if you do not specify an absolute path, then file operations will default to using the current directory path, which is …
Dir.pwd
So a simple test is …
File.exist?(dae)
… to which I get false
on my system.
So you must pass an valid absolute pathname to an existing dae
file.
… OR you can temporarily change the directory to where you know the dae
file resides …
Dir.chdir(my_exports) {
imp = model.import(dae, false)
}
Thanks, i dont have any error message, but i cant see model on sketchup, here the new code
model=Sketchup.active_model model.start_operation('DAE import', true) dae="one_stair.dae" Dir.chdir("C:/Users/mattda/Documents/test_dae") { imp = model.import(dae, false) print(dae) } Sketchup.active_model.select_tool(nil) defn=model.definitions[-1] inst=model.active_entities.add_instance(defn, ORIGIN) defn.name=File.basename(dae, ".*") # or any other name defn.description="Imported from DAE" # or other text defn.entities.clear! model.commit_operation
No one will be able to test unless you post the dae
test file.
one_stair.zip (1.2 MB)
The dae test file
You can give a full path of the file like this as well e.g.:
dae="C:/Users/mattda/Documents/test_dae/one_stair.dae"
Since you are on SU2021, you should use the option hash as described here:
Model.import-instance_method
and the options of the different file types you can find here: file.importer_options. So for dae
is like this:
options = { :validate_dae => true,
:merge_coplanar_faces => true,
:show_summary => true }
Therefor the import method should look like :
imp = model.import(dae, options)
I have no idea why you are selecting the Select tool, but there is no influence to the import is fail or not…
The #clear! method is used to remove all entities from the collection of entities. Meaning that, you are clearing all of the entities of the imported component definition and you will get an empty component instance too. Actually an imported component is there, but without any geometry in it.
Therefore you will see nothing in a modell. However you can still “hunt” for it in Outliner or in a Component tray.
I would remove this line from the code.
__
__
As a summary this code snippet should work if the one_stair.dae
file there and the moquette.jpg
is in a subfolder of one_stair
like:
model=Sketchup.active_model
model.start_operation('DAE import', true)
dae="C:/Users/mattda/Documents/test_dae/one_stair.dae"
options = { :validate_dae => true,
:merge_coplanar_faces => true,
:show_summary => true }
imp = model.import(dae, options)
# What is this for?
Sketchup.active_model.select_tool(nil)
defn=model.definitions[-1]
defn.name=File.basename(dae, ".*") # or any other name
defn.description="Imported from DAE" # or other text
inst=model.active_entities.add_instance(defn, ORIGIN)
# Do not remove the entities!
#defn.entities.clear!
model.commit_operation
Yeah it’s work the code
model=Sketchup.active_model
model.start_operation(‘DAE import’, true)
dae=“C:/Users/mattda/Documents/test_dae/one_stair.dae”
options = { :validate_dae => true,
:merge_coplanar_faces => true,
:show_summary => true }
imp = model.import(dae, options)#It’s for deactivate the manual positionning of the imported model
Sketchup.active_model.select_tool(nil)defn=model.definitions[-1]
inst=model.active_entities.add_instance(defn, ORIGIN)
defn.name=File.basename(dae, “.*”) # or any other name
defn.description=“Imported from DAE” # or other text
model.commit_operation