View Full Version : Unseen by flags and bitwise operators

05-13-2013, 02:03 PM
I am having some issues with getting the proper values from an object's UnseenByRays, UnaffectedByFog, etc. Below is a snippet from the docs:

Object Info
class pcore.LWObjectInfo(*args)
flags(LWItemID item) → unsigned int
Returns the state of certain object settings as bits combined using bitwise-or. Possible flags are:

Currently I am receiving values however they are never 0 and 1. I am using the bitwise-or operator but it doesnt seem to be working as expected (I have also tried the bitwise-and operator and it still doesnt work). I am currently trying just to read the state of the different parameters and change it if the state isnt on. Here is a portion of the relevant code:

# Set target values
targetValue = 1
targetObject = "Box"

# Select all objects
# Get list of selected objects
objectsList = lwsdk.LWInterfaceInfo().selected_items()
# Loop through objects and look for objects to replace
for item in objectsList:
# Get the objects ID
theID = lwsdk.itemid_to_str(item)
# Get the objects name
objectName = (lwsdk.LWItemInfo().name(item))
# Compare target name to the current object
if targetObject.lower() in objectName.lower():
lwsdk.command('SelectItem %s' % theID )
obj_info = lwsdk.LWObjectInfo()
if executeCommand == 'unseenbyrays':
currentState = lwsdk.LWObjectInfo().flags(item) & lwsdk.LWOBJF_UNSEEN_BY_RAYS
if currentState != int(targetValue):

Does anyone happen to have any insight on what I am doing wrong? :) Also, is there a flag for UnseenByRadiosity? I couldn't find it in the Python or C SDK docs.


05-13-2013, 03:49 PM
currentState won't be equal to '1' unless LWOBJF_UNSEEN_BY_RAYS is in the first bit position. For instance, if the rays flag is in the third bit position '0100' and the incoming flags are '1101', then flags & rays will be '0100'. Try checking for zero/non-zero instead of equality to '1'.

05-13-2013, 04:16 PM
Interesting, thanks for the info. I guess I will have to dig into it a bit more and see what I come up with.

05-13-2013, 04:45 PM
I'm not a Python coder (yet) but in general, bit-flag tests are usually in the form of:

if ( flags & specific_condition_flag )
Code to be executed when flag is set goes here

This works because (in most languages) non-zero equates to true -- it won't matter which bit the specific flag is using. If the flag is not set, then the bitwise-AND will return 0 and the conditional will equate to 'false'.

05-14-2013, 03:58 PM
Thanks jrandom I got it working. :)