Complex scripting/plugin - to create a component library
-
I've managed to mock some files up and test it...
I think this works now### usage; LukeR.importer module LukeR def self.importer() model=Sketchup.active_model defs=model.definitions #model.start_operation("importer",true) ### Bugsplats with image.explode !!! ### PURGE UNUSED COMPONENTS [NOT NEEDED IF YOU DO IT MANUALLY FIRST] defs.purge_unused ### SETUP LIST OF NAMES etc cnames=[] defs.each{|d|cnames << d.name if File.extname(d.name).downcase == '.dwg'} cnames.sort! ### ADJUST TO YOUR OWN PATHS fj='P;\02 General\Resource Library\People\JPEG' ### for images ff='P;\02 General\Resource Library\People' ### for saved SKPs #fj='C;\Users\TIG\Desktop\SKPs\PNGs' #ff='C;\Users\TIG\Desktop\SKPs' ### PROCESS NAMES... cnames.each{|name| puts name ### SET UP defn etc ### COUNT EDGES d=defs[name] count=0 bb=Geom;;BoundingBox.new d.entities.each{|e| bb.add(e.bounds) count+=1 if e.is_a?(Sketchup;;Edge) } ### ADD IMAGE TEXTURES NB; '.png' img=d.entities.add_image(File.join(fj, File.basename(name, ".*")+".png"), [-1.mm, -1.mm, 0], 2.mm+bb.width, 2.mm+bb.height) next unless img img.explode t=Geom;;Transformation.new() count.times{ d.entities.intersect_with(true, t, d.entities, t, true, d.entities.to_a) } ### repeat to ensure all splits... ### REMOVE img's UNWANTED PERIMETER togos=[] d.entities.each{|e|togos << e if e.valid? and e.is_a?(Sketchup;;Edge) and e.faces.length<2} d.entities.erase_entities(togos) ### ENSURE FACES ARE 'UP' & SET LAYERING hidn=model.layers.add("People Outlines")### or whatever name you like! peep=model.layers.add("People Images")### or whatever name you like! d.entities.each{|e|e.reverse! if e.is_a?(Sketchup;;Face) && e.normal!=Z_AXIS; e.layer=hidn if e.is_a?(Sketchup;;Edge); e.layer=peep if e.is_a?(Sketchup;;Face);} ### STAND COMPONENT'S CONTENTS TO BE VERTICAL tr=Geom;;Transformation.rotation(ORIGIN, X_AXIS, 90.degrees) d.entities.transform_entities(tr, d.entities.to_a) #=begin ### REMOVE ANY 'HOLES' faces=[] d.entities.each{|e|faces << e if e.is_a?(Sketchup;;Face)} (faces.length-1).times{ for face in faces if face.valid? for edgeuse in face.outer_loop.edgeuses if not edgeuse.partners[0] ### outermost face faces = faces - [face] loops = face.loops for loop in loops for fac in faces if fac.valid? and (fac.outer_loop.edges - loop.edges) == [] faces = faces - [fac] fac.erase! if fac.valid? ### fac abutts kept face so it must be erased... end #if fac end #for fac end #for loop end #if outermost end #for edgeuse end #if valid end #for face }#times #=end ### ALL FACES GET MAIN MATERIAL faces=[] d.entities.each{|e|faces << e if e.is_a?(Sketchup;;Face)} mat=nil faces.each{|e|mat=e.material if e.material} faces.each{|e| e.material=mat e.back_material=mat } ### MOVE ORIGIN TO BOTTOM-CENTER OF OUTLINE tr=Geom;;Vector3d.new(-bb.center.x, 0, 0) d.entities.transform_entities(tr, d.entities.to_a) ### MAKE IT A FACE-ME COMPONENT #d.behavior.always_face_camera=true ### FINALLY... SAVE IT INTO 'PEOPLE' AS A' SKP' d.refresh_thumbnail d.save_as(File.join(ff, File.basename(name, ".*")+".skp")) }#names #model.commit_operation puts puts 'Done.' puts return true end#def end#module
NOTE: the image files' PNG and folder names you might want to adjust...
-
Brilliant thanks TIG, sorry I didnt get back to you - not much free time yesterday.
Will test today at some point.I have also thought of a better solution for matching the images up with the geometry. originally the image and the dwg are the same - the dwg is just live traced from the image. So if I keep the image border the bounding box of the dwg when imported will perfectly match the texture image in sketchup. I originally removed this frame in illustrator but realised this is removed in su anyway and therefore it is making it worse not better silly me.
Just wondering, is there anyway to scale the geometry with constrained proportions to a set height? (ok if not) but at the moment illustrator is sort of a half manual, half automatic process for scaling which takes time.
-
scrap the scaling !
Managed to add the scaling in the images (canvas size in photoshop) before it goes to a dwg - I think it will keep its scaling -
OK TIG!
This looks like it could be the last edit! (sorry there was a lot of work!! - I really appreciate it and will make sure I share the work on the warehouse after getting permission from vyonyx)
The images are all mapping perfectly now!
So the issue is that now I have added a frame from the original jpeg for the bounding box to reference its size correctly to (as notede 2 post ago). I think the could requires editing to delete this external frame as well. At the moment I believe that the following is happening:
- The image is made slightly bigger (good as it just makes them a little sharper at least).
- The image is exploded and frame removed but as it is 1mm bigger the other frame now in the components is not deleted.
Summary - I think we need to add another delete for the outer frame as well as the img exploded frame.
Good news is the remove holes seem to be working well! (just inverted at the moment as the second outer frame remains)
-
If you duplicate this code
### REMOVE img's UNWANTED PERIMETER togos=[] d.entities.each{|e|togos << e if e.valid? and e.is_a?(Sketchup;;Edge) and e.faces.length<2} d.entities.erase_entities(togos)
immediately after itself... then first time it runs the outer perimeter edges of the original image are deleted - it was actually added 1mm bigger all round than the original outline's size to ensure its edges were separated from the outline's. The second go it erases the 'frame' of the outline so you have the 'real' outline left - frankly this 'frame' seems superfluous to me
The potential problem is that if this 'frame' shares any horizontal/vertical short edges [that you will actually want to be kept because they are outlining the desired form] then it simply screws up, because they'll go too and no face loop is left for later processing, AND we have no way of knowing which of these single-faced edges is which... so don't even ask how we can do it...
If you don't have this 'frame' the result is fine without the second step.
Alternatively add the 'frame' as being a discontinuous loop - perhaps just the max-x/y dims on two sides only, which then gets erases in the first run to the perimeter deletion code anyway, because these edges can never have any faces so ..faces.length<2 applies
BUT if you must have this continuous looped 'frame', then its edges can only touch the main outline's edges at points, any 'coincident' edges - no matter how short - will get removed in the second run, and then the facing of the discontinuous outline's edge will fail dismally... -
sorry just posted at the same time you did so didnt see the above
- The extra frame is to make the bounding box of the component match the texture. As you saw before, because these textures (pngs) are not cut out properly, the bounding box is different to the bounding box of the geometry component. so causing a mis match in the mapping of the texture to the geometry. This solves this.
However yes you are right in the fact that this extra frame would mess things up IF the geometry we want to keep is the same. however this only happens when the image is cut off at the edge of the png file. And I don't care for these images as they wont be used. i.e. is you have a person that is cropped off and not whole, I wouldnt put this in a model. Some of the trees do this, but they wouldnt be used anyway and would be deleted.
A SOLUTION - if we needed one, i.e. if some of the geometry is the same as the frame.
I could extent the canvas size of the original image via photoshop at the start. Hence extending the frame beyond the boundaries of the image we want to keep.EDIT: Also just noticed the face me code wasnt put in again. Copied this back in
NOW TO PROCESS THESE! (overnight) -
EDIT!
SORTED TIG, I just copied it againheres the final Code.
One LAST thing - How easy is it to apply this to a folder of seperate Skp files rather than active components? Onyl because it takes about 15 minutes to process a single component and when they are all (or some of them about 50 or so) in one model it slows down considerably and I would leave it on overnight to do a batch. Probably not necesary but just be interested in your thoughts to improve the processing speed?
FINAL CODE:
### usage; LukeR.importer module LukeR def self.importer() model=Sketchup.active_model defs=model.definitions #model.start_operation("importer",true) ### Bugsplats with image.explode !!! ### PURGE UNUSED COMPONENTS [NOT NEEDED IF YOU DO IT MANUALLY FIRST] defs.purge_unused ### SETUP LIST OF NAMES etc cnames=[] defs.each{|d|cnames << d.name if File.extname(d.name).downcase == '.dwg'} cnames.sort! ### ADJUST TO YOUR OWN PATHS fj='P;\02 General\Resource Library\People\PNG' ### for images ff='P;\02 General\Resource Library\Sketchup People' ### for saved SKPs #fj='C;\Users\TIG\Desktop\SKPs\PNGs' #ff='C;\Users\TIG\Desktop\SKPs' ### PROCESS NAMES... cnames.each{|name| puts name ### SET UP defn etc ### COUNT EDGES d=defs[name] count=0 bb=Geom;;BoundingBox.new d.entities.each{|e| bb.add(e.bounds) count+=1 if e.is_a?(Sketchup;;Edge) } ### ADD IMAGE TEXTURES NB; '.png' img=d.entities.add_image(File.join(fj, File.basename(name, ".*")+".png"), [-1.mm, -1.mm, 0], 2.mm+bb.width, 2.mm+bb.height) next unless img img.explode t=Geom;;Transformation.new() count.times{ d.entities.intersect_with(true, t, d.entities, t, true, d.entities.to_a) } ### repeat to ensure all splits... ### REMOVE img's UNWANTED PERIMETER togos=[] d.entities.each{|e|togos << e if e.valid? and e.is_a?(Sketchup;;Edge) and e.faces.length<2} d.entities.erase_entities(togos) ### REMOVE outline's UNWANTED PERIMETER togos=[] d.entities.each{|e|togos << e if e.valid? and e.is_a?(Sketchup;;Edge) and e.faces.length<2} d.entities.erase_entities(togos) ### ENSURE FACES ARE 'UP' & SET LAYERING hidn=model.layers.add("People Outlines")### or whatever name you like! peep=model.layers.add("People")### or whatever name you like! d.entities.each{|e|e.reverse! if e.is_a?(Sketchup;;Face) && e.normal!=Z_AXIS; e.layer=hidn if e.is_a?(Sketchup;;Edge); e.layer=peep if e.is_a?(Sketchup;;Face);} ### STAND COMPONENT'S CONTENTS TO BE VERTICAL tr=Geom;;Transformation.rotation(ORIGIN, X_AXIS, 90.degrees) d.entities.transform_entities(tr, d.entities.to_a) ### MAKE IT A FACE-ME COMPONENT d.behavior.always_face_camera=true #=begin ### REMOVE ANY 'HOLES' faces=[] d.entities.each{|e|faces << e if e.is_a?(Sketchup;;Face)} (faces.length-1).times{ for face in faces if face.valid? for edgeuse in face.outer_loop.edgeuses if not edgeuse.partners[0] ### outermost face faces = faces - [face] loops = face.loops for loop in loops for fac in faces if fac.valid? and (fac.outer_loop.edges - loop.edges) == [] faces = faces - [fac] fac.erase! if fac.valid? ### fac abutts kept face so it must be erased... end #if fac end #for fac end #for loop end #if outermost end #for edgeuse end #if valid end #for face }#times #=end ### ALL FACES GET MAIN MATERIAL faces=[] d.entities.each{|e|faces << e if e.is_a?(Sketchup;;Face)} mat=nil faces.each{|e|mat=e.material if e.material} faces.each{|e| e.material=mat e.back_material=mat } ### MOVE ORIGIN TO BOTTOM-CENTER OF OUTLINE tr=Geom;;Vector3d.new(-bb.center.x, 0, 0) d.entities.transform_entities(tr, d.entities.to_a) ### MAKE IT A FACE-ME COMPONENT #d.behavior.always_face_camera=true ### FINALLY... SAVE IT INTO 'PEOPLE' AS A' SKP' d.refresh_thumbnail d.save_as(File.join(ff, File.basename(name, ".*")+".skp")) }#names #model.commit_operation puts puts 'Done.' puts return true end#def end#module
-
TIG, this is working perfectly now - I compressed the images a bit more and they import much faster now so thats good.
Bar just one more thing:
Can we change the axis/origin to the center of the people? at the moment the insertion point when adding the components into SU is a bit below the people by about 150mm or so. Would just be nice to get them to be inserted at the correct level. Got any code for this?Heres the current module:
### usage; Importer.people module Importer def self.people() model=Sketchup.active_model defs=model.definitions #model.start_operation("people",true) ### Bugsplats with image.explode !!! ### PURGE UNUSED COMPONENTS [NOT NEEDED IF YOU DO IT MANUALLY FIRST] defs.purge_unused ### SETUP LIST OF NAMES etc cnames=[] defs.each{|d|cnames << d.name if File.extname(d.name).downcase == '.dwg'} cnames.sort! ### ADJUST TO YOUR OWN PATHS fj='P;\02 General\Resource Library\People\PNG' ### for images ff='P;\02 General\Resource Library\Sketchup People' ### for saved SKPs #fj='C;\Users\TIG\Desktop\SKPs\PNGs' #ff='C;\Users\TIG\Desktop\SKPs' ### PROCESS NAMES... cnames.each{|name| puts name ### SET UP defn etc ### COUNT EDGES d=defs[name] count=0 bb=Geom;;BoundingBox.new d.entities.each{|e| bb.add(e.bounds) count+=1 if e.is_a?(Sketchup;;Edge) } ### ADD IMAGE TEXTURES NB; '.png' img=d.entities.add_image(File.join(fj, File.basename(name, ".*")+".png"), [-1.mm, -1.mm, 0], 2.mm+bb.width, 2.mm+bb.height) next unless img img.explode t=Geom;;Transformation.new() count.times{ d.entities.intersect_with(true, t, d.entities, t, true, d.entities.to_a) } ### repeat to ensure all splits... ### REMOVE img's UNWANTED PERIMETER togos=[] d.entities.each{|e|togos << e if e.valid? and e.is_a?(Sketchup;;Edge) and e.faces.length<2} d.entities.erase_entities(togos) ### REMOVE outline's UNWANTED PERIMETER togos=[] d.entities.each{|e|togos << e if e.valid? and e.is_a?(Sketchup;;Edge) and e.faces.length<2} d.entities.erase_entities(togos) ### ENSURE FACES ARE 'UP' & SET LAYERING hidn=model.layers.add("People Outlines")### or whatever name you like! peep=model.layers.add("People")### or whatever name you like! d.entities.each{|e|e.reverse! if e.is_a?(Sketchup;;Face) && e.normal!=Z_AXIS; e.layer=hidn if e.is_a?(Sketchup;;Edge); e.layer=peep if e.is_a?(Sketchup;;Face);} ### STAND COMPONENT'S CONTENTS TO BE VERTICAL tr=Geom;;Transformation.rotation(ORIGIN, X_AXIS, 90.degrees) d.entities.transform_entities(tr, d.entities.to_a) ### MAKE IT A FACE-ME COMPONENT d.behavior.always_face_camera=true #=begin ### REMOVE ANY 'HOLES' faces=[] d.entities.each{|e|faces << e if e.is_a?(Sketchup;;Face)} (faces.length-1).times{ for face in faces if face.valid? for edgeuse in face.outer_loop.edgeuses if not edgeuse.partners[0] ### outermost face faces = faces - [face] loops = face.loops for loop in loops for fac in faces if fac.valid? and (fac.outer_loop.edges - loop.edges) == [] faces = faces - [fac] fac.erase! if fac.valid? ### fac abutts kept face so it must be erased... end #if fac end #for fac end #for loop end #if outermost end #for edgeuse end #if valid end #for face }#times #=end ### ALL FACES GET MAIN MATERIAL faces=[] d.entities.each{|e|faces << e if e.is_a?(Sketchup;;Face)} mat=nil faces.each{|e|mat=e.material if e.material} faces.each{|e| e.material=mat e.back_material=mat } ### MOVE ORIGIN TO BOTTOM-CENTER OF OUTLINE tr=Geom;;Vector3d.new(-bb.center.x, 0, 0) d.entities.transform_entities(tr, d.entities.to_a) ### MAKE IT A FACE-ME COMPONENT #d.behavior.always_face_camera=true ### FINALLY... SAVE IT INTO 'PEOPLE' AS A' SKP' d.refresh_thumbnail d.save_as(File.join(ff, File.basename(name, ".*")+".skp")) }#names #model.commit_operation puts puts 'Done.' puts return true end#def def self.trees() model=Sketchup.active_model defs=model.definitions #model.start_operation("trees",true) ### Bugsplats with image.explode !!! ### PURGE UNUSED COMPONENTS [NOT NEEDED IF YOU DO IT MANUALLY FIRST] defs.purge_unused ### SETUP LIST OF NAMES etc cnames=[] defs.each{|d|cnames << d.name if File.extname(d.name).downcase == '.dwg'} cnames.sort! ### ADJUST TO YOUR OWN PATHS fj='P;\02 General\Resource Library\Trees\PNG' ### for images ff='P;\02 General\Resource Library\Sketchup Trees' ### for saved SKPs #fj='C;\Users\TIG\Desktop\SKPs\PNGs' #ff='C;\Users\TIG\Desktop\SKPs' ### PROCESS NAMES... cnames.each{|name| puts name ### SET UP defn etc ### COUNT EDGES d=defs[name] count=0 bb=Geom;;BoundingBox.new d.entities.each{|e| bb.add(e.bounds) count+=1 if e.is_a?(Sketchup;;Edge) } ### ADD IMAGE TEXTURES NB; '.png' img=d.entities.add_image(File.join(fj, File.basename(name, ".*")+".png"), [-1.mm, -1.mm, 0], 2.mm+bb.width, 2.mm+bb.height) next unless img img.explode t=Geom;;Transformation.new() count.times{ d.entities.intersect_with(true, t, d.entities, t, true, d.entities.to_a) } ### repeat to ensure all splits... ### REMOVE img's UNWANTED PERIMETER togos=[] d.entities.each{|e|togos << e if e.valid? and e.is_a?(Sketchup;;Edge) and e.faces.length<2} d.entities.erase_entities(togos) ### REMOVE outline's UNWANTED PERIMETER togos=[] d.entities.each{|e|togos << e if e.valid? and e.is_a?(Sketchup;;Edge) and e.faces.length<2} d.entities.erase_entities(togos) ### ENSURE FACES ARE 'UP' & SET LAYERING hidn=model.layers.add("Tree Outlines")### or whatever name you like! peep=model.layers.add("Trees")### or whatever name you like! d.entities.each{|e|e.reverse! if e.is_a?(Sketchup;;Face) && e.normal!=Z_AXIS; e.layer=hidn if e.is_a?(Sketchup;;Edge); e.layer=peep if e.is_a?(Sketchup;;Face);} ### STAND COMPONENT'S CONTENTS TO BE VERTICAL tr=Geom;;Transformation.rotation(ORIGIN, X_AXIS, 90.degrees) d.entities.transform_entities(tr, d.entities.to_a) ### MAKE IT A FACE-ME COMPONENT d.behavior.always_face_camera=true #=begin ### REMOVE ANY 'HOLES' faces=[] d.entities.each{|e|faces << e if e.is_a?(Sketchup;;Face)} (faces.length-1).times{ for face in faces if face.valid? for edgeuse in face.outer_loop.edgeuses if not edgeuse.partners[0] ### outermost face faces = faces - [face] loops = face.loops for loop in loops for fac in faces if fac.valid? and (fac.outer_loop.edges - loop.edges) == [] faces = faces - [fac] fac.erase! if fac.valid? ### fac abutts kept face so it must be erased... end #if fac end #for fac end #for loop end #if outermost end #for edgeuse end #if valid end #for face }#times #=end ### ALL FACES GET MAIN MATERIAL faces=[] d.entities.each{|e|faces << e if e.is_a?(Sketchup;;Face)} mat=nil faces.each{|e|mat=e.material if e.material} faces.each{|e| e.material=mat e.back_material=mat } ### MOVE ORIGIN TO BOTTOM-CENTER OF OUTLINE tr=Geom;;Vector3d.new(-bb.center.x, 0, 0) d.entities.transform_entities(tr, d.entities.to_a) ### MAKE IT A FACE-ME COMPONENT #d.behavior.always_face_camera=true ### FINALLY... SAVE IT INTO 'PEOPLE' AS A' SKP' d.refresh_thumbnail d.save_as(File.join(ff, File.basename(name, ".*")+".skp")) }#names #model.commit_operation puts puts 'Done.' puts return true end#def end#module
-
This bit of the code moves the component's origin to be at the bottom-center of the image-that's-now-geometry...
### MOVE ORIGIN TO BOTTOM-CENTER OF OUTLINE tr=Geom::Vector3d.new(-bb.center.x, 0, 0) d.entities.transform_entities(tr, d.entities.to_a)
At this point we have already stood it up 'on its end'.
The 'vector' moves everything across in the 'X', to the center [note how it's a -ve value].
To move if down you change the 'Z' value.
It can only be easily done by a set distance.
So to move everything down in every imported set, by 150mm, we make it this...
### MOVE ORIGIN TO BOTTOM-CENTER OF OUTLINE tr=Geom::Vector3d.new(-bb.center.x, 0, **-150.mm**) d.entities.transform_entities(tr, d.entities.to_a)
Note the value's format
-150**.**mm
because the base-units in SketchUp are inches, so-6
would have a similar result at 6" [actually that is 152.mm]... -
Nice one thanks, I had an inkling that it would have to be a set value, I'll drop loads in and find the best value to change them to and process again.
We are now using these and its working very well. Gets nicer images without a long post-processing in photoshop.
Thanks again TIG
-
Processed all the people last night and works REALLY WELL.
Thanks again TIG!
Advertisement