MotionBuilder Forum
Welcome to Autodesk’s MotionBuilder Forums. Share your knowledge, ask questions, and explore popular MotionBuilder topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Why: Different Python objects for the same Object in MotionBuilder

2 REPLIES 2
Reply
Message 1 of 3
vsnappy1
492 Views, 2 Replies

Why: Different Python objects for the same Object in MotionBuilder

I'm stuck and confused.  What can cause MoBu to enter a state where it returns different python objects for the same object in Mobu.  EG:

 

import pyfbsdk
 
pyfbsdk.FBApplication().FileNew()
 
def run_test():
   my_cube = pyfbsdk.FBModelCube('my_cube')
 
   comp_list = pyfbsdk.FBComponentList()
   pyfbsdk.FBFindObjectsByName( my_cube.Name, comp_list, False, True )
   my_cube_found_by_name = comp_list[0]
 
   print 'my_cube.FullName = \t\t\t', my_cube.FullName
   print 'my_cube object = \t\t\t', my_cube
   print 'my_cube_found_by_name.FullName = \t', my_cube_found_by_name.FullName
   print 'my_cube_found_by_name object = \t', my_cube_found_by_name
   print 'my_cube == my_cube_found_by_name : {0}'.format( my_cube ==       my_cube_found_by_name )
 
   return my_cube, my_cube_found_by_name
 
run_test()

 on a fresh session of MotionBuilder returns:

 

>>> my_cube.FullName = Model::my_cube

>>> my_cube object = <pyfbsdk.FBModelCube object at 0x000000003B996638>

>>> my_cube_found_by_name.FullName = Model::my_cube

>>> my_cube_found_by_name object = <pyfbsdk.FBModelCube object at 0x000000003B996638>

>>> my_cube == my_cube_found_by_name : True

 

However, after a while of running other scripts, I get this:

 

>>> my_cube.FullName = Model::my_cube

>>> my_cube object = <pyfbsdk.FBModelCube object at 0x000000003B919A98>

>>> my_cube_found_by_name.FullName = Model::my_cube

>>> my_cube_found_by_name object = <pyfbsdk.FBModelCube object at 0x000000003B9252E8>

>>> my_cube == my_cube_found_by_name : False

 

Anyone have explanatin, ideas, or suggetions?

Kelly Snapka
Principal Artist
www.dsvolition.com/
2 REPLIES 2
Message 2 of 3
vsnappy1
in reply to: vsnappy1

Autodesk was kind enough to look into this with me. After some back and forth, we narrowed it down to a callback that I had active. Specifically, there is an issue with storing the node, found via event.ChildComponent, into a variable(global, namespaced, or otherwise).  Storing the name of the node is fine, just not the python object.  This workaround is enough for my needs, but as I understand it the issue is being reported as a bug.

 

Here is some sample code that reproduces the problem...

 

import pyfbsdk

##pyfbsdk.FBApplication().FileNew()
sys = pyfbsdk.FBSystem()
scn = sys.Scene
new_nodes = {}

def run_test():   
        my_cube = pyfbsdk.FBModelCube('my_cube')
        
        comp_list = pyfbsdk.FBComponentList()
        pyfbsdk.FBFindObjectsByName( my_cube.Name, comp_list, False, True )
        my_cube_found_by_name = comp_list[0]
        
        print 'my_cube.FullName = \t\t\t', my_cube.FullName
        print 'my_cube object = \t\t\t', my_cube
        print 'my_cube_found_by_name.FullName = \t', my_cube_found_by_name.FullName
        print 'my_cube_found_by_name object = \t', my_cube_found_by_name
        print 'my_cube == my_cube_found_by_name : {0}\n'.format( my_cube == my_cube_found_by_name )
        
        return my_cube, my_cube_found_by_name

def my_callback( control, event ):
        if event.Type == pyfbsdk.FBSceneChangeType.kFBSceneChangeAttach:
               node = event.ChildComponent
               node_cgn = node.ClassGroupName
               
               if node_cgn == 'Model':
                       if not node_cgn in new_nodes:
                               new_nodes[ node_cgn ] = { node.__hash__(): node }
                       else:
                               new_nodes[ node_cgn ][ node.__hash__() ] = node 
            
c1, c2 = run_test()

scn.OnChange.Add( my_callback )
c3, c4 = run_test()

scn.OnChange.Remove( my_callback )
c5, c6 = run_test()

 ...which results in:

>>>

my_cube.FullName = Model::my_cube 22

my_cube object = <pyfbsdk.FBModelCube object at 0x000000003CDD33B8>

my_cube_found_by_name.FullName = Model::my_cube 22

my_cube_found_by_name object = <pyfbsdk.FBModelCube object at 0x000000003CDD33B8>

my_cube == my_cube_found_by_name : True

 

>>>

my_cube.FullName = Model::my_cube 23

my_cube object = <pyfbsdk.FBModelCube object at 0x000000003CDD3408>

my_cube_found_by_name.FullName = Model::my_cube 23

my_cube_found_by_name object = <pyfbsdk.FBModelCube object at 0x000000003C9507B8>

my_cube == my_cube_found_by_name : False

 

my_cube.FullName = Model::my_cube 24

my_cube object = <pyfbsdk.FBModelCube object at 0x000000003CDD3318>

my_cube_found_by_name.FullName = Model::my_cube 24

my_cube_found_by_name object = <pyfbsdk.FBModelCube object at 0x000000003CDD3318>

my_cube == my_cube_found_by_name : True

 

 

Kelly Snapka
Principal Artist
www.dsvolition.com/
Message 3 of 3
StepanK
in reply to: vsnappy1

huh, that's nasty thingy You found!

a side note on references and events -
I suggest that You were rather careful when working with these event handlers. Especially problematic may be certain handlers that may remain active when MoBu is being shut down, no matter how hard I tried to catch shutdown and remove handlers, use try/except clauses etc, the handler still may crash MB... so it shuts down and in some final stage crashes 🙂 Simply, don't be surprised that certain code may behave unusually in event handler function.

---

references - I had some weird situations when I got references to a Fcurve, then I realized that after I changed the layer or what was it - can't remember exactly, the reference got lost (unbound object). Hours of headbanging until I nailed that down finally.

---

Hopefully, searching the object by name might always return proper reference, even though it is slow, of course.

 

Regards,
Stepan

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report