| Python Script Engine 8.3 GL Studio Editor Python Script API

BasicGLPolygon.py

Example on how to create a simple GLPolygon.

Example on how to create a simple GLPolygon.

1# Import all referenced modules
2 import Editor
3 import EditorLog
4 import GLPolygon
5 import Group
6 import VertexArray
7 import Vertex
8
9# Get the currently open and in focus document
10currentDoc = Editor.GetCurrentDocument()
11
12# Create some vertices to use as the bounds of the GLPolygon
13verts = VertexArray.VertexArray()
14verts.Insert(Vertex.Vertex( 10, 10, 0, 100, 0, 0, 185))
15verts.Insert(Vertex.Vertex(210, 10, 0, 100, 0, 0, 185))
16verts.Insert(Vertex.Vertex(210, 110, 0, 100, 0, 0, 185))
17verts.Insert(Vertex.Vertex( 10, 110, 0, 100, 0, 0, 185))
18
19# Create the GLPolygon, passing in the vertices we just created
20polygon = GLPolygon.GLPolygon(verts)
21polygon.SetPolygonMode( polygon.POLY_MODE_FILLED )
22polygon.SetName('poly')
23
24# Add the new GLPolygon to our current Document
25currentDoc.InsertObject( polygon )
26
27# New in GL Studio 6.0.1.
28# How to create a Yellow GLPolygon from a Python list of Vertices.
29vertsList = [
30 Vertex.Vertex( 50, 50, 0, 200, 220, 0, 255),
31 Vertex.Vertex(120, 50, 0, 200, 220, 0, 255),
32 Vertex.Vertex(120, 120, 0, 200, 220, 0, 255),
33 Vertex.Vertex( 50, 120, 0, 200, 220, 0, 255),
34]
35
36# Create a polygon and set its fill mode to filled.
37yellowPolygon = GLPolygon.GLPolygon( vertsList )
38yellowPolygon.SetPolygonMode( yellowPolygon.POLY_MODE_FILLED )
39yellowPolygon.SetName('yellow')
40
41currentDoc.InsertObject( yellowPolygon )
42
43# Make a larger polygon and set a location.
44multiPolygon = GLPolygon.GLPolygon([
45 Vertex.Vertex( 80, 65, 0, 0, 40, 0, 0),
46 Vertex.Vertex( 95, 35, 0, 0, 40, 0, 0),
47 Vertex.Vertex(180, 35, 0, 0, 40, 150, 100),
48 Vertex.Vertex(195, 65, 0, 0, 255, 150, 100),
49 Vertex.Vertex(180, 100, 0, 255, 40, 150, 255),
50 Vertex.Vertex( 95, 100, 0, 0, 40, 0, 0),
51 ],
52 Vertex.Vertex(-130, 0, 0)
53)
54# Fill and outline mode, and turn on Gouraud shading
55multiPolygon.SetPolygonMode( multiPolygon.POLY_MODE_FILL_AND_OUTLINE )
56multiPolygon.SetShadingMode( multiPolygon.SHADING_GOURAUD )
57multiPolygon.SetName('multi')
58
59# Insert a group and add objects to it
60group = Group.Group()
61group.SetName('group')
62currentDoc.InsertObject(group)
63
64# Add an object with no parent doc into the group
65group.InsertObject(multiPolygon)
66
67# Move an object already in the document into the group
68group.MoveObjectToGroup(yellowPolygon)
69
70
71# Find an object in the document by its name
72lookup = currentDoc.GetObjectByName('group')
73
74# Objects are generally returned as instances of the base class DisplayObject
75 EditorLog.PrintInfo( str( type(lookup) ) + ', really a ' + lookup.GetObjectClassName() ) # Prints "<class 'GLPolygon.DisplayObject'>, really a Group"
76
77# Try casting it to other objects that it might be.
78poly = GLPolygon.CastToGLPolygon(lookup) # Returns None because this is a Group, not a polygon
79group = Group.CastToGroup(lookup) # Succeeds
80
81 EditorLog.PrintInfo(poly.GetName() if poly else 'Not a polygon') # Prints "Not a polygon" because the cast failed
82 EditorLog.PrintInfo(group.GetName() if group else 'Not a Group') # Prints "group"
83
84# New in GL Studio 6.2: Iterate over a Group's objects
85 for x in group:
86 EditorLog.PrintInfo( x.GetName() + ' is in ' + x.GetParent().GetName() )
87
88
89# Define a simple processing function prints some info about the object (used below)
90 def Process(prefix, x):
91 EditorLog.PrintInfo( '%s.%s: %s' % (prefix, x.GetName(), x.GetObjectClassName()) )
92
93# Recurse into groups for processing
94 def PrintNames(group, prefix=''):
95 prefix = (prefix + '.' if prefix else '') + group.GetName()
96 for x in group:
97 Process(prefix, x)
98 subgroup = Group.CastToGroup(x)
99 if subgroup:
100 PrintNames(subgroup, prefix)
101
102PrintNames( currentDoc.GetObjectList() )
Editor::GetCurrentDocument
Document GetCurrentDocument()
Gets the current document.
EditorLog::PrintInfo
void PrintInfo(String str)
Output an info string to the editor log.
Definition: EditorLog.i:89
GLPolygon::GLPolygon
GLPolygon(VertexArray &vertices)
Creates a polygon with the specified vertices.
GLPolygon::CastToGLPolygon
static GLPolygon * CastToGLPolygon(DisplayObject *obj)
Casts the Display Object as a GLPolygon NOTE: perform an isNULL check on the returned value to ensure...
Group::Group
Group()
Constructs an empty group.
Group::CastToGroup
static Group * CastToGroup(DisplayObject *obj)
Casts a DisplayObject to a Group (returns null on failure to cast)
VertexArray::VertexArray
VertexArray()
default ctor
Vertex::Vertex
Vertex()
Default constructor.