Texture rotation in the Inventor API

by Vladimir Ananyev

Q: How can I access the rotation and scale data of the texture?

All these values are stored in the appearance asset in the Inventor document.  An asset is a collection of values of different types. They can be simple types like floats, integers, strings, and Booleans, but they can also be more complex types like color, filename, reference to other assets, textures, and choices. In this case we need to find texture and access its values.

The following VBA sample prints all values stored in the AssetTexture object from the active Appearance object.

Sub AssetTextureSample()
    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveDocument
    Dim oAppearance As Asset
    Set oAppearance = oDoc.ActiveAppearance
 
    Dim oValue As AssetValue
    For Each oValue In oAppearance
 
        If oValue.ValueType = AssetValueTypeEnum.kAssetValueTextureType Then
 
            Dim oTextureAssetValue As TextureAssetValue
            Set oTextureAssetValue = oValue
            Dim oTexture As AssetTexture
            Set oTexture = oTextureAssetValue.value
 
            If oTexture.Item("unifiedbitmap_Bitmap").value  "" Then
                Debug.Print "---------------------------"
                Debug.Print oAppearance.DisplayName
                Debug.Print "---------------------------"
                Dim v As Variant
                For Each v In oTextureAssetValue.value
                    Debug.Print v.name, v.value
                Next
                Debug.Print "---------------------------" & vbNewLine
            End If
            
        End If  'texture
    Next   'AssetValue
 
    Beep
End Sub

Output:

---------------------------
Birch - Natural Polished
---------------------------
AssetLibID    AFEFC330-5E61-4E24-814F-AE810148B79D
ExchangeGUID  
common_Shared_Asset         common_shared
common_Tint_color            50383104 
common_Tint_toggle          False
texture_LinkTextureTransforms             False
texture_MapChannel           1 
texture_MapChannel_ID_Advanced             1 
texture_MapChannel_UVWSource_Advanced      0 
texture_OffsetLock          False
texture_RealWorldOffsetX     0 
texture_RealWorldOffsetY     0 
texture_RealWorldScaleX      10 
texture_RealWorldScaleY      10
texture_ScaleLock           True
texture_UOffset              0 
texture_URepeat             True
texture_UScale               1 
texture_UVScale              1 
texture_VOffset              0 
texture_VRepeat             True
texture_VScale               1 
texture_WAngle               0
unifiedbitmap_Bitmap        1/Mats/Woods & Plastics.Finish Carpentry.Wood.Red Birch.png
unifiedbitmap_Blur           0,01 
unifiedbitmap_Blur_Offset    0 
unifiedbitmap_Filtering      0 
unifiedbitmap_Invert        False
unifiedbitmap_RGBAmount      1 
---------------------------

Highlighted members are responsible for the texture rotate angle (texture_Wangle) and scale (texture_RealWorldScaleX and texture_RealWorldScaleY).  Adding the lines below to the VBA sample you may get the current angle and scale data in more convenient format: 

If oTexture.Item("unifiedbitmap_Bitmap").value  "" Then
    Debug.Print "---------------------------"
    Debug.Print oAppearance.DisplayName
    Debug.Print "---------------------------"
    Debug.Print "    unified bitmap path:  ", _
        oTexture.Item("unifiedbitmap_Bitmap").value
    Debug.Print "    Rotation angle,deg (texture_WAngle): " _
        & oTexture.Item("texture_WAngle").value
    Debug.Print "    X Size    (texture_RealWorldScaleX): " _
        & oTexture.Item("texture_RealWorldScaleX").value & "  " _
        & oTexture.Item("texture_RealWorldScaleX").Units
    Debug.Print "    Y Size    (texture_RealWorldScaleY): " _
        & oTexture.Item("texture_RealWorldScaleY").value & "  " _
        & oTexture.Item("texture_RealWorldScaleY").Units
    Debug.Print "---------------------------" & vbNewLine
End If

Output:

---------------------------
Birch - Natural Polished
    unified bitmap path:    1/Mats/Woods & Plastics.Finish Carpentry.Wood.Red Birch.png
    Rotation angle,deg (texture_WAngle): 30
    X Size    (texture_RealWorldScaleX): 10  Inch
    Y Size    (texture_RealWorldScaleY): 10  Inch
---------------------------

Note: Rotation angle is represented in degrees in the range 0° – 360°.

Rotation and scale properties are read and write, so we may change them.  Attached part document (Inventor 2016 format) contains the iLogic rule that rotates the active texture according the current value on the slider in the iLogic form:

3-1

3-2

Here is the iLogic code:

' this iLogic rule rotates the active texture in the range 0° - 360°
' User parameter TextureAngle is controlled by the slider on the iLogic form
DimoDocAsPartDocument = ThisDoc.Document
DimoAppearanceAsAsset = oDoc.ActiveAppearance
DimoValueAsAssetValue
ForEachoValueInoAppearance
    IfoValue.ValueType = AssetValueTypeEnum.kAssetValueTextureTypeThen
        DimoTextureAssetValueAsTextureAssetValue = oValue
        DimoTextureAsAssetTexture = oTextureAssetValue.Value
        IfoTexture.Item("unifiedbitmap_Bitmap").Value  ""Then
              oTexture.Item("texture_WAngle").Value = TextureAngle
        EndIf
    EndIf
Next

 

Download TestPlate

 

Please refer “Materials and Appearances” overview in the Inventor API Help for additional information on this topic.


Comments

3 responses to “Texture rotation in the Inventor API”

  1. I have tried this in Inventor 2017 and your part works as expected, however when I change to one of my own materials the code no longer works.
    Is there a specific requirement in your material/Colour set up that enables this?

  2. Ngoc Son Avatar
    Ngoc Son

    Hello Vladimir Ananyev,
    Is there any way to get the texture direction such as parallel with the X-axis, …?
    Many thanks.
    BR/NgocSon

  3. The X axis of what? The X axis of the model? I think at 0 angle the texture is parallel with or following the U direction of the surface it’s used on. If the question is how to “find out” if the direction is parallel with X axis, then you could do that based on the U direction of the surface. If the question is how to “make” the texture direction parallel with the X axis, then you would need to create separate texture for each surface as their U direction will be different.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading