Jump to content

Create a texture resource from image in a folder


Recommended Posts

Hi,

I try now to subdivide my question from my previous post - Looking for script to import images as imageprop.

Does anyone have a good approach to create a texture resource from images loaded from a folder.

I found the example script to load images as symbols(https://developer.vectorworks.net/index.php/Python_Sample_Import_Images_as_Symbols), but I can't figure out how to bind them to a renderworks texture. Afterwards the texture could be used on an extruded object or with a image prob.

Cheers

Jonas

 

Link to comment

Hi Jonas,

 

From Vectorworks 2019 and newer it is possible to create a image based texture by using CreateTextureBitmapD (before you had to use a dummy texture...)

#Part 1 - import and convert image
hPaint = vs.ImportImageFile(thePath, 0,0) #Needs to be deleted afterwards
hImage = vs.CreateImageFromPaint(hPaint, 'Just_a_name')

#Part 2 - Create ShaderRecord and convert to TextureBitmap
hShaderRec = vs.CreateShaderRecord(hTexture, 1, 41) #1 = Color image
hTextureBitMap = vs.CreateTextureBitmapD( hShaderRec)

#Part 3 - Connect the image to the TextureBitmap
vs.SetObjectVariableHandle(hTextureBitMap, 528, hImage) #Were the magic happens!

Hope this helps.

🙂

Gelde-Aart

  • Like 2
Link to comment
  • 1 year later...
On 6/22/2020 at 2:20 PM, jonasfehr said:

Here my complete script to choose a folder and import the images as rendertexture:

 

import os

major, minor, maintenance, platform = vs.GetVersion()
isMac = False
if platform == 1: isMac = True

# define a location to import the images
importPt = (0,0)

symCreatedCnt = 0

err, dirPath = vs.GetFolder( 'Select a Folder' )
if err == 0: # no-error
	hsfDirPath = dirPath
	if isMac: ok, hsfDirPath = vs.ConvertPosix2HSFPath( dirPath )

	fileIndex = 1
	while True: # loop the files
		fileName = vs.GetFilesInFolder( hsfDirPath, fileIndex )
		fileIndex += 1

		if fileName == '': # no more files
			break

		name, ext = os.path.splitext( fileName )
		if ext.lower() == '.png' or ext.lower() == '.jpg':
			imagePath = os.path.join( dirPath, fileName )
						
			hPaint = vs.ImportImageFile(imagePath, 0,0) #Needs to be deleted afterwards
			hImage = vs.CreateImageFromPaint(hPaint, 'Just_a_name')

			hTexture = vs.CreateTexture()
			vs.SetName(hTexture, name+"_tex")
			hShaderRec = vs.CreateShaderRecord(hTexture, 1, 41) #1 = Color image
			hTextureBitMap = vs.CreateTextureBitmapD( hShaderRec)

			vs.SetObjectVariableHandle(hTextureBitMap, 528, hImage) #Were the magic happens!
			
			vs.DelObject(hPaint)
			symCreatedCnt += 1

vs.AlrtDialog( 'Done! Imported ', symCreatedCnt , ' images to textures.' )

Hope it is beneficial for other too...

Hello. This script has some problems in VW2021. Do you have some updates to code. Problem is when i create file with multiple textures and save to "User Libraries"

When apply texture object is black, and texture is missing link with Image file

  • Like 1
Link to comment
  • 1 month later...
  • 7 months later...

Hello
Try to insert that lines. Looks somehow rude, but it seems to "burn" the image into the texture:
Also it fixes the width of the image to (document unit?) 10.
 

			vs.SetObjectVariableHandle(hTextureBitMap, 528, hImage) #Were the magic happens!
			vs.ResetObject(hTexture)
			vs.SetObjectVariableHandle(hTextureBitMap, 528, hImage) 
			
			resolutionx = vs.GetObjectVariableLongInt(hPaint,530)
			resolutiony = vs.GetObjectVariableLongInt(hPaint,531)
			vs.SetTexBFeatureStart(hTextureBitMap, 0, 0) #pixel start
			vs.SetTexBFeatureEnd(hTextureBitMap, resolutionx, 0) #pixel End
			vs.SetTexBitFeatureSize(hTextureBitMap, 10) #size units

 

Edited by DomC
Type Errors
Link to comment

Hello DomC and all,

 

This is really great. This could solve another problem I have.

If I run this script a second time it duplicates the texture with another name ( texture 1, tecture 2, etc.)

if it could skip the images with existing texture that would be great!

 

(Even better if it could reload the image file for existing textures and leave all else the same)

 

What do you think?

 

Bart

Link to comment

This one would update or create new textures:

 


import os

def put_bitmap_in_texture(hTexture, hPaint, image_size): #hTexture Resource, hPaint 
	hImage = vs.CreateImageFromPaint(hPaint, 'Just_a_name')
		
	hShaderRec = vs.CreateShaderRecord(hTexture, 1, 41) #1 = Color image
	hTextureBitMap = vs.CreateTextureBitmapD( hShaderRec)
	vs.SetObjectVariableHandle(hTextureBitMap, 528, hImage) #Were the magic happens!
	vs.ResetObject(hTexture)
	vs.SetObjectVariableHandle(hTextureBitMap, 528, hImage)	
	resolutionx = vs.GetObjectVariableLongInt(hPaint,530)
	resolutiony = vs.GetObjectVariableLongInt(hPaint,531)
	vs.SetTexBFeatureStart(hTextureBitMap, 0, 0) #pixel start
	vs.SetTexBFeatureEnd(hTextureBitMap, resolutionx, 0) #pixel End
	vs.SetTexBitFeatureSize(hTextureBitMap, image_size) #size units
	
	return hTextureBitMap

major, minor, maintenance, platform = vs.GetVersion()
isMac = False
if platform == 1: isMac = True

# define a location to import the images
importPt = (0,0)
image_size = 10

symCreatedCnt = 0
err, dirPath = vs.GetFolder( 'Select a Folder' )

if err == 0: # no-error
	hsfDirPath = dirPath
	if isMac: ok, hsfDirPath = vs.ConvertPosix2HSFPath( dirPath )
	
	name_counter = 1
	fileIndex = 1
	while True: # loop the files
		fileName = vs.GetFilesInFolder( hsfDirPath, fileIndex )
		fileIndex += 1

		if fileName == '': # no more files
			break

		name, ext = os.path.splitext( fileName )
		if ext.lower() == '.png' or ext.lower() == '.jpg':
			imagePath = os.path.join( dirPath, fileName )
						
			hPaint = vs.ImportImageFile(imagePath, 0,0) #Needs to be deleted afterwards
			texture_name = name + "_tex"
			existing_obj_handle = vs.GetObject(texture_name)
			
			update_tex = False
			if existing_obj_handle != vs.Handle(0): #object already exists
				obj_type = vs.GetTypeN(existing_obj_handle)
				
				if obj_type == 97: #object is already a texture > Update
					hTexture = existing_obj_handle
					update_tex = True
					
				else: #object exists but is not a texture
					hTexture = vs.CreateTexture()
					texture_name = name + str(name_counter) +'_tex'
					name_counter += 1
					
				hTextureBitMap = put_bitmap_in_texture(hTexture, hPaint, image_size)
				vs.UpdateThumbnailPreview(hTexture)
			
			else: #new texture
				hTexture = vs.CreateTexture()
				hTextureBitMap = put_bitmap_in_texture(hTexture, hPaint, image_size)
			
			if not update_tex: 
				vs.SetName(hTexture, texture_name)

			vs.DelObject(hPaint)


 

  • Like 2
  • Love 2
Link to comment
  • 1 month later...

The Size of "10" means, that the width line of the texture is set to 10mm.

image.png.b15936aa42a8abf40031fe3973cb4d17.png

If you set your document units to meter as example it will still be 10mm and will show 0.001m. If you want to attach that texture on a 1m x 1m object and it should fit the size you can make the size = 1000 or you can scale the texture mapping by for this object. But this will need another script (Or an enhancement of the import-script) to attach the texture and scale by 
geometry_size_in_mm divided through 10(or what ever size you imported the texture)

 

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...