Import DAE, place it to center and save SKP
-
Hi guys,
I am familiar with Python in C4D, but I was asked to do a small script for SketchUp, so I have to fight with Ruby and SketchUp itself So I apologize for some newbie questions:
I need to batch import DAE files and save it to SKP. I have found some sample scripts around here, so batch import is working fine. But after import, object does not get placed in scene, but I can find it in compoments.
if file_ext==".dae" newFilePath = f + "/" + file_name + ".skp" Sketchup.active_model.import(File.join(f,d),false) Sketchup.active_model.save(newFilePath) entities.clear! end
Q1: Sketchup.active_model.save saves empty scene, which is expected, because model is not in the scene. Is there any way to place imported object to world center, make object active and save the scene?
Q2: upon import of DAE, can I change info of the component - like its name and description?
Thank a lot for your help.
Stan
-
We are missing some of your code.
Here's a simplistic example to import dae, place an instance and export the definition...<span class="syntaxdefault">model</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start_operation</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'DAE import'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> true</span><span class="syntaxkeyword">)<br /></span><span class="syntaxcomment"># block to ensure defn gets cleared on commit.<br /></span><span class="syntaxdefault">dae</span><span class="syntaxkeyword">=</span><span class="syntaxstring">"full_path_to_file.dae"<br /></span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">import</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">dae</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> false</span><span class="syntaxkeyword">)<br /></span><span class="syntaxcomment"># you can also assemble a hash to set options etc and pass that instead...<br /></span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">send_action</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'selectSelectionTool;'</span><span class="syntaxkeyword">)<br /></span><span class="syntaxcomment"># this activates another tool and stops manual placement option<br /></span><span class="syntaxdefault">defn</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definitions</span><span class="syntaxkeyword">[-</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]<br /></span><span class="syntaxcomment"># this is the newest [unused] component definition<br /># We can place an instance with;<br />#inst=model.active_entities.add_instance(defn, ORIGIN)<br /># IF it's needed ! Remove inintial #<br /># BUT for now let's assume you don't want or need it !<br /></span><span class="syntaxdefault">defn</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">basename</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">dae</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">".*"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxcomment"># or any other name<br /></span><span class="syntaxdefault">defn</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">description</span><span class="syntaxkeyword">=</span><span class="syntaxstring">"Imported from DAE"</span><span class="syntaxdefault"> </span><span class="syntaxcomment"># or other text<br /></span><span class="syntaxdefault">skp</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">dae</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">gsub</span><span class="syntaxkeyword">(/[.]</span><span class="syntaxdefault">dae</span><span class="syntaxkeyword">$/,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'.skp'</span><span class="syntaxkeyword">)<br /></span><span class="syntaxcomment"># use .skp in lieu of .dae in file-name and <br /></span><span class="syntaxdefault">defn</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">save_as</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">skp</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">defn</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">clear</span><span class="syntaxkeyword">!</span><span class="syntaxdefault"> </span><span class="syntaxcomment"># delete unwanted defn IF NO inst added<br /></span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">commit_operation<br /></span>
See API guidance pages, e.g.
http://ruby.sketchup.com/Sketchup/Model.html#import-instance_method
http://ruby.sketchup.com/Sketchup/ComponentDefinition.html -
Hi TIG,
sorry for the late reply - I have been busy finishing another project project. I have just tested your code and it works perfectly! Thank you million times!
Best regards,
Stan
Advertisement