Cinema 4D - my Python scripts - output spline points

From NoskeWiki
Jump to navigation Jump to search

About

NOTE: This page is a daughter page of: Cinema 4D


This page contains code for a Python script I've written for Cinema 4D. To understand how it compiles see Cinema 4D - my Python scripts.

output_spline_points.py

output_spline_points.py

# For each selected open spline (line), outputs the name and
# point values (x,z,z) to another dialog, plus the console.

import c4d
from c4d import gui

# Unique id numbers for each of the GUI elements
LBL_USAGE = 1000
LBL_INFO1 = 1001
LBL_INFO2 = 1002
LBL_INFO3 = 1003
LBL_INFO4 = 1004
LBL_OUT = 1005
GROUP_TEXT = 10000
TXT_BEFORE_SPLINE = 10001
TXT_AFTER_SPLINE = 10002
TXT_BEFORE_POINT = 10003
TXT_AFTER_POINT = 10004
TXT_BEFORE_CLOSED = 10005
TXT_AFTER_CLOSED = 10006
TXT_BEFORE_SIZE = 10007
TXT_AFTER_SIZE = 10008
CHK_SPLINE = 10009
CHK_POINTS = 10010
CHK_SIZE = 10011
CHK_CLOSED = 10012
CHK_OUTPUT_TO_CONSOLE_ONLY = 10013

class OptionsDialog(gui.GeDialog):
  """ Dialog for outputting spline name and points.
  """
  def CreateLayout(self):
    self.SetTitle('Output Point Information')
    self.AddMultiLineEditText(LBL_USAGE, c4d.BFH_SCALEFIT, inith=40, initw=500,
                              style=c4d.DR_MULTILINE_READONLY)
    self.SetString(LBL_USAGE,
        "USAGE: Does find/replace on selected object names\n"
        "   using regular expression")
    # Output options:
    self.GroupBegin(GROUP_TEXT, c4d.BFH_SCALEFIT, 4, 4)
    #
    self.AddCheckbox(CHK_SPLINE, c4d.BFH_SCALEFIT,
                     initw=1, inith=1, name="print object name: ")
    self.SetBool(CHK_SPLINE, True)
    self.AddEditText(TXT_BEFORE_SPLINE, c4d.BFH_SCALEFIT)
    self.SetString(TXT_BEFORE_SPLINE, 'c = CreateCoutour(\"')
    self.AddStaticText(LBL_INFO1, c4d.BFH_LEFT, name='name') 
    self.AddEditText(TXT_AFTER_SPLINE, c4d.BFH_SCALEFIT)
    self.SetString(TXT_AFTER_SPLINE, '\");\n')
    #
    self.AddCheckbox(CHK_CLOSED, c4d.BFH_SCALEFIT,
                     initw=1, inith=1, name="print closed: ")
    self.SetBool(CHK_CLOSED, True)
    self.AddEditText(TXT_BEFORE_CLOSED, c4d.BFH_SCALEFIT)
    self.SetString(TXT_BEFORE_CLOSED, 'c.SetClosed(')
    self.AddStaticText(LBL_INFO1, c4d.BFH_LEFT, name='0/1') 
    self.AddEditText(TXT_AFTER_CLOSED, c4d.BFH_SCALEFIT)
    self.SetString(TXT_AFTER_CLOSED, ');')
    #
    self.AddCheckbox(CHK_SIZE, c4d.BFH_SCALEFIT,
                     initw=1, inith=1, name="print num points: ")
    self.SetBool(CHK_SIZE, True)
    self.AddEditText(TXT_BEFORE_SIZE, c4d.BFH_SCALEFIT)
    self.SetString(TXT_BEFORE_SIZE, '// ')
    self.AddStaticText(LBL_INFO1, c4d.BFH_LEFT, name='name') 
    self.AddEditText(TXT_AFTER_SIZE, c4d.BFH_SCALEFIT)
    self.SetString(TXT_AFTER_SIZE, ' points:')
    #
    self.AddCheckbox(CHK_POINTS, c4d.BFH_SCALEFIT,
                     initw=1, inith=1, name="print points: ")
    self.SetBool(CHK_POINTS, True)
    self.AddEditText(TXT_BEFORE_POINT, c4d.BFH_SCALEFIT)
    self.SetString(TXT_BEFORE_POINT, 'c.AddPoint(')
    self.AddStaticText(LBL_INFO1, c4d.BFH_LEFT, name='x,y,z') 
    self.AddEditText(TXT_AFTER_POINT, c4d.BFH_SCALEFIT)
    self.SetString(TXT_AFTER_POINT, ');')
    #
    self.GroupEnd()
    self.AddSeparatorH(c4d.BFH_SCALE);
    self.AddCheckbox(CHK_OUTPUT_TO_CONSOLE_ONLY, c4d.BFH_SCALEFIT,
                     initw=1, inith=1, name="output to console only")
    # Buttons - an Ok and Cancel button:
    self.AddDlgGroup(c4d.DLG_OK|c4d.DLG_CANCEL);
    self.ok = False
    return True
 
  # React to user's input:
  def Command(self, id, msg):
    if id==c4d.DLG_CANCEL:
      self.Close()
    elif id==c4d.DLG_OK:
      self.ok = True
      self.opt_before_spline = self.GetString(TXT_BEFORE_SPLINE)
      self.opt_after_spline = self.GetString(TXT_AFTER_SPLINE)
      self.opt_before_point = self.GetString(TXT_BEFORE_POINT)
      self.opt_after_point = self.GetString(TXT_AFTER_POINT)
      self.opt_before_size = self.GetString(TXT_BEFORE_SIZE)
      self.opt_after_size = self.GetString(TXT_AFTER_SIZE)
      self.opt_before_closed = self.GetString(TXT_BEFORE_CLOSED)
      self.opt_after_closed = self.GetString(TXT_AFTER_CLOSED)
      self.opt_out_spline = self.GetBool(CHK_SPLINE)
      self.opt_out_points = self.GetBool(CHK_POINTS)      
      self.opt_out_size = self.GetBool(CHK_SIZE)
      self.opt_out_closed = self.GetBool(CHK_CLOSED)
      self.opt_out_console_only = self.GetBool(CHK_OUTPUT_TO_CONSOLE_ONLY)
      self.Close()
    return True

class TextDialog(gui.GeDialog):
  """ Dialog for outputting spline name and points.
  """
  def CreateLayout(self):
    self.SetTitle('Output (for copy/paste)')
    self.AddMultiLineEditText(LBL_USAGE, c4d.BFH_SCALEFIT, inith=200, initw=500,
                              style=c4d.DR_MULTILINE_READONLY)
    self.SetString(LBL_USAGE, self.opt_text)
    self.AddDlgGroup(c4d.DLG_OK);
    return True
 
  # React to user's input:
  def Command(self, id, msg):
    if id==c4d.DLG_OK:
      self.Close()
    return True


def main():
  # Get the selected objects, including children.
  selection = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN)
  
  if len(selection) <= 0:
    gui.MessageDialog('Must select spline object(s).')
    return

  # Open the options dialogue to let users choose their options.
  dlg = OptionsDialog()
  dlg.Open(c4d.DLG_TYPE_MODAL, defaultw=500, defaulth=50)
  if not dlg.ok:
    return

  print '========== OUTPUT SPLINE POINTS =========='
  out = ''
  num_splines_out = 0
  for i in range(0,len(selection)):
    spline = selection[i]
    if not spline.CheckType(c4d.Ospline):
      continue
    num_splines_out += 1
    
    num_points = spline.GetPointCount()
    closed = spline[c4d.SPLINEOBJECT_CLOSED]
    print ' '
    if dlg.opt_out_spline:
      line = dlg.opt_before_spline + spline.GetName() + dlg.opt_after_spline
      print line
      out += line + '\n'
    if dlg.opt_out_closed:
      line = dlg.opt_before_closed + str(closed) + dlg.opt_after_closed
      print line
      out += line + '\n'
    if dlg.opt_out_size:
      line = dlg.opt_before_size + str(num_points) + dlg.opt_after_size
      print line
      out += line + '\n'
    if dlg.opt_out_points:
      for i in range(0,num_points):
        pt = spline.GetPoint(i)
        line = (dlg.opt_before_point + str(pt.x) + ',' +
               str(pt.y) + ',' + str(pt.z) + dlg.opt_after_point)
        print line
        out += line + '\n' 
        
  
  if dlg.opt_out_console_only:
    gui.MessageDialog(str(num_splines_out) +
                      ' splines output to script console')
    exit
                    
  # Open the txt dialogue for user to copy/paste output:
  dlg = TextDialog()
  dlg.opt_text = out
  dlg.Open(c4d.DLG_TYPE_MODAL, defaultw=500, defaulth=200)
  


if __name__=='__main__':
    main()


See Also


Code license
For all of the code on my site... if there are specific instruction or licence comments please leave them in. If you copy my code with minimum modifications to another webpage, or into any code other people will see I would love an acknowledgment to my site.... otherwise, the license for this code is more-or-less WTFPL (do what you want)! If only copying <20 lines, then don't bother. That said - if you'd like to add a web-link to my site www.andrewnoske.com or (better yet) the specific page with code, that's a really sweet gestures! Links to the page may be useful to yourself or your users and helps increase traffic to my site. Hope my code is useful! :)