Make Unique Texture
-
Is there an API equivalent to the interactive context menu item for a face called "Make Unique Texture"?
Also, is there a known problem with Texture Writer reducing the resolution of Textures? When we write out an OpenFlight file, we try to use the original images to create the OpenFlight rgb files. But if the original images are not available, we resort to using the Texture Writer. Our customers complain the there is a loss of resolution when we use the Texture Writer. Is there any way to control the resolution?
-
I think there is no API equivalent, see this statement from Aerilius.
-
@spring.freediver said:
Is there any way to control the resolution?
I do not see a way specifically.
Are you specifying a low-loss filetype such as png ?
-
I did find this in the API. Worth a try if the material is textured.
-
There is no direct API equivalent to making a unique texture.
However, if you make a 'texturewriter' and use it to export the texture file, using the selected face.materail as the 'source', then it will retain any distorted UVmapping etc.
Therefore, you can extract the distorted texture image file for the selected face.material to a TEMP location.
Then you simply 'clone' [make] a new material... based exactly on the original material's spec [RBGA etc]... BUT you instead use the new texture-image file [using the original's texture size etc too]...
So although it's not "in" the API you can readily write a 'reusable' method along these lines to make a new unique material for any selected [distorted] face...
There is [currently] no API method to write [or read] SKM files so you will need to manually save any model materials externally, OR you can install/use my "SKMtools", which add those functionalities to the API.
Here is a method that makes a face's material unique and returns the new material [or none is no texture].
It is now part of my SKMtools/materials missing API methods [but it is as yet unpublished]... If you give it an optional argument 'true' the new material's texture is made as a .png format file rather than the mimicking the original's file-type...class Sketchup;;Face if not Sketchup;;Face.method_defined?(;material_make_unique) def material_make_unique(png=false) mat=self.material return nil unless mat return nil unless txt=mat.texture iwi=txt.width ihi=txt.height ### later we use ### texture.size=[w, h] tads=txt.attribute_dictionaries ### nam=mat.display_name alp=mat.alpha col=mat.color mads=mat.attribute_dictionaries ### mats=Sketchup.active_model.materials namp=nam+"1" namp.next! while mats[namp] num=namp.gsub(/^#{nam}/,"") nmat=mats.add(namp) ### tw=Sketchup.create_texture_writer tmp=ENV["TEMP"] ### it's a PC == 'TEMP' or 'TMP' tmp=ENV["TMPDIR"] unless tmp ### it's a MAC tna=File.basename(txt.filename, ".*")+num+File.extname(txt.filename) tna=File.basename(tna, ".*")+".png" if png pth=File.join(tmp, tna).tr("\\","/") tw.load(self, true) tw.write(self, true, pth) nmat.alpha=alp nmat.color=col mads.each{|d| d.each_pair{|k,v| nmat.set_attribute(d.name, k, v)}} if mads nmat.texture=pth ntxt=nmat.texture ntxt.size=[iwi, ihi] tads.each{|d| d.each_pair{|k,v| ntxt.set_attribute(d.name, k, v)}} if tads return nmat end end end#class Face
You are advised not to mess with a base class but it should give you enough code hints to make your own tool to do this...
-
Yea, please do not mess with the base classes.
-
For the avoidance of doubt --- here's a base-class free version...
Please implement something like this...<span class="syntaxdefault"></span><span class="syntaxcomment">### pass a 'face' as the first argument<br />### and a second optional argument to convert the image texture into a PNG format<br />### it returns the new material, or 'nil' if the face is incorrect<br />### or if there's no face.material.texture...<br /></span><span class="syntaxdefault">def material_make_unique</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">face</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">nil</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> png</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">false</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> return nil unless face<br /> return nil unless face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> return nil unless mat</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material<br /> return nil unless txt</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">mat</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">texture<br /> iwi</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">txt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">width<br /> ihi</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">txt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">height<br /> </span><span class="syntaxcomment">### later we use ### texture.size=[w, h]<br /></span><span class="syntaxdefault"> tads</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">txt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">attribute_dictionaries<br /> </span><span class="syntaxcomment">###<br /></span><span class="syntaxdefault"> nam</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">mat</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">display_name<br /> alp</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">mat</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">alpha<br /> col</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">mat</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">color<br /> mads</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">mat</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">attribute_dictionaries<br /> </span><span class="syntaxcomment">###<br /></span><span class="syntaxdefault"> mats</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">materials<br /> namp</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">nam</span><span class="syntaxkeyword">+</span><span class="syntaxstring">"1"<br /></span><span class="syntaxdefault"> namp</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">next</span><span class="syntaxkeyword">!</span><span class="syntaxdefault"> while mats</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">namp</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> num</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">namp</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">gsub</span><span class="syntaxkeyword">(/^</span><span class="syntaxcomment">#{nam}/,"")<br /></span><span class="syntaxdefault"> nmat</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">mats</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">namp</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">###<br /></span><span class="syntaxdefault"> tw</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">create_texture_writer<br /> tmp</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">ENV</span><span class="syntaxkeyword">[</span><span class="syntaxstring">"TEMP"</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxcomment">### it's a PC == 'TEMP' or 'TMP'<br /></span><span class="syntaxdefault"> tmp</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">ENV</span><span class="syntaxkeyword">[</span><span class="syntaxstring">"TMPDIR"</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> unless tmp </span><span class="syntaxcomment">### it's a MAC<br /></span><span class="syntaxdefault"> tna</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">txt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">filename</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">".*"</span><span class="syntaxkeyword">)+</span><span class="syntaxdefault">num</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">extname</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">txt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">filename</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> tna</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">tna</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">".*"</span><span class="syntaxkeyword">)+</span><span class="syntaxstring">".png"</span><span class="syntaxdefault"> if png<br /> pth</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">join</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">tmp</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> tna</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">tr</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"\\"</span><span class="syntaxkeyword">,</span><span class="syntaxstring">"/"</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> tw</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">load</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">face</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> true</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> tw</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">write</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">face</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> true</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> pth</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">###<br /></span><span class="syntaxdefault"> nmat</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">alpha</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">alp<br /> nmat</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">color</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">col<br /> mads</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each_pair</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">k</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">v</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> nmat</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">set_attribute</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> k</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> v</span><span class="syntaxkeyword">)}}</span><span class="syntaxdefault"> if mads<br /> nmat</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">texture</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">pth<br /> ntxt</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">nmat</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">texture<br /> ntxt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">size</span><span class="syntaxkeyword">=[</span><span class="syntaxdefault">iwi</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> ihi</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> tads</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each_pair</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">k</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">v</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> ntxt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">set_attribute</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> k</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> v</span><span class="syntaxkeyword">)}}</span><span class="syntaxdefault"> if tads<br /> return nmat<br />end<br /></span>
Advertisement