Python Script Engine  8.2
GL Studio Editor Python Script API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
BasicGLPolygon.py

Example on how to create a simple GLPolygon.

Example on how to create a simple GLPolygon.

1# Import all referenced modules
2import Editor
3import EditorLog
4import GLPolygon
5import Group
6import VertexArray
7import 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
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
75EditorLog.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
81EditorLog.PrintInfo(poly.GetName() if poly else 'Not a polygon') # Prints "Not a polygon" because the cast failed
82EditorLog.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
85for 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)
90def Process(prefix, x):
91 EditorLog.PrintInfo( '%s.%s: %s' % (prefix, x.GetName(), x.GetObjectClassName()) )
92
93# Recurse into groups for processing
94def 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() )
Document GetCurrentDocument()
Gets the current document.
void PrintInfo(String str)
Output an info string to the editor log.
Definition: EditorLog.i:89
GLPolygon(VertexArray &vertices)
Creates a polygon with the specified vertices.
static GLPolygon * CastToGLPolygon(DisplayObject *obj)
Casts the Display Object as a GLPolygon NOTE: perform an isNULL check on the returned value to ensure...
Group()
Constructs an empty group.
static Group * CastToGroup(DisplayObject *obj)
Casts a DisplayObject to a Group (returns null on failure to cast)
VertexArray()
default ctor
Vertex()
Default constructor.