Randomized Surface Mapping script?

Would like to ask if there is a script that randomizes the UVW repeat of a surface mapping?
I am trying to achieve a different UVW surface mapping on same objects.

Thank you

Hi Yu_Heng - it looks like this may be possible using RhinoCommon, but I have not messed with that area of RC before, so I’m not sure how easy that will be.

-Pascal

1 Like

@Yu_Heng,

below is a simplified script which can be used in Rhino 5 to randomize UV Repeat and UV Offset of multiple surfaces. Note that you have to apply a mapping before using it. Even if surfaces use surface mapping by default, you have to apply it once before using this script.

RandomizeSurfaceTextureUV.py (1.4 KB)

Currently this script randomizes the UV Offset (see line 24 and 25) and the UV Repeat (line 28 and 29). If you only want to randomize Offset or Repeat individually, open the script in the python editor using _RunPythonScript and comment out what you do not need using a # symbol.

@pascal, i’ve not been able to make this run in the WIP, so it would be great if you can give it a try. Please note my comment at line 36.

thanks,
c.

Hi Clement - I’ll take a look - it may not be for a couple of days, I am out of the office with a class this week.

-Pascal

Hi @pascal, ok. Have a nice teaching week !

c.

Hi Clement - I see that has changed - I guess I’d add a check for version number -

        if Rhino.RhinoApp.Version > 5.0:
            scriptcontext.doc.Objects.Replace(ids[i], obj.Geometry)

-Pascal

@pascal,

thanks for checking this. Is this the only way ? It feels rather buggy in the WIP, if i’ve used Replace once on the object, i can comment out the Replace code and it seems to work without it…

c.

Hi Pascal, any news why this does not run in RH6 ?

_
c.

Clement, sorry - I dropped the ball here - I will check it now.

https://mcneel.myjetbrains.com/youtrack/issue/RH-49440

-Pascal

1 Like

Hi @pascal, thanks for looking at this. I am not able to make the above script do anything in V6. So the workaround does not work anymore. I am curious how the script needs to be changed to make it work in V5 and V6.

_
c.

Hi Clement - the workaround worked here…- the commented line. Let me test some more…

@clement - actually, here, today, if I apply say planar mapping to a surface I do not need the ReplaceObject() call in V6 - the script works with that line commented.

-Pascal

Hi Pascal, OK, i’ve found that the one i had saved here is different from the one i posted above. Instead of the commented line, the saved one got this:

if Rhino.RhinoApp.Version > 5.0:
   scriptcontext.doc.Objects.Replace(ids[i], obj.Geometry)

When i close V6, reopen it, create a plane, assign new material, assign texture, assign planar mapping and run the script, nothing is changed. Do you see something different ?

Appart from this, why doesn`t this work with surface mapping out of the box ?

_
c.

Hi Clement - Offsetting does not work at all (Rhino UI) on surface mapped objects as far as I can see right now.

I’ll try your changed script in a moment here…

Ah, OK - @clement - use Rhino.RhinoApp.ExeVersion not just ‘Version’. Version gives more info, ExeVersion just the one number.

-Pascal

1 Like

Hi Pascal, yes this what i just figured out now too it must be ExeVersion

thanks,
c.

@clement - the script seems to work as expcted now in V6… can you verify?

thanks,

-Pascal

Hi Pascal, sorry no. This line of code is still required:

if Rhino.RhinoApp.Version > 5.0:
   scriptcontext.doc.Objects.Replace(ids[i], obj.Geometry)

Even with that code, it needs 2 runs until i see a change in the mapping. I’ve applied a planar mapping before running it btw.

_
c.

Hi Clement OK - thanks for testing - this works here, first time, every time in SR 10, SR11 and SR12 - this code, and file:
RandoTexture.3dm (212.7 KB)

# randomize uv texture offset and repeat (c) 2017, Clement Greiner - CG3D

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
import random

def RandomizeSurfaceTextureUV():
    
    msg = "Select surface to randomize UV Texture"
    ids = rs.GetObjects(msg, 8, True, True, True)
    if not ids: return
    
    rh_objs = [rs.coercerhinoobject(i, True, True) for i in ids]
    
    for i, obj in enumerate(rh_objs):
        mapping = obj.GetTextureMapping(1)
        if not mapping:
            print "No mapping ?"
        else:
            xform = mapping.UvwTransform
            
            # random offset (float range between 0.0 and 1.0)
            xform.M03 = random.random() # U-Offset
            xform.M13 = random.random() # V-Offset
            
            # random repeat (integer range between 1 and 10)
            xform.M00 = random.randint(1, 10) # U-Repeat
            xform.M11 = random.randint(1, 10) # V-Repeat
            
            mapping.UvwTransform = xform
            #obj.SetTextureMapping(1, mapping)
            obj.CommitChanges()
            scriptcontext.doc.Objects.ModifyTextureMapping(obj, 1, mapping)
            
            # required in Rhino 6 ??? Works without this in V5:
            #scriptcontext.doc.Objects.Replace(ids[i], obj.Geometry)
        
    scriptcontext.doc.Views.Redraw()

RandomizeSurfaceTextureUV()
1 Like

Hi @pascal, thanks for testing. It now works in V6 and still in V5 :wink:

_
c.

1 Like

:+1:

Superb. Thanks for letting me know.

-Pascal