UVCOMMAND question?



  • Hi - I'm working on a Python script that applies a texture to a model and I'd like to move the UV around after it's applied. The script works if I set the c4d layout to my version of UV editing tools but if I'm in a modeling layout the script is unable to return the handle when calling bodypaint.GetActiveUVSet(). Is there a way to to set the active UV without being in a UV editing layout?

    Here are some relevant parts of the script -

    def UVbag(model, settings):
        doc.SetActiveObject(model)
    
        # Retrieves active UVSet
        handle = bodypaint.GetActiveUVSet(doc, c4d.GETACTIVEUVSET_ALL)
        if not handle:
            print "No active UVSet!"
            return
    
        # Retrieves UVW list
        uvw = handle.GetUVW()
        if uvw is None:
            return
    
        # Calls UVCOMMAND_TRANSFORM to change UVW list
        ret = bodypaint.CallUVCommand(handle.GetPoints(), handle.GetPointCount(), handle.GetPolys(), handle.GetPolyCount(), uvw,
                                      handle.GetPolySel(), handle.GetUVPointSel(), model, handle.GetMode(), c4d.UVCOMMAND_TRANSFORM, settings)
        if not ret:
            print "CallUVCommand() failed!"
            return
    
        # Sets the transformedUVW from Texture View 
        if handle.SetUVWFromTextureView(uvw, True, True, True):
            pass
        else:
            print "UVW from Texture View failed to be set!"
    
        # Releases active UVSet
        bodypaint.FreeActiveUVSet(handle)
    
    

    snippet from main part of script -

    
        tags = newObj.GetTags()
        #adjust UV map to match our png layouts
        # Builds UVCOMMAND_TRANSFORM container for the command settings
        settings = c4d.BaseContainer()
        for tag in tags:
            if tag.GetType() == 5673:
                polys = newObj.GetPolygonS()
                polys.DeselectAll()
                if tag.GetName() in ["back"]:
                    tagSelection = tag.GetBaseSelect()
                    tagSelection.CopyTo(polys)    
                    c4d.EventAdd()
                    settings[c4d.UVCOMMAND_TRANSFORM_MOVE_Y] = .25
                    settings[c4d.UVCOMMAND_TRANSFORM_SCALE_X] = 1
                    settings[c4d.UVCOMMAND_TRANSFORM_SCALE_Y] = -.5
                    UVbag(newObj, settings)#
                if tag.GetName() == "Face 1":
                    tagSelection = tag.GetBaseSelect()
                    tagSelection.CopyTo(polys)
                    c4d.EventAdd()
                    settings[c4d.UVCOMMAND_TRANSFORM_MOVE_Y] =  -.25
                    settings[c4d.UVCOMMAND_TRANSFORM_SCALE_X] = 1
                    settings[c4d.UVCOMMAND_TRANSFORM_SCALE_Y] = .5
                    UVbag(newObj, settings)              
    

    Any help or insight is appreciated.



  • ...just realized it's not the layout that makes it work, I need to have the UV Polygons tool activated. I'm going to see if I can activate that tool via python.


  • Global Moderator

    Hi @del first of all welcome in the plugincafe community!

    As you figured it out, to have GetActiveUVSet to work you have to open at least one time a TextureView and in your case set the document mode to Muvpolygons.

    Both can be done with.

    c4d.CallCommand(170103) # New Texture View...
            
    #Enter UV Polygon mode
    doc.SetMode(c4d.Muvpolygons)
    

    Note it's also possible to edit UV directly from UV tag. While you can't use Bodypaint command, it may be an option for you since you only do translation work. For more information please read this thread: https://plugincafe.maxon.net/topic/10915/14369_using-uvcommand-with-multiple-objects/2

    If you have any question, please let me know.
    Cheers,
    Maxime



  • Thank you for the guidance. I'll dig into that info.