Jump to content
Developer Wiki and Function Reference Links ×

Help finding center of Symbol!


Ben Wingrove

Recommended Posts

I am trying to make a simple Marionette to rotate certain symbols by name in my drawing, every time I run the script it is offsetting the center of the symbol and moving it around, I have tried different nodes to get the center but they all seem to have the same effect!! I have been trying to get this to work for a few days now and could really do with some pointers.

 

Thanks

 

B

 

5a1aff49bc9b0_ScreenShot2017-11-26at9_44_39AM.thumb.png.d01a7505819e023b192c5377374eb977.png

 

Link to comment

This node gives you the center of the Bounding Box.

 

'''
    Changed on 22.02.2016 : Wrong Var was used in the calcul. of the center    
    Changed on 04.04.2016 : Created the 'def GetBBoxN (h)'
    Changed on 18.04.2016 : set Initialize function: Marionette.InitializeView
    Changed on 27.11.2017 : Force TopView
    
    author: patrick winkler
'''

import vs
import Marionette

@Marionette.NodeDefinition
class Params(metaclass = Marionette.OrderedClass):
    this = Marionette.Node( 'Get Bounding Box' )
    this.SetDescription("Returns the bounding box's coordinates of the projection of an object on the screen plane")
    
    # IMPORTANT:  SetThe View to Top while Execution - The BB Values are otherwise incorrect!!!!
    this.SetInitFunc(Marionette.InitializeView)
    this.SetFinalFunc (Marionette.RestoreView)    
    
    obj = Marionette.PortIn(vs.Handle(0))
    obj.SetDescription( "The input object" )
    
    topL2D = Marionette.PortOut()
    topL2D.SetDescription( "The top left coordinates of the bounding box" )
    
    botR2D = Marionette.PortOut()        
    botR2D.SetDescription( "The bottom right coordinatesr of the bounding box" )

    width = Marionette.PortOut()        
    width.SetDescription("The width of the bounding box.")    
    
    height = Marionette.PortOut()        
    height.SetDescription("The height of the bounding box.")

    center = Marionette.PortOut()        
    center.SetDescription("The centerpoint of the bounding box.")

def RunNode(self):    
    def GetBBoxN (h, top_view = True):
        '''
            Returns top left point, bottom right point, width, height and center of the object bounding box (as tuple).
            
            IMPORTANT: The result depends on the view in VW if top_view is False. 
        '''    
        if top_view:
            # Change the view to Top temporarily
            orig_view = vs.GetView()
            vs.SetView(0, 0, 0, 0, 0, 0)
        
        box = vs.GetBBox(h)
        topL2D = box[0]
        botR2D = box[1]
        
        center = [None, None]
        
        BB_width  = botR2D[0] - topL2D[0]
        BB_height = topL2D[1] - botR2D[1]
            
        center[0] = topL2D[0] + BB_width / 2
        center[1] = topL2D[1] - BB_height / 2
        
        center = tuple (center)
            
        if top_view:
            # Restore the original view
            xAngle, yAngle, zAngle, offset = orig_view
            xDistance, yDistance, zDistance = offset
            
            vs.SetView(xAngle, yAngle, zAngle, xDistance, yDistance, zDistance)
        
        return (topL2D, botR2D, BB_width, BB_height, center)
    
    obj = self.Params.obj.value    
    
    if not obj:
        raise ValueError('No object handle was provided!')
    
    topL2D, botR2D, BB_width, BB_height, center = GetBBoxN(obj, top_view = True)
    
    # SET THE OUT PORTS
    self.Params.topL2D.value = topL2D
    self.Params.botR2D.value = botR2D
    
    self.Params.width.value = BB_width
    self.Params.height.value = BB_height
    
    self.Params.center.value = tuple (center)
    

 

 

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...