PDA

View Full Version : How to access Surface and Texture settings via Python



mitsura
06-06-2012, 10:35 AM
Hi,

I want write a Python script to change the surface settings of all objects in a scene depending on the current surface settings of the objects. Import are settings like Blending Mode and Layer Opacity and Invert Layer

The code would look like this:
- get all objects in the scene and store them in a dictionary
- iterate over each object in the dictionary
- for each object:
- get all surfaces
- iterate over each surface
- per surface iterate over each layer
- check specific layer settings and change if conditions are met

So, I need some idea how to get access the surface/texture settings for any given object.

I was looking the C SDK and found a Handler called LWTextureAccess but I cannot find anything concerning surfaces. Although this handler has a link to the objID but I do not see where it fits between the object - surface - texture layer

Any help pointing me in the right direction would be very much appreciated.

Yves.

mitsura
06-08-2012, 04:24 PM
By found the info I needed in the C SDK.

Here is the rough version of the script for those who are interested (I hope the formatting does not get lost):

#! /usr/bin/env python
# -*- Mode: Python -*-
# -*- coding: ascii -*-

"""
This code will iterate over each layer of the Surface Color of the selected objects and
if the layer is an Image Map set the Blending Mode to Normal if it was set to Multiply.
NOTE: I needed this for changing scenes generated by the Poser plug-in
"""

import sys
import lwsdk


__author__ = ""
__date__ = ""
__copyright__ = ""
__version__ = ""
__maintainer__ = ""
__email__ = ""
__status__ = ""
__lwver__ = ""



class change_poser_scene(lwsdk.IGeneric):
def __init__(self, context):
super(change_poser_scene, self).__init__()

# LWGeneric -------------------------------------------
def process(self, ga):

# Get all selected objects
interface_info = lwsdk.LWInterfaceInfo()
selected_items = interface_info.selected_items()

# For each object get the list of the object's surfaces
for obj in selected_items:

objFilename = lwsdk.LWObjectInfo().filename(obj)
surfaceIDs = lwsdk.LWSurfaceFuncs().byObject(objFilename)

# Look at the Color thingy for each surface
for surfaceID in surfaceIDs:
surfaceName = lwsdk.LWSurfaceFuncs().name(surfaceID)

# Get the ID for the Texture assigned to the Color (if any)
colorTextureID = lwsdk.LWSurfaceFuncs().getTex( surfaceID, lwsdk.SURF_COLR)

# Iterate over all layers in the Texture
layerID = lwsdk.LWTextureFuncs().firstLayer(colorTextureID)
while layerID:
# From lwtxtr.h
# define TLT_IMAGE 0
# define TLT_PROC 1
# define TLT_GRAD 2
# Get the type of each layer and check if it is an Image map
colorTextureLayerType = lwsdk.LWTextureFuncs().layerType(layerID)

if colorTextureLayerType == lwsdk.TLT_IMAGE:
# If it is indeed an image map, get the Blending Mode
blendingMode = lwsdk.LWTextureFuncs().getParam(layerID, lwsdk.TXTAG_BLEND)

if blendingMode[1] == lwsdk.TXBLN_MULTIPLY:
# Change blending mode to Normal if it was set to Multiply
print "Setting BlendingMode from multiply to normal for the image layer on the color channel for surface: " + surfaceName + " on object: " + objFilename
lwsdk.LWTextureFuncs().setParam(layerID, lwsdk.TXTAG_BLEND, lwsdk.TXBLN_NORMAL)

layerID = lwsdk.LWTextureFuncs().nextLayer(colorTextureID, layerID)

return lwsdk.AFUNC_OK

ServerTagInfo = [
( "Python Reset Blending Mode", lwsdk.SRVTAG_USERNAME | lwsdk.LANGID_USENGLISH ),
( "Reset Blending Mode", lwsdk.SRVTAG_BUTTONNAME | lwsdk.LANGID_USENGLISH ),
( "Utilities/Python", lwsdk.SRVTAG_MENU | lwsdk.LANGID_USENGLISH )
]

ServerRecord = { lwsdk.GenericFactory("LW_PyChangePoserScene", change_poser_scene) : ServerTagInfo }

mitsura
06-08-2012, 04:30 PM
#! /usr/bin/env python
# -*- Mode: Python -*-
# -*- coding: ascii -*-

"""
This code will iterate over each layer of the Surface Color of the selected objects and
if the layer is an Image Map set the Blending Mode to Normal if it was set to Multiply.
NOTE: I needed this for changing scenes generated by the Poser plug-in
"""

import sys
import lwsdk


__author__ = ""
__date__ = ""
__copyright__ = ""
__version__ = ""
__maintainer__ = ""
__email__ = ""
__status__ = ""
__lwver__ = ""



class change_poser_scene(lwsdk.IGeneric):
def __init__(self, context):
super(change_poser_scene, self).__init__()

# LWGeneric -------------------------------------------
def process(self, ga):

# Get all selected objects
interface_info = lwsdk.LWInterfaceInfo()
selected_items = interface_info.selected_items()

# For each object get the list of the object's surfaces
for obj in selected_items:

objFilename = lwsdk.LWObjectInfo().filename(obj)
surfaceIDs = lwsdk.LWSurfaceFuncs().byObject(objFilename)

# Look at the Color thingy for each surface
for surfaceID in surfaceIDs:
surfaceName = lwsdk.LWSurfaceFuncs().name(surfaceID)

# Get the ID for the Texture assigned to the Color (if any)
colorTextureID = lwsdk.LWSurfaceFuncs().getTex( surfaceID, lwsdk.SURF_COLR)

# Iterate over all layers in the Texture
layerID = lwsdk.LWTextureFuncs().firstLayer(colorTextureID)
while layerID:
# From lwtxtr.h
# define TLT_IMAGE 0
# define TLT_PROC 1
# define TLT_GRAD 2
# Get the type of each layer and check if it is an Image map
colorTextureLayerType = lwsdk.LWTextureFuncs().layerType(layerID)

if colorTextureLayerType == lwsdk.TLT_IMAGE:
# If it is indeed an image map, get the Blending Mode
blendingMode = lwsdk.LWTextureFuncs().getParam(layerID, lwsdk.TXTAG_BLEND)

if blendingMode[1] == lwsdk.TXBLN_MULTIPLY:
# Change blending mode to Normal if it was set to Multiply
print "Setting BlendingMode from multiply to normal for the image layer on the color channel for surface: " + surfaceName + " on object: " + objFilename
lwsdk.LWTextureFuncs().setParam(layerID, lwsdk.TXTAG_BLEND, lwsdk.TXBLN_NORMAL)

layerID = lwsdk.LWTextureFuncs().nextLayer(colorTextureID, layerID)

return lwsdk.AFUNC_OK

ServerTagInfo = [
( "Python Reset Blending Mode", lwsdk.SRVTAG_USERNAME | lwsdk.LANGID_USENGLISH ),
( "Reset Blending Mode", lwsdk.SRVTAG_BUTTONNAME | lwsdk.LANGID_USENGLISH ),
( "Utilities/Python", lwsdk.SRVTAG_MENU | lwsdk.LANGID_USENGLISH )
]

ServerRecord = { lwsdk.GenericFactory("LW_PyChangePoserScene", change_poser_scene) : ServerTagInfo }